文章标题

来源:互联网 发布:李荣浩后羿知乎 编辑:程序博客网 时间:2024/04/30 21:21

RecyclerView 和CheckBox错乱的解决方案

主要思路是利用setTag()方法将所有和用户交互的CheckBox状态储存在hashmap中,然后在onBindView()方法中利用tag来过去相应的状态并设置,这样就可以避免因为Recyclerview的复用而造成的checkbox错乱的问题.代码如下

@Overridepublic void onBindViewHolder(final MyViewHolder holder, final int position) {        holder.checkBox.setTag(position);        if (checkmap.get(position)==null){            holder.checkBox.setChecked(false);        }else {             holder.checkBox.setChecked(checkmap.get((Integer)holder.checkBox.getTag()));        }        Log.e("tag",position+"#########  ");        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {                Log.e("tag",position+"******  "+b);                checkmap.put((Integer) holder.checkBox.getTag(),b);            }        });    }
0 0
原创粉丝点击