复选框在RecyclerView中变得“粘滞" [英] Checkbox becoming 'sticky' in RecyclerView

查看:80
本文介绍了复选框在RecyclerView中变得“粘滞"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对话框,该对话框通过带有复选框的列表来接受用户输入,这些复选框的布局为RecyclerView.但是,当我在列表中选择一个CheckBox时,也会检查列表中更下方的另一个CheckBox,但是我没有这样做.这些图像将有助于说明我的观点.

I have a dialog which takes in user input through a list with checkboxes, whose layout is a RecyclerView. But when I select a CheckBox in the list, another CheckBox further down in the list also gets checked, but which I didn't do. These images will help illustrate my point.

在这里,我只选择了日历和照相机":

Here, I've only selected Calendar and Camera:

但在列表的更下方,还选择了Google和Google地图,而我没有选择.

but further down in the list, Google and Maps also get selected which I didn't select.

我对bindActivity的代码是:

public void bindActivity(ResolveInfo resolveInfo)
        {
            mResolveInfo = resolveInfo;
            PackageManager pm = getActivity().getPackageManager();
            String appName = mResolveInfo.loadLabel(pm).toString();
            mAppImageView.setImageDrawable(resolveInfo.loadIcon(pm));
            mAppTextView.setText(appName);
        }

如果我在bindActivity中添加mAppCheckBox.setChecked(false),则当我在列表中走得更远并且RecyclerView回收"列表,然后又上升时,我之前的选择将变为未选择状态.

If I add mAppCheckBox.setChecked(false) in bindActivity, then when i go further down in the list and the RecyclerView 'recycles' the list, and then I go up, my earlier selection becomes unselected.

关于如何摆脱粘滞"复选框的任何建议,我都希望得到.

I would love any suggestions on how to get rid of the 'sticky' checkbox.

推荐答案

您看到的是这种行为,因为ViewHolder正在被重用.向下滚动后,上方项目的ViewHolder(现在不可见)将被重新用于将要显示的新项目.该ViewHolderCheckBox先前已被检查,没有人明确表示应取消选中它.

You see that behavior, because ViewHolders are being reused. Once you scroll down, the ViewHolder of the upper items (that are not visible now) will be reused for the new items that are going to be displayed. This ViewHolder had a CheckBox that was checked previously and nobody had explicitly said, that it should be unchecked.

您应该注意正确重置状态.您可以将选中项的位置保存在数组中,然后在onBindViewHolder()中适当地设置CheckBox的选定状态:

You should take care of resetting the state correctly. You can save checked items positions in an array and later in onBindViewHolder() set the selected state of CheckBox appropriately:

public class MyAdapter extends RecyclerView.Adapter<Item> {

  final boolean[] checkedArr;

  public MyAdapter(List<Item> list) {
    checkedArr = new boolean[list.size()];
    // Filling all the items as unchecked by default
    Arrays.fill(checkedArr, false);
  }

  @Override public void onBindViewHolder(Item holder, int position) {
    // You have removed the listener in `onViewRecycled`
    // Thus, this `setChecked()` won't cause any listener to be fired
    holder.checkbox.setChecked(checkedArr[holder.getAdapterPosition()]);
    holder.checkbox.setOnCheckedChangeListener(
        (buttonView, isChecked) -> {
          // Retaining checked state in the array
          checkedArr[holder.getAdapterPosition()] = isChecked;
        });
  }

  @Override public void onViewRecycled(Item holder) {
    super.onViewRecycled(holder);
    // When view is being recycled remove the listener
    holder.checkbox.setOnCheckedChangeListener(null);
  }
  ...
}

或者您可以避免重塑车轮" 并使用 MultiViewAdapter 附带复选框功能:单个,单个或无,多种模式.

Or you can refrain from "reinventing a wheel" and use MultiViewAdapter that is shipping with checkboxes feature: single, single or none, multiple modes.

这篇关于复选框在RecyclerView中变得“粘滞"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆