How can I select all checkboxes in recyclerView?
I try to do it like this:
in Adapter:
public void selectAll(){
Log.e("onClickSelectAll","yes");
isSelectedAll=true;
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
PersonDTO item = dataSet.get(position);
holder.tvName.setText(item.getName());
if (!isSelectedAll) holder.cbSelect.setSelected(false);
else holder.cbSelect.setSelected(true);}
In layout
Activity
I have a button
with onClickListener
:
private void onClickSelectAll(View view) {
getSelectPersonsAdapter().selectAll();
}
You made a simple mistake here
if (!isSelectedAll) holder.select_item.setChecked(false);
else holder.select_item.setChecked(true);
change setSelected
into setChecked
thats it
for both Select All and Unselect All
activty
checkall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkall.isChecked()){
categoryAdapter.selectAll();
}
else {
categoryAdapter.unselectall();
}
}
});
in adapter
public void selectAll(){
isSelectedAll=true;
notifyDataSetChanged();
}
public void unselectall(){
isSelectedAll=false;
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(@NonNull final MyviewHolder holder, int position) {
final CategoryListItems list = categoryListItems.get(position);
holder.text.setText(list.getName());
if (!isSelectedAll){
holder.checkbox.setChecked(false);
}
else holder.checkbox.setChecked(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With