这些时间做安卓盒子项目,因为安卓电视的显示器比较大,所以一个界面显示 很多数据 ,最多的时候,一个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>
界面布局是随意写的,请根据实际情况调整。上丑图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android使用RecyclerView实现列表数据选择 *** 作全部内容,希望文章能够帮你解决Android使用RecyclerView实现列表数据选择 *** 作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)