Android - How to change the visibility of a widget of and show it in all items in recyclerView?

+1 vote
asked Sep 13, 2017 by azzam-alsharafi

I have a RecyclerView in my activity and I want to add Multiple selection to the RecyclerView, and this is my Adapter:

public class RingtoneItemAdabter extends RecyclerView.Adapter<RingtoneItemAdabter.ViewHolder> { private Context context; private Activity activity; private List<RingtoneItem> list; private boolean selectionMode = false; private List<RingtoneItem> selectedItems = new ArrayList<>(); public class ViewHolder extends RecyclerView.ViewHolder { public Switch aSwitch; public TextView textView; public View view; public CheckBox checkBox; public ViewHolder(View view) { super(view); this.view = view; aSwitch = (Switch) view.findViewById(; textView = (TextView) view.findViewById(; checkBox = (CheckBox) view.findViewById(; } } public RingtoneItemAdabter(Context context, Activity activity, List<RingtoneItem> list) { this.context = context; this.activity = activity; this.list = list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.recyclerview_item, parent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(final ViewHolder holder, int position) { final RingtoneItem item = list.get(position); holder.checkBox.setVisibility(selectionMode ? View.VISIBLE : View.GONE); holder.textView.setText(item.getTitle()); holder.aSwitch.setChecked(item.isActive()); holder.textView.setTextColor(item.isActive() ? Color.BLACK : Color.GRAY); holder.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { holder.textView.setTextColor(Color.BLACK); item.setActive(true); writeToFile(); } else { holder.textView.setTextColor(Color.GRAY); item.setActive(false); writeToFile(); } } }); holder.view.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { if(selectionMode){ return false; }else { startSelecting(); holder.checkBox.setChecked(true); return true; } } }); holder.view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(selectionMode){ holder.checkBox.setChecked(!holder.checkBox.isChecked()); } } }); holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ selectedItems.add(item); }else { selectedItems.remove(item); } if(selectedItems.size() == 0){ stopSelecting(); } } }); } @Override public int getItemCount() { ... } private void writeToFile() { ... } private void startSelecting(){ selectionMode = true; notifyDataSetChanged(); } private void stopSelecting(){ selectionMode = false; notifyDataSetChanged(); }

and this is the checkbox tag in recyclerview_item:

 <CheckBox android:id="@+id/selection_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:visibility="gone" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" />

so I want the startSelection method to show the checkbox in every item, and the stopSelection method to hide the checkbox in every item.


It's looks like I got a problem, the problem is now when I click and hold on an item the checkboxes appear and the checkbox of the item I long clicked must get checked, and if I unchecked every box, the checkboxed should disappear,this is how the app should work, but the when I tried it, it's didn't work, when I long click an item the checkboxes appear in some cases another checkbox get checked, but in another cases no one of the checkboxes get checked, and when I uncheck every checkbox, the checkboxes stay there. I have an idea about why the checkboxed don't disappear, because for example if I long clicked the fourth item, the checkboxes will appear, but rather than the fourth item which should get checked the first item will get checked, but the fourth item is the one which will added to selectedItems list, so if I unchecked the first box nothing will happen because it's not stored in the list, but if I checked the fourth item, then another fourth item will added to the list, and if I unchecked it, one of the two copies of the fourth item will removed, but one will stay. check the code above.

I know my explain is very bad, and my English isn't very good, but hope you understand :)

1 Answer

0 votes
answered Mar 8, 2018 by sisa

Define a boolean variable for show/hide checkbox state and in onBindViewHolder set visibility of your checkbox related to that variable

boolean showCheckboxes = false;
public void startSelection() { showCheckboxes = true; notifyDataSetChanged();
public void stopSelection() { showCheckboxes = false; notifyDataSetChanged();
public void onBindViewHolder(final ViewHolder holder, int position) { final RingtoneItem item = list.get(position); holder.checkBox.setVisibility(showCheckboxes ? View.VISIBLE: View.GONE); /* ... */
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.