Checkbox auto call onCheckedChange when listview scroll?

前端 未结 5 941
终归单人心
终归单人心 2020-12-02 14:26

I have a problem with listview which list item contain a checkbox. When i check a box and scroll list, checkbox sometime auto call oncheckedchange and value of checkbox is c

5条回答
  •  忘掉有多难
    2020-12-02 14:58

    holder.cBox.setOnCheckedChangeListener(null);
    holder.cBox.setChecked(list.get(position).isChecked());
    holder.tvName.setText(item.getName());
    
    holder.cBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if (isChecked)
            {
                list.get(position).setChecked(true);
            }
            else
            {
                list.get(position).setChecked(false);
            }
        }
    });
    

    In the list, the item have an attribute to set whether the item is checked or not. You can use this to set you item whether checked, and first you should set the

    cBox.setOnCheckedChangeListener(null);
    cBox.setChecked(list.get(position).isChecked());
    

    Then set the real new OnCheckedChangeListener()

    I hope my answer is useful for you and those who look at this page or have trouble dealing with listviews that have item checkboxes.

提交回复
热议问题