Android使用RecyclerView实现列表数据选择操作
程序员文章站
2023-11-24 21:53:28
这些时间做安卓盒子项目,因为安卓电视的显示器比较大,所以一个界面显示 很多数据 ,最多的时候,一个actvity中用到了好几个recyclerview。
在recyc...
这些时间做安卓盒子项目,因为安卓电视的显示器比较大,所以一个界面显示 很多数据 ,最多的时候,一个actvity中用到了好几个recyclerview。
在recyclerview中实现item选中处理时,发现用checkbox的oncheckedchangelistener监听事件时,会达不到预期,所以用了onclicklistener来实现。
主界面代码:
public class checkrecyclerviewactivity extends appcompatactivity implements checkadapter.checkitemlistener { //适配器 private checkadapter mcheckadapter; //列表 private recyclerview check_rcy; //全选操作 private checkbox check_all_cb; //列表数据 private list<checkbean> dataarray; //选中后的数据 private list<checkbean> checkedlist; private boolean isselectall; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_check_recyclerview); checkedlist = new arraylist<>(); initdatas(); initviews(); } private void initviews() { check_rcy = (recyclerview) findviewbyid(r.id.check_rcy); check_all_cb = (checkbox) findviewbyid(r.id.check_all_cb); linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(this, linearlayoutmanager.vertical, false); check_rcy.setlayoutmanager(linearlayoutmanager); mcheckadapter = new checkadapter(this, dataarray, this); check_rcy.setadapter(mcheckadapter); //如果使用checkbox的oncheckedchangelistener事件,则选中事件会有一些意想不到的结果,欢迎体验 //在列表item中的checkbox也一样的效果 check_all_cb.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { isselectall = !isselectall; checkedlist.clear(); if (isselectall) {//全选处理 checkedlist.addall(dataarray); } for (checkbean checkbean : dataarray) { checkbean.setchecked(isselectall); } mcheckadapter.notifydatasetchanged(); } }); } private void initdatas() { dataarray = new arraylist<>(); for (int i = 0; i < 20; i++) { checkbean bean = new checkbean(); bean.setorder(string.valueof(i + 1)); bean.setname("名称_" + i); bean.setcontent("第" + i + "条内容"); bean.settime(new simpledateformat("yyyy-mm-dd hh:mm:ss").format(new date())); dataarray.add(bean); } } @override public void itemchecked(checkbean checkbean, boolean ischecked) { //处理item点击选中回调事件 if (ischecked) { //选中处理 if (!checkedlist.contains(checkbean)) { checkedlist.add(checkbean); } } else { //未选中处理 if (checkedlist.contains(checkbean)) { checkedlist.remove(checkbean); } } //判断列表数据是否全部选中 if (checkedlist.size() == dataarray.size()) { check_all_cb.setchecked(true); } else { check_all_cb.setchecked(false); } } }
列表数据适配器:
public class checkadapter extends recyclerview.adapter<checkadapter.viewholder> { private context mcontext; private list<checkbean> mdatas; private checkitemlistener mchecklistener; public checkadapter(context mcontext, list<checkbean> mdatas, checkitemlistener mchecklistener) { this.mcontext = mcontext; this.mdatas = mdatas; this.mchecklistener = mchecklistener; } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(mcontext).inflate(r.layout.check_recyclerview_item, parent, false); viewholder viewholder = new viewholder(view); return viewholder; } @override public void onbindviewholder(final viewholder holder, final int position) { final checkbean bean = mdatas.get(position); holder.item_order_tv.settext(bean.getorder()); holder.item_name_tv.settext(bean.getname()); holder.item_content_tv.settext(bean.getcontent()); holder.item_time_tv.settext(bean.gettime()); holder.item_cb.setchecked(bean.ischecked()); //点击实现选择功能,当然可以把点击事件放在item_cb对应的checkbox上,只是焦点范围较小 holder.item_content_ll.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { bean.setchecked(!bean.ischecked()); holder.item_cb.setchecked(bean.ischecked()); if (null != mchecklistener) { mchecklistener.itemchecked(bean, holder.item_cb.ischecked()); } notifydatasetchanged(); } }); } @override public int getitemcount() { return mdatas.size(); } public class viewholder extends recyclerview.viewholder { //序号 private textview item_order_tv; //选择 private checkbox item_cb; //整个条目 private linearlayout item_content_ll; //名称 textview item_name_tv; //内容 textview item_content_tv; //时间 private textview item_time_tv; public viewholder(view itemview) { super(itemview); item_order_tv = (textview) itemview.findviewbyid(r.id.item_order_tv); item_cb = (checkbox) itemview.findviewbyid(r.id.item_cb); item_name_tv = (textview) itemview.findviewbyid(r.id.item_name_tv); item_content_tv = (textview) itemview.findviewbyid(r.id.item_content_tv); item_time_tv = (textview) itemview.findviewbyid(r.id.item_time_tv); item_content_ll = (linearlayout) itemview.findviewbyid(r.id.item_content_ll); } } public interface checkitemlistener { void itemchecked(checkbean checkbean, boolean ischecked); } }
测试数据实体bean:
public class checkbean implements serializable { private string order; private string name; private string content; private string time; private boolean ischecked; public string getorder() { return order; } public void setorder(string order) { this.order = order; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getcontent() { return content; } public void setcontent(string content) { this.content = content; } public string gettime() { return time; } public void settime(string time) { this.time = time; } public boolean ischecked() { return ischecked; } public void setchecked(boolean checked) { ischecked = checked; } }
主界面布局文件:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:background="@drawable/drawable_white_round_bg" android:orientation="vertical"> <linearlayout android:layout_width="match_parent" android:layout_height="20dp" android:orientation="horizontal"> <textview android:layout_width="40dp" android:layout_height="match_parent" android:text="序号" android:layout_marginleft="10dp" android:textsize="12sp" android:textcolor="#333333" android:gravity="center" /> <checkbox android:id="@+id/check_all_cb" android:layout_width="12dp" android:layout_gravity="center" android:button="@null" android:layout_marginleft="10dp" android:layout_marginright="10dp" android:layout_height="12dp" android:background="@drawable/drawable_cb_selector" /> <textview android:layout_width="wrap_content" android:layout_height="match_parent" android:text="名称" android:layout_marginleft="10dp" android:textsize="12sp" android:textcolor="#333333" android:gravity="center" /> <textview android:layout_width="100dp" android:layout_height="match_parent" android:text="内容" android:layout_marginleft="10dp" android:textsize="12sp" android:textcolor="#333333" android:gravity="center" /> <textview android:layout_width="80dp" android:layout_height="match_parent" android:text="时间" android:layout_marginleft="10dp" android:textsize="12sp" android:textcolor="#333333" android:gravity="center" /> </linearlayout> <view android:layout_width="match_parent" android:layout_height="1px" android:background="#bcbcbc"></view> <android.support.v7.widget.recyclerview android:id="@+id/check_rcy" android:layout_width="match_parent" android:layout_height="wrap_content"></android.support.v7.widget.recyclerview> </linearlayout>
列表item布局:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <linearlayout android:id="@+id/item_content_ll" android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <textview android:id="@+id/item_order_tv" android:layout_width="40dp" android:layout_height="match_parent" android:layout_marginleft="10dp" android:textsize="15sp" android:textcolor="#333333" android:gravity="center" /> <checkbox android:id="@+id/item_cb" android:layout_width="20dp" android:layout_gravity="center" android:button="@null" android:layout_marginleft="10dp" android:layout_marginright="10dp" android:layout_height="20dp" android:background="@drawable/drawable_cb_selector" /> <textview android:id="@+id/item_name_tv" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginleft="10dp" android:textsize="15sp" android:textcolor="#333333" android:gravity="center" /> <textview android:id="@+id/item_content_tv" android:layout_width="100dp" android:layout_height="match_parent" android:layout_marginleft="10dp" android:textsize="15sp" android:textcolor="#333333" android:gravity="center" /> <textview android:id="@+id/item_time_tv" android:layout_width="120dp" android:layout_height="match_parent" android:layout_marginleft="10dp" android:textsize="15sp" android:textcolor="#333333" android:gravity="center" /> </linearlayout> <view android:layout_width="match_parent" android:layout_height="1px" android:background="#bcbcbc"/> </linearlayout>
界面布局是随意写的,请根据实际情况调整。上丑图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。