How to deselect the select all checkbox when i am deselecting even 1 checkbox in android

Mathur

New Member
#1
I want functionality in my project where on a click of select all checkbox all the checkbox in the list must get selected and on deselect of checkbox all the other checkbox must get deselected. I have achieved this functionality by doing this:
Mã:
/*in my main activity i have done this*/

  cbSelectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                for (int i = 0; i < resumeSearchList.size(); i++) {
                    resumeSearchList.get(i).setSelected(cbSelectAll.isChecked());
                }
                cvSearchResultListAdapter.notifyDataSetChanged();
            }
        });
    }
and in adapter class I have done this..
Mã:
holder.cbSelect.setChecked(dataList.get(position).isSelected());
Now my problem is whenever I will unchecked any checkbox in the list the select all button must get unchecked. Please help

thanks in advance
 

Admin

Administrator
Thành viên BQT
#2
You can set OnCheckedChangeListener on holder.cbSelect. in your adapter.
Mã:
holder.cbSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    dataList.get(holder.getAdapterPosition()).setSelected(isChecked);
                    // here you can call listener to get notified
            }
        });
    }
You can have listener to get notified in your Activity.

For adding listener,

1. Create an interface:

Mã:
public interface OnMyCheckBoxCheckedChangeListener {
    void onMyCheckBoxCheckedChange();
}
2. Add parameter in your constructor of Adapter:
Mã:
private OnMyCheckBoxCheckedChangeListener mOnMyCheckBoxCheckedChangeListener;

    public YourAdapter(..., ..., OnMyCheckBoxCheckedChangeListener onMyCheckBoxCheckedChangeListener) {
            mOnMyCheckBoxCheckedChangeListener = onMyCheckBoxCheckedChangeListener;
        }
3. Call interface where you want to notify:
Mã:
holder.cbSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    dataList.get(holder.getAdapterPosition()).setSelected(isChecked);
                    mOnMyCheckBoxCheckedChangeListener.onMyCheckBoxCheckedChange();
            }
        });
    }
4. Set adapter from your activity:
Mã:
yourRecyclerView.setAdapter(new YourAdapter(..., ..., new OnMyCheckBoxCheckedChangeListener() {
    @Override
    public void onMyCheckBoxCheckedChange() {
        // you will get notified here.
    }
}));
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top