Android中RecyclerView实现商品分类功能
程序员文章站
2021-12-03 08:59:58
本文实例为大家分享了android中recyclerview实现商品分类功能的具体代码,供大家参考,具体内容如下三个个recyclerview实现//左边的布局
本文实例为大家分享了android中recyclerview实现商品分类功能的具体代码,供大家参考,具体内容如下
三个个recyclerview实现
//左边的布局
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="vertical"> <textview android:id="@+id/tv_name" android:textsize="18sp" android:text="阿萨德发的" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout>
//右边的布局
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/name"/> <android.support.v7.widget.recyclerview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/right_recy" android:layout_below="@+id/name"/> </relativelayout>
//子布局
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <imageview android:id="@+id/image2" android:layout_width="90dp" android:layout_height="90dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/title1" /> </linearlayout>
//定义一个接口
public interface callback { void onsuccess(list<leftbean.databean> list); void onfailer(string error); }
//左边的model层
public class leftmodel { private string path="http://www.zhaoapi.cn/product/getcatagory"; public void getdata(final callback callback){ okhttp okhttp=new okhttp(); okhttp.get(path).getdataliserner(new okhttp.getdata() { @override public void data(string s) { gson gson=new gson(); leftbean json = gson.fromjson(s, leftbean.class); list<leftbean.databean> data = json.getdata(); if (data!=null){ callback.onsuccess(data); }else { callback.onfailer("失败"); } } }); } }
//左边的presenter层
public class leftpresenter { private leftview leftview; private final leftmodel leftmodel; public leftpresenter(leftview leftview) { this.leftview = leftview; leftmodel = new leftmodel(); } public void showleft(){ leftmodel.getdata(new callback() { @override public void onsuccess(list<leftbean.databean> list) { leftview.onsuccess(list); } @override public void onfailer(string error) { leftview.failer(error); } }); } }
//view层
public interface leftview { void onsuccess(list<leftbean.databean> list); void failer(string error); }
//左边的适配器
public class leftrecycadapter extends recyclerview.adapter<leftrecycadapter.leftviewholer>{ private context mcontext; private list<leftbean.databean> list; public leftrecycadapter(context mcontext, list<leftbean.databean> list) { this.mcontext = mcontext; this.list = list; } @nonnull @override public leftviewholer oncreateviewholder(@nonnull viewgroup viewgroup, int viewtype) { view view = layoutinflater.from(mcontext).inflate(r.layout.left_item, viewgroup,false); leftviewholer leftviewholer=new leftviewholer(view); return leftviewholer; } @override public void onbindviewholder(@nonnull leftviewholer leftviewholer, int position) { leftviewholer.textview.settext(list.get(position).getname()); } @override public int getitemcount() { return list.size(); } public class leftviewholer extends recyclerview.viewholder { private textview textview; public leftviewholer(@nonnull view itemview) { super(itemview); textview=itemview.findviewbyid(r.id.tv_name); textview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { onclicklistener.onclick(v,getadapterposition()); } }); } } public interface onclicklistener{ void onclick(view view,int position); } onclicklistener onclicklistener; public void setonclicklistener(onclicklistener onclicklistener){ this.onclicklistener=onclicklistener; } }
开始右边的了
//右边的接口
public interface callbackright { void onsuccess2(list<rightbean.databean> list); void onfailer2(string error); }
//右边的model层
public class rightmodel { // private string path1="http://www.zhaoapi.cn/product/getproductcatagory?cid=3&tdsourcetag=s_pcqq_aiomsg"; public void showright(final string cid2, final callbackright callbackright){ okhttp okhttp=new okhttp(); okhttp.get(cid2).getdataliserner(new okhttp.getdata() { @override public void data(string s) { gson gson=new gson(); rightbean json = gson.fromjson(s, rightbean.class); list<rightbean.databean> data = json.getdata(); if (data!=null){ callbackright.onsuccess2(data); }else { callbackright.onfailer2("错误"); } } }); } }
//右边的presenter层
public class rightpresenter { private final rightmodel rightmodel; private rightview rightview; public rightpresenter(rightview rightview) { this.rightview = rightview; rightmodel = new rightmodel(); } public void showright(string id){ rightmodel.showright(id, new callbackright() { @override public void onsuccess2(list<rightbean.databean> list) { rightview.onsuccess2(list); } @override public void onfailer2(string error) { rightview.onfailer2(error); } }); } }
//右边的view层
public interface rightview { void onsuccess2(list<rightbean.databean> list); void onfailer2(string error); }
//右边的适配器
public class rightrecycadapter extends recyclerview.adapter<rightrecycadapter.viewholder> { private context mcontext; private list<rightbean.databean> list; public rightrecycadapter(context mcontext, list<rightbean.databean> list) { this.mcontext = mcontext; this.list = list; } @nonnull @override public viewholder oncreateviewholder(@nonnull viewgroup viewgroup, int viewtype) { view view = layoutinflater.from(mcontext).inflate(r.layout.right_item, viewgroup, false); viewholder viewholder=new viewholder(view); return viewholder; } @override public void onbindviewholder(@nonnull viewholder viewholder, int position) { viewholder.textview.settext(list.get(position).getname()); list<rightbean.databean.listbean> list = this.list.get(position).getlist(); gridlayoutmanager gridlayoutmanager=new gridlayoutmanager(mcontext,3); viewholder.recyclerview.setlayoutmanager(gridlayoutmanager); childadapter childadapter=new childadapter(mcontext,list); viewholder.recyclerview.setadapter(childadapter); } @override public int getitemcount() { return list.size(); } public class viewholder extends recyclerview.viewholder { private textview textview; private recyclerview recyclerview; public viewholder(@nonnull view itemview) { super(itemview); textview=itemview.findviewbyid(r.id.name); recyclerview=itemview.findviewbyid(r.id.right_recy); } } }
//子类适配器
public class childadapter extends recyclerview.adapter<childadapter.viewholder> { private context context; private list<rightbean.databean.listbean> list; public childadapter(context context, list<rightbean.databean.listbean> list) { this.context = context; this.list = list; } @nonnull @override public viewholder oncreateviewholder(@nonnull viewgroup viewgroup, int viewtype) { view view = layoutinflater.from(context).inflate(r.layout.child, viewgroup, false); viewholder viewholder=new viewholder(view); return viewholder; } @override public void onbindviewholder(@nonnull viewholder viewholder, int position) { viewholder.textview.settext(list.get(position).getname()); picasso.with(context).load(list.get(position).geticon()).into(viewholder.imageview); } @override public int getitemcount() { return list.size(); } public class viewholder extends recyclerview.viewholder { private imageview imageview; private textview textview; public viewholder(@nonnull view itemview) { super(itemview); imageview=itemview.findviewbyid(r.id.image2); textview=itemview.findviewbyid(r.id.title1); } } }
//开始使用
public class fragment1 extends fragment implements leftview,rightview { private view view; private recyclerview left; private recyclerview right; private rightpresenter rightpresenter; handler handler=new handler(){ @override public void handlemessage(message msg) { super.handlemessage(msg); final list<leftbean.databean> list = (list<leftbean.databean>) msg.obj; linearlayoutmanager linearlayoutmanager=new linearlayoutmanager(getactivity(),linearlayoutmanager.vertical,false); left.setlayoutmanager(linearlayoutmanager); leftrecycadapter leftrecycadapter=new leftrecycadapter(getactivity(),list); left.setadapter(leftrecycadapter); leftrecycadapter.setonclicklistener(new leftrecycadapter.onclicklistener() { @override public void onclick(view view, int position) { int cid = list.get(position).getcid(); rightpresenter.showright("http://www.zhaoapi.cn/product/getproductcatagory?cid="+cid); } }); //list<rightbean.databean> list1 = (list<rightbean.databean>) msg.obj; } }; @nullable @override public view oncreateview(@nonnull layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view = inflater.inflate(r.layout.fragment_fragment1, container, false); initview(); return view; } private void initview() { leftpresenter leftpresenter=new leftpresenter(this); leftpresenter.showleft(); left = (recyclerview) view.findviewbyid(r.id.left_recy); right = (recyclerview) view.findviewbyid(r.id.right_recy); rightpresenter = new rightpresenter(this); } @override public void onsuccess(list<leftbean.databean> list) { message message = message.obtain(); message.obj=list; handler.sendmessage(message); } @override public void failer(string error) { } @override public void onsuccess2(final list<rightbean.databean> list) { /*message message = message.obtain(); message.obj=list; handler.sendmessage(message);*/ handler.post(new runnable() { @override public void run() { linearlayoutmanager linear= new linearlayoutmanager(getactivity(),linearlayoutmanager.vertical,false); right.setlayoutmanager(linear); rightrecycadapter rightrecycadapter=new rightrecycadapter(getactivity(),list); right.setadapter(rightrecycadapter); } }); } @override public void onfailer2(string error) { } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: JDBC获取数据库连接由浅入深
推荐阅读
-
Android中AlarmManager+Notification实现定时通知提醒功能
-
Android Recyclerview实现上拉加载更多功能
-
Android 中RecyclerView顶部刷新实现详解
-
Android中RecyclerView实现多级折叠列表效果(二)
-
Android中RecyclerView实现多级折叠列表效果(TreeRecyclerView)
-
android中关于call拨号功能的实现方法
-
android在连拍菜单中增加连拍张数选项功能实现代码
-
Android Recyclerview实现多选,单选,全选,反选,批量删除的功能
-
Android中CountDownTimer 实现倒计时功能
-
Android实现打开手机淘宝并自动识别淘宝口令弹出商品信息功能