我实现与下面的代码的单一选择recyclerView-Selection
API:
selectionTracker = SelectionTracker.Builder<Long>(SELECTION_ID,recyclerView,
Adapter.MessagesItemKeyProvider(1),
Adapter.MessagesItemLookup(messagesRecyclerView),
StorageStrategy.createLongStorage()).withSelectionPredicate(SelectionPredicates.createSelectSingleAnything()).build()`
现在,我的recyclerView
只是单个项目可以选择。
当选择另一个项目,而不是替换我只需要清除当前选择中的选择?
为了让单一的选择,你只需要提供一个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();