【问题标题】:CheckBox Checked or UnChecked When Scrolling Listview in Android在Android中滚动Listview时CheckBox选中或未选中
【发布时间】:2017-09-15 12:54:32
【问题描述】:

我用 Textview 和 Checkbox 创建了一个 Listview。首先,我将所有项目保持为选中默认值。

但是当我取消选中复选框并向下滚动以取消选中列表视图中的其他一些项目时,会选中较旧的项目。请帮我写代码。

这是我的适配器类代码。

@Override
public View getView(final int position, View convertView, ViewGroup arg2) {

try {
    ViewHolder holder = new ViewHolder();

    LayoutInflater inflater = ((Activity) context).getLayoutInflater();

    if (convertView == null) {

        convertView = layout_inflater.inflate(R.layout.dashbordmenu_listadapter, null);
        holder.txtMenutitle = (TextView) convertView.findViewById(R.id.txtPersonList);
        holder.checkAppList = (CheckBox) convertView.findViewById(R.id.checkPersonList);
        convertView.setTag(holder);
        convertView.setTag(R.id.txtPersonList, holder.txtMenutitle);
        convertView.setTag(R.id.checkPersonList, holder.checkAppList);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.checkAppList.setTag(position);
    holder.txtMenutitle.setText(arrayListDashboard1.get(position).getMenuTitle());
    holder.checkAppList.setChecked(arrayListDashboard1.get(position).isSelected());
    holder.checkAppList.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int selectPosition = (int) buttonView.getTag();
            if(buttonView.isChecked())
            {
                selectedAppId = arrayListDashboard1.get(selectPosition).getMenuId();
                Log.d("TEST","selectedAppId = "+selectedAppId);

            } else {
                notSelectedAppId = arrayListDashboard1.get(selectPosition).getMenuId();
                  Log.d("TEST","notSelectedAppId = "+notSelectedAppId);
            }
        }
    });

} catch (Exception e) {
    e.printStackTrace();
}
return convertView;
}
public class ViewHolder {
   TextView txtMenutitle;
   public CheckBox checkAppList;
  }

【问题讨论】:

  • holder.checkAppList.setChecked(arrayListDashboard1.get(position).isSelected()); 这行可能是问题所在。也许尝试将每个视图的状态保存在 ArrayList<Boolean>Map<View, Boolean>
  • 此外,isSelected() 不等于 isChecked()

标签: android listview checkbox


【解决方案1】:

第一种方法:

为您的模型添加一个布尔字段。将其设置为默认 true。如果未选中复选框,则使布尔字段为 false。根据布尔字段更新您的复选框。

第二种方法:

使用 SparseBooleanArray,在您的适配器中初始化 SparseBooleanArray。它将保存位置和布尔值。所以你可以保留你的复选框值。

SparseBooleanArray 文档:

与普通的布尔数组不同,索引中可能存在间隙。 它旨在比使用 HashMap 映射更节省内存 整数到布尔值,因为它避免了自动装箱键和 值及其数据结构不依赖于额外的条目对象 每个映射。

第三种方法:

正如 LunarWatcher 所说,只需初始化布尔值的 ArrayList 并更新您的复选框。

【讨论】:

  • 或者,保持一个布尔数组跟踪复选框
  • 是的,好主意。我会添加它。谢谢@LunarWatcher
【解决方案2】:

在您的模型中,使用一个布尔值来记录复选框是否已选中,并在绑定视图时使用它

【讨论】:

    【解决方案3】:

    RecylerView 在滚动时重复使用视图,因此您必须维护每个列表项的复选框状态。在BindViewHolder 中,您将检查该位置的状态并相应地设置复选框的状态。

    【讨论】:

      【解决方案4】:

      为您的模型类设置一个布尔变量“isCheck”并将其设置为默认 false。根据复选框值更改为 true 或 false,更改此值。 并检查复选框更改事件 isCheck 是真还是假。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-20
        • 1970-01-01
        • 2016-02-28
        • 1970-01-01
        • 2015-08-23
        • 2015-08-20
        • 2012-03-07
        • 2017-04-12
        相关资源
        最近更新 更多