How to select all items which listed in recyclerview?

前端 未结 4 1205
后悔当初
后悔当初 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:31

    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 selected = new ArrayList();
        ArrayList items = new ArrayList();
    
        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
            }
        }
    
    }
    

提交回复
热议问题