How To Select All Items Which Listed In Recyclerview?
Solution 1:
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
classadapter {
ArrayList<Item> selected = newArrayList<Item>();
ArrayList<Item> items = newArrayList<Item>();
publicvoidselecteAll() {
selected.clear();
selected.addAll(items);
notifyDataSetChanged();
}
publicvoidclearAll() {
selected.clear();
notifyDataSetChanged();
}
publicvoidbindView() {
Item item = items.get(position);
if(selected.contains(item) {
// Do selected action
} else {
// Non selecetd ctions
}
}
}
Solution 2:
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)publicvoidsetBtnSelectAll(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();
}
}
Solution 3:
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:
classMyAdapter(val context: Context): RecyclerView.Adapter<MyAdapter.ViewHolder>() {
val mItems = mutableListOf<ItemObject>()
privatevar selectAllItems: Boolean = falsefunupdateList(list: List<ItemObject>) {
mItems.addAll(list)
notifyDataSetChanged()
}
funselectAllItems(selectAll: Boolean){
selectAllItems = selectAll
notifyDataSetChanged()
}
overridefunonBindViewHolder(viewHolder: ViewHolder, position: Int) {
val pos = viewHolder.adapterPosition
viewHolder.checkbox.isSelected = selectAllItems
}
classViewHolder(val view: View): RecyclerView.ViewHolder(view) {
val checkbox: Checkbox = view.myCheckBox
}
}
Solution 4:
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 = ...;
...
publicvoidselectAll() {
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);
}
}
}
Post a Comment for "How To Select All Items Which Listed In Recyclerview?"