How to select all items which listed in recyclerview?

前端 未结 4 1219
后悔当初
后悔当初 2021-01-13 03:39

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

4条回答
  •  醉酒成梦
    2021-01-13 04:14

    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 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);
            }
        }
    }
    

提交回复
热议问题