如何选择recyclerview中列出的所有项目? [英] How to select all items which listed in recyclerview?
本文介绍了如何选择recyclerview中列出的所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在recyclerview中有项目列表,并且它们是可以选择的.
I have list of items inside of recyclerview, and they are multiple selectable.
我希望有一个选择按钮来全选,如果选择则取消全选.我没有看到通过RecyclerView.Adapter进行迭代的任何选项.我该如何实施?
I want to have select button to select all, and if selected deselect all. I didn't see any option to iterate through RecyclerView.Adapter to do that. How can I implement that?
谢谢.
推荐答案
尝试维护选定的项目列表和适配器中的项目列表,
Try to maintain Selected item list and list of items in Adapter,
选择全选"按钮时,只需将所有项目添加到所选项目列表中,然后调用notifyDataSetChanged
When you select "Select All" button, just add all item in selected item list and call notifyDataSetChanged
只需一个sudo代码
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
}
}
}
这篇关于如何选择recyclerview中列出的所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文