只有Android的recyclerview选择单个项目

问题描述 投票:-2回答:1

我实现与下面的代码的单一选择recyclerView-Selection API:

selectionTracker = SelectionTracker.Builder<Long>(SELECTION_ID,recyclerView,
Adapter.MessagesItemKeyProvider(1),
Adapter.MessagesItemLookup(messagesRecyclerView),
StorageStrategy.createLongStorage()).withSelectionPredicate(SelectionPredicates.createSelectSingleAnything()).build()`

现在,我的recyclerView只是单个项目可以选择。

当选择另一个项目,而不是替换我只需要清除当前选择中的选择?

android android-recyclerview selection
1个回答
0
投票

为了让单一的选择,你只需要提供一个SelectionPredicate到您的SelectionTracker.Builder。

final SelectionTracker<Long> tracker = new SelectionTracker.Builder<>(
    "my-selection-id",
    recyclerView,
    provider,
    detailLookup,
    StorageStrategy.createLongStorage())
    .withSelectionPredicate(new SelectionTracker.SelectionPredicate<Long>() {
        @Override
        public boolean canSetStateForKey(@NonNull Long key, boolean nextState) {
            return true;
        }

        @Override
        public boolean canSetStateAtPosition(int position, boolean nextState) {
            return true;
        }

        @Override
        public boolean canSelectMultiple() {
            return false; // Set to false to allow single selecting
        }
    })
    .build();
© www.soinside.com 2019 - 2024. All rights reserved.