Re-entry of the Recycler View checkbox confusion and limit the number of optional checkbox

The project will need recycler View and CheckBox, but at the time of check after check slide up and down recycler view checkbox confusion, the next debug and found is because the recycler View slide up and down, when many of your item up or when it's when the drop-down item redrawn, resulting in confusion check choice.

I find on Baidu in the next, I think we can of check.setTag, look at the code:

private HashMap hashMap = new HashMap();
private SparseBooleanArray mCheckStates=new SparseBooleanArray();//用于存放选择的状态

@Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        viewHolder.checkBox.setTag(i);//在最开始适配的时候,将每一个CheckBox设置一个当前的Tag值,这样每个CheckBox都有了一个固定的标识

        setCheck_true(viewHolder,i);
      }


//单选
    private void setCheck_true(final ViewHolder viewHolder, final int i){
        viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int pos= (int) buttonView.getTag();//得到当前CheckBox的Tag值,由于之前保存过,所以不会出现索引错乱
                if (isChecked){
                    //点击时将当前CheckBox的索引值和Boolean存入SparseBooleanArray中
                    if (hashMap.size()<num){
                        mCheckStates.put(pos,true);
                        hashMap.put(pos,pos);
                    }else if (mCheckStates.get(i,false)==false){
                        viewHolder.checkBox.setChecked(false);
                        ToastUtil.showToast((Activity) context,"金币数不足",1500);
                    }
                }else {
                    viewHolder.checkBox.setClickable(true);
                    //否则将 当前CheckBox对象从SparseBooleanArray中移除
                    mCheckStates.delete(pos);
                    hashMap.remove(pos);
                }
            }

        });
        //得到CheckBox的Boolean值后,将当前索引的CheckBox状态改变
        viewHolder.checkBox.setChecked(mCheckStates.get(i,false));
    }

When the upper and lower slides in this way, experience Viewholder redrawn to mCheckStates will check the state of the acquired position;

 

And after that, I feel inadequate to go plus the number of clicks a limit check, whether single or click Select All must be within this number, between whether a check. setChecked (false) useless, because when sliding up and down recycler, item again drawn, but every time item0 clicks when you refresh the next page, the next page is clicked the first one also, I went to a set (int count = 0) each time you click, increment, decrement canceled, but still not count in place new drawing (count = 0) to pull recycler recycler previous position when redrawn, or count increase the number of previous self, pass.

So I hashmap used to store the item clicked position: hash.put (position, position); then when the click is determined (hash.size <num) is then determined (mCheckStates.get (i, false) else if the in = = false),

mCheckStates.get (i, false) == true: it shows that the item of the check is true; mCheckStates.get (i, false) == false: indicates that the item has not been clicked, but it (checkbox) will redraw is shecheck (true), so it is necessary

viewHolder.checkBox.setChecked(false);

Look at the code:

 viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int pos= (int) buttonView.getTag();//得到当前CheckBox的Tag值,由于之前保存过,所以不会出现索引错乱
                if (isChecked){
                    //点击时将当前CheckBox的索引值和Boolean存入SparseBooleanArray中
                    if (hashMap.size()<num){
                        mCheckStates.put(pos,true);
                        hashMap.put(pos,pos);
                    }else if (mCheckStates.get(i,false)==false){
                        viewHolder.checkBox.setChecked(false);
                        ToastUtil.showToast((Activity) context,"金币数不足",1500);
                    }
                }else {
                    viewHolder.checkBox.setClickable(true);
                    //否则将 当前CheckBox对象从SparseBooleanArray中移除
                    mCheckStates.delete(pos);
                    hashMap.remove(pos);
                }
            }

        });
        //得到CheckBox的Boolean值后,将当前索引的CheckBox状态改变
        viewHolder.checkBox.setChecked(mCheckStates.get(i,false));

 

Published 19 original articles · won praise 3 · views 10000 +

Guess you like

Origin blog.csdn.net/xyzahaha/article/details/85336349