0

I am implementing a custom recyclerview with switch for item selection. I have a "Select All" option at the right corner of App Bar (top bar). I want to allow user to use Select All option and also allow individual item selection in recyclerview.

I don't know how to implement individual selection along with Select All. When an item is deselected after using the select all option and when the list is scrolled the item gets selected automatically as the isSelectedAll flag is set true in onBindViewHolder method in the below code.

******SELECT ALL CLICK LISTENER IN ACTIVITY CLASS******
mBinding.imageViewActionSelect.setOnClickListener(v -> {
    mAdapter.selectAll();
});

******ADAPTER CLASS******
public void selectAll() {
    isSelectedAll = true;
    notifyDataSetChanged();
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    if (isSelectedAll) {
        holder.mSwitchView.setChecked(true);
    }
    else
        holder.mSwitchView.setChecked(false);
}
Durga
  • 73
  • 12
  • 1
    Try adding that flag in the Collection model so you have control over it – Ognian Gloushkov Mar 15 '17 at 09:55
  • Have you added any field to handle individual multiple selection state ? if yes then just make all item field value true and when deselect item then change that field value false. You need to show each item selection based on that field so it resolve your current problem. – Haresh Chhelana Mar 15 '17 at 09:58
  • What's the result with your actual code ? – Cochi Mar 15 '17 at 10:09

2 Answers2

0

First, Add one variable isSelected to your POJO class and then, while you select All option, make that variable true by running your list of items in a loop, and then call adapter.notifyDataSetChanged(). Now when you select or deselect individual items make that isSelected variable true or false according to your needs and dont forget to call notifyDataSetChanged(). You are good to go.

Abhishek Jha
  • 167
  • 4
  • 15
0

You could use this FastAdapter library: https://github.com/mikepenz/FastAdapter . Single select is easy, its already done for you. For multi select, you could just loop through all your items and call mAdapter.toggleSelection(position) . When you need the selected items you could easily call mAdapter.getSelection() . Its that easy, no need to do stuff yourself

Elvis Chidera
  • 201
  • 4
  • 10