I have list of items inside of recyclerview, and they are multiple selectable.
I want to have select button to select all, and if selected deselect all. I didn\'t se
Maintaining a separate list or flag doesn't work for me, as the contents of my recycler view can change at any given moment, and calling notifyDataSetChanged()
can be costly for large data sets. The solution I came up with doesn't use either, instead iterating over the recycler views children. For example, inside whatever function responds to your "select all action", iterate the child views like so:
RecyclerView rv = ...;
List<View> selection = ...;
...
public void selectAll() {
int count = rv.getChildCount();
for (int i = 0; i < count; i++) {
View view = rv.getChildAt(i);
if (!view.isActivated()) {
view.setActivated(true);
selection.add(view);
}
}
}
In my case,
Step - 1 :- first globally initialize flag in activity class
boolean flagSelectAll = false;
Step - 2 :- Then set below code in Onclick of Button
@OnClick(R.id.btnSelectAll)
public void setBtnSelectAll(View view){
for (int i=0;i<dMyItemLists.size();i++){
if (dMyItemLists.get(i).isSelected()){
flagSelectAll = true;
dMyItemListAdapter.selectAllItem(false);
dMyItemListAdapter.notifyDataSetChanged();
}
else {
dMyItemListAdapter.selectAllItem(true);
dMyItemListAdapter.notifyDataSetChanged();
}
break;
}
}
Step - 3 :- Then create method selectAllItem in recyclerview's adpater class like below :
public void selectAllItem(boolean isSelectedAll) {
try {
if (itemList != null) {
for (int index = 0; index < itemList.size(); index++) {
itemList.get(index).setSelected(isSelectedAll);
}
}
notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
Try to maintain Selected item list and list of items in Adapter,
When you select "Select All" button, just add all item in selected item list and call notifyDataSetChanged
Just a sudo code
class adapter {
ArrayList<Item> selected = new ArrayList<Item>();
ArrayList<Item> items = new ArrayList<Item>();
public void selecteAll() {
selected.clear();
selected.addAll(items);
notifyDataSetChanged();
}
public void clearAll() {
selected.clear();
notifyDataSetChanged();
}
public void bindView() {
Item item = items.get(position);
if(selected.contains(item) {
// Do selected action
} else {
// Non selecetd ctions
}
}
}
There is no need to maintain another list with selected items. You need to create a method to set a flag and in the onBindViewHolder method just check for that flag. Something like this:
class MyAdapter(val context: Context): RecyclerView.Adapter<MyAdapter.ViewHolder>() {
val mItems = mutableListOf<ItemObject>()
private var selectAllItems: Boolean = false
fun updateList(list: List<ItemObject>) {
mItems.addAll(list)
notifyDataSetChanged()
}
fun selectAllItems(selectAll: Boolean){
selectAllItems = selectAll
notifyDataSetChanged()
}
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val pos = viewHolder.adapterPosition
viewHolder.checkbox.isSelected = selectAllItems
}
class ViewHolder(val view: View): RecyclerView.ViewHolder(view) {
val checkbox: Checkbox = view.myCheckBox
}
}