Select RecyclerView 中的所有复选框

Select all checkboxes in RecyclerView

如何 select recyclerView?

中的所有复选框

我试着这样做:

在适配器中:

   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);}

layout Activity 我有一个 buttononClickListener:

 private void onClickSelectAll(View view) {

        getSelectPersonsAdapter().selectAll();
    }

你在这里犯了一个简单的错误

  if (!isSelectedAll) holder.select_item.setChecked(false);
    else holder.select_item.setChecked(true);

setSelected改成setChecked就是这样

Select全部全部取消选择

活动

 checkall.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(checkall.isChecked()){
                categoryAdapter.selectAll();
            }
            else {
                categoryAdapter.unselectall();
            }


        }
    });

在适配器中

  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);