Search code examples
androidandroid-recyclerviewandroid-viewholderandroid-checkbox

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


I have a button and a recycler view in an activity. Inside each row of the recycler view (viewholder in adapter class), I have an invisible checkbox. What I want to happen is when the user clicks the button, the checkboxes in each row should become visible. How can I achieve this?


Solution

  • Try this example, Like the Jyotish Biswas explained I have done it with an example, I thought this explanation will clear your doubts.

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

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

    Try this and let me know if you have any issues.