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?
Asked
Active
Viewed 1,044 times
-1
-
please share some code from activity and adapter. – aminography Jul 21 '20 at 13:46
-
Please, put your code to see the problem in the implementation. But the solution could be the next, the adapter receive a data model with the attribute isCheckBoxVisible = false. When you press your activity button, then pass a new data model to your adapter with the attribute to true to show the checkbox that you need. If you work with presenter / vm, then this ui logic is moved to that layer. – Manuel Mato Jul 21 '20 at 13:47
-
Do you want to show it for example with full code? @Evian – Brahma Datta Jul 21 '20 at 14:58
3 Answers
1
Use a boolean flag to do it
on button click set that flag true
and set notifyDataSetChanged()
for that adapter
And in Adapter onBind
chek that flag if flag true then set visibility VISIBLE
for Checkbox
else GONE

Jyotish Biswas
- 674
- 5
- 11
1
you can do it in Viewholder like that :
Entire viewholder:
holder.mycheckbox.setVisibility(View.GONE);

youness makhfi
- 21
- 4
1
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.

Ruben Helsloot
- 12,582
- 6
- 26
- 49

Brahma Datta
- 1,102
- 1
- 12
- 20
-
Hi thanks for the answer. Sorry for getting back so late. Yeah I managed to do it and it's pretty much the same way you explained in your answer so I'll just accept yours. – Evian Pringle Jul 31 '20 at 13:54
-
Thank you Very much @EvianPringle. Hope your question reaches others as well. – Brahma Datta Jul 31 '20 at 13:58