我使用以下代码来使用recyclerview-selection:

selectionTracker = new SelectionTracker.Builder<>("lazy-load-img-list-selection", this.recyclerView, new KeyProvider(1), new Lookup(), StorageStrategy.createLongStorage()).build();


我预计

selectionTracker.setOnSelectionEnter
selectionTracker.setOnSelectionExit


切换按钮“删除选择”“清除选择”可见性

怎么做?

更新:

我可以检测选择状态回收器适配器#onBindViewHolder,但这不是一个好方法,当确定onSelectionExit时,我必须检测所有项目的状态,每个状态都会渲染每个项目

最佳答案

我找到解决方案:

  selectionTracker.addObserver(new SelectionTracker.SelectionObserver() {
        @Override
        public void onItemStateChanged(@NonNull Object key, boolean selected) {
            super.onItemStateChanged(key, selected);
            Selection selection = selectionTracker.getSelection();
            if (selected && selection.size() == 1) {
                if (onSelectionEnter != null) {
                    onSelectionEnter.run();
                }
            } else if (!selected && selection.size() == 0) {
                if (onSelectionExit != null) {
                    onSelectionExit.run();
                }
            }
        }
    });

08-07 19:04