RecyclerView中使用CheckBox出现勾选混乱的解决方案

来源:互联网 发布:屏幕特效软件 编辑:程序博客网 时间:2024/05/22 06:46

做安卓的,应该都有使用过ListView或RecyclerView,并且可能都有遇到过在这个两个控件中使用CheckBox的时候出现勾选错乱的问题。这是因为ListView或RecyclerView都使用了复用机制,当在ListView或RecyclerView中的每一项都添加一个CheckBox时,勾选当前页面的几个CheckBox会发现下面还有其他的CheckBox也被勾选了,今天我们就来讨论如何解决这个问题。
首先创建一个一个项目,然后在activity_main中添加一个ListView或RecyclerView控件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:background="@color/xgsg_background_gray"    android:layout_width="match_parent"    android:layout_height="match_parent">    <android.support.v7.widget.RecyclerView        android:id="@+id/recyclerView"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/xgsg_background_gray"        android:scrollbars="vertical"/></LinearLayout>

接下来为这个RecyclerView创建一个item布局文件,命名为item_recyclerview,并添加一个CheckBox空间,代码如下:

<?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="wrap_content"    android:background="@color/white"    android:orientation="horizontal">    <CheckBox        android:id="@+id/check"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="@null"        android:button="@drawable/check_state"        android:layout_width="20dp"        android:layout_height="20dp" /></LinearLayout>

接下来就是编辑MainActivity,findViewById找到布局中的控件,然后准备数据,为其设置Adapter等,
这些都是使用ListView或RecyclerView的基本流程,使用过ListView或RecyclerView的朋友,应该很清楚,这里我就不再废话了,下面就直接讲重点吧。
解决CheckBox的选择错乱,只需要两步。
一、定义一个全局变量数组public boolean[] flag;//把flag数组定义为全局变量
用于保存CheckBox的选择状态。
二、我们在设置Adapter继承RecyclerView.Adapter的时候只需在onBindViewHoder方法编写这个几句代码
复用CheckBox的时候都会去调用一次CheckBox的选择监听,所以我们第一次先把监听状态设置为空,直接从保存好的选中状态数组中获取设置它的选择状态,然后在重新设置它的选择监听把操作后的选择状态重新保存选中状态数组中,这样我们每次获取到的就都是最新的选择状态。
myViewHolder.checkBox.setOnCheckedChangeListener(null);//先设置一次CheckBox的选中监听器,传入参数null
myViewHolder.checkBox.setChecked(flag[position]);//用数组中的值设置CheckBox的选中状态

//再设置一次CheckBox的选中监听器,当CheckBox的选中状态发生改变时,把改变后的状态储存在数组中              myViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {                  @Override                  public void onCheckedChanged(CompoundButton compoundButton, boolean b) {                      flag[position] = b;                  }              });  

完整代码如下:

 @Override          public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {              MyViewHolder myViewHolder = (MyViewHolder) holder;              myViewHolder.checkBox.setText(content.get(position));              myViewHolder.checkBox.setOnCheckedChangeListener(null);//先设置一次CheckBox的选中监听器,传入参数null              myViewHolder.checkBox.setChecked(flag[position]);//用数组中的值设置CheckBox的选中状态              //再设置一次CheckBox的选中监听器,当CheckBox的选中状态发生改变时,把改变后的状态储存在数组中              myViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {                  @Override                  public void onCheckedChanged(CompoundButton compoundButton, boolean b) {                      flag[position] = b;                  }              });          }  

扩展:如果是从外界要来操作CheckBox的选择状态也很简单,我们已经把CheckBox的选择状态值都保存到了一个全局数组中,所以外界只要操作该数组的状态值就可以了,然后重新刷新一下数据适配器就可以了。

1 1