Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all checkboxes in RecyclerView

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();
    }
like image 890
no news Avatar asked Sep 05 '16 10:09

no news


Video Answer


2 Answers

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

like image 93
AMAN SINGH Avatar answered Oct 05 '22 07:10

AMAN SINGH


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);
like image 33
Manju S B Avatar answered Oct 05 '22 05:10

Manju S B