选择RecyclerView中的所有复选框

时间:2016-09-05 10:53:49

标签: android android-recyclerview android-checkbox

如何选择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我有button onClickListener

 private void onClickSelectAll(View view) {

        getSelectPersonsAdapter().selectAll();
    }

2 个答案:

答案 0 :(得分:14)

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

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

setSelected更改为setChecked,以便

答案 1 :(得分:0)

同时全选取消全选

活动

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