How to disable drag to selection using SelctionTracker in Recyclerview?

Viewed 156

I am using SelectionTracker.Builder to multi select items in the RecyclerView. But after selecting first item, SelectionTracker selects other items on dragging too. I want to disable drag to select feature and keep only Click to select multiple items. Is there any way to it?

.withSelectionPredicate(new SelectionTracker.SelectionPredicate<String>() {
                @Override
                public boolean canSetStateForKey(@NonNull String key, boolean nextState) {
                    return true;
                }

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

                @Override
                public boolean canSelectMultiple() {
                    return true;
                }   
            }).build();

I tried SelectionPredicate but this also limited the number of items that can be selected to 1.

1 Answers

There is supposedly the BandSelectionPredicate, but it doesn't seem to work.

.withBandPredicate(object : BandPredicate() {
  override fun canInitiate(e: MotionEvent): Boolean = false
})

So my workaround is to check the state of OperationsMonitor.isStarted inside a custom SelectionPredicate

val operationMonitor = OperationMonitor()
.withOperationMonitor(operationMonitor)
.withSelectionPredicate(object :
  SelectionTracker.SelectionPredicate<String>() {
    override fun canSelectMultiple(): Boolean = true

    override fun canSetStateAtPosition(position: Int, nextState: Boolean): Boolean {
      if (operationMonitor.isStarted) return false
      
      // rest of logic, then eventually
      return true
    }

    override fun canSetStateForKey(key: String, nextState:Boolean): Boolean {
       if (operationMonitor.isStarted) return false
       
       // rest of logic, then eventually
       return true
    }
})
Related