如何在 activity 单击按钮时使复选框在 RecyclerView 中可见?

How to make checkboxes visible in RecyclerView on a button click from activity?

我在 activity 中有一个按钮和一个回收站视图。在回收站视图的每一行内(适配器 class 中的查看器),我有一个不可见的复选框。我想要发生的是当用户单击按钮时,每行中的复选框应该可见。我怎样才能做到这一点?

使用布尔标志来做 在按钮上单击设置该标志为真 并为该适配器设置 notifyDataSetChanged() 在适配器中 onBind 检查该标志,如果标志为真,则为复选框设置可见性 VISIBLE 否则 GONE

您可以在 Viewholder 中这样做:

整个取景器:

holder.mycheckbox.setVisibility(View.GONE);

试试这个例子,就像 Jyotish Biswas 解释的那样我已经用一个例子做了,我认为这个解释会消除你的疑虑。

ArrayList<Model> mModelList;
TestRecyclerViewAdapter mTestRecyclerViewAdapter;

RecyclerView mRecyclerView;

private String dummy_groups[] = {

        "Group 1",
        "Group 2",
        "Group 3",
        "Group 4",
        "Group 5",
        "Group 6",
        "Group 7",
        "Group 8"


};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    checkAllBoxes = findViewById(R.id.checkAllBoxes);


    mRecyclerView = findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);

    ArrayList<Model> recordsDataLists = prepareData();
    mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    mTestRecyclerViewAdapter = new TestRecyclerViewAdapter(recordsDataLists);
    mRecyclerView.setAdapter(mTestRecyclerViewAdapter);

    mModelList = prepareData();
    mTestRecyclerViewAdapter.updateData(mModelList);

    checkAllBoxes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            for (int i = 0; i < mTestRecyclerViewAdapter.getItemCount(); i++)
            {
                mModelList.get(i).setChecked(true);
                mTestRecyclerViewAdapter.notifyDataSetChanged();
            }
        }
    });
}

private ArrayList<Model> prepareData() {
    ArrayList<Model> theImage = new ArrayList<>();
    for (int i = 0; i < dummy_groups.length ; i++){
        Model createList = new Model();
        createList.setName(dummy_groups[i]);
        //createList.setChecked(true);
        theImage.add(createList);
    }
    return theImage;
}

适配器class:

public class TestRecyclerViewAdapter extends 
RecyclerView.Adapter<TestRecyclerViewAdapter.TestRecyclerViewHolder> {

ArrayList<Model> mModel;

public TestRecyclerViewAdapter(ArrayList<Model> model) {
    mModel = model;
}

public void updateData(ArrayList<Model> modelArrayList){
    mModel.clear();
    mModel.addAll(modelArrayList);
    notifyDataSetChanged();
}

@NonNull
@Override
public TestRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup 
 parent, int viewType) {
    View view = 





LayoutInflater.from(parent.getContext()).inflate(R.layout.recylerview_layout,parent,false);
        return new TestRecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TestRecyclerViewHolder holder, int position) {

        Model model = mModel.get(position);

        if (model.isChecked())
        {
            holder.checkBoxes.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public int getItemCount() {
        return mModel.size();
    }

    public class TestRecyclerViewHolder extends RecyclerView.ViewHolder{

        CheckBox checkBoxes;

        public TestRecyclerViewHolder(@NonNull View itemView) {
            super(itemView);

            checkBoxes = itemView.findViewById(R.id.checkBoxes);
        }
    }
}

recylcerview.layout.xml

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:weightSum="10"
    android:orientation="horizontal"
    android:layout_height="50dp">


    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="8"
        android:text="@string/app_name"
        android:layout_gravity="center"
        android:textSize="25sp"
        android:gravity="center"
        android:textStyle="bold"/>


    <CheckBox
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:layout_gravity="center"
        android:gravity="center"
        android:id="@+id/checkBoxes"
        android:visibility="gone"/>


</LinearLayout>

Model.java

public class Model {

    public boolean isChecked;

    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }
}

试试这个,如果您有任何问题,请告诉我。