RecyclerView复用的一些问题(Checkbox错乱)
2017-12-22 13:12
1216 查看
在 RecyclerView 中使用 CheckBox 时,滚动会导致CheckBox选中错乱而文字不会错乱?
因为文字是从数据源中拿出来的,而CheckBox的选中与否是没有数据源的,RecyclerView复用此item时,它会展示复用的item的所有属性,并根据数据源替换数据,既文字可以替换,而CheckBox继续沿用复用item的状态。
因为文字是从数据源中拿出来的,而CheckBox的选中与否是没有数据源的,RecyclerView复用此item时,它会展示复用的item的所有属性,并根据数据源替换数据,既文字可以替换,而CheckBox继续沿用复用item的状态。
1.创建一个全局集合(保存checkbox状态的)
// 先给其默认值为false private List<Boolean> booleans = new ArrayList<>();
2.onBindViewHolder()
@Override public void onBindViewHolder(ViewHolder holder, final int position) { CheckBox checkBox = holder.getView(R.id.rv_check_cb); // 先设置一次CheckBox的选中监听器,传入参数null,防止多次监听错乱 checkBox.setOnCheckedChangeListener(null); // 用集合中的值设置CheckBox的选中状态 checkBox.setChecked(booleans.get(position)); // 上面两步恢复了checkbox的状态,然后进行监听并更新checkbox状态 checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { booleans.set(position,b); } }); }
相关文章推荐
- RecyclerView中CheckBox复用以及选中消失的问题
- RecyclerView解决CheckBox复用问题
- recyclerView列表使用Timer定时器复用错乱的问题
- 解决RecyclerView加载图片时复用itemview导致的图片数量错乱问题
- RecyclerView与CheckBox结合使用错乱问题
- Android中Recyclerview使用9----Recyclerview的条目复用,出现CheckBox或者设置背景混乱问题
- 解决RecyclerView复用,导致Item混乱问题,checkbox也类似此做法
- Android中RecyclerView在TV中处理控件焦点移动,EditText值的修改,CheckBox复用等问题解决
- RecyclerView 复用出现 CheckBox 或者 设置背景混乱问题
- RecyclerView与CheckBox结合使用错乱问题
- RecyclerView+checkbox滑动导致复用混乱问题的解决方案
- 解决RecyclerView复用导致EdittText、CheckBox等显示异常的问题
- glmis项目工作日志4_解决RecyclerView中使用CheckBox时出现数据错乱的问题。
- Recyclerview中checkbox的复用问题
- 解决RecyclerView删除Item导致位置错乱的问题 以及删除最后一个出错的问题
- 关于使用RecyclerView遇到的一些问题集合
- Recyclerview 删除/插入item时,导致position错乱问题
- RecyclerView的Item复用出现问题导致排版混乱
- RecyclerView嵌套GridView显示不全,Item的复用的问题
- Android RecycleView 实现单选列表 并解决View复用导致错乱问题