even if it's just for samsung devices
You can implement this feature on any device. I first found this feature in Mi devices.
It is somewhat complicated. But you can use libraries for this purpose like DragSelectRecyclerView.
Add following code to your build.gradle:
dependencies {
implementation 'com.github.MFlisar:DragSelectRecyclerView:0.3'
}
In your java file, add following code:
Create a touch listener.
mDragSelectTouchListener = new DragSelectTouchListener()
.withSelectListener(onDragSelectionListener)
// following is all optional
.withMaxScrollDistance(distance) // default: 16; defines the speed of the auto scrolling
.withTopOffset(toolbarHeight) // default: 0; set an offset for the touch region on top of the RecyclerView
.withBottomOffset(toolbarHeight) // default: 0; set an offset for the touch region on bottom of the RecyclerView
.withScrollAboveTopRegion(enabled) // default: true; enable auto scrolling, even if the finger is moved above the top region
.withScrollBelowTopRegion(enabled) // default: true; enable auto scrolling, even if the finger is moved below the top region
.withDebug(enabled);
Attach this touch listener to the RecyclerView
.
recyclerView.addOnItemTouchListener(mDragSelectTouchListener);
On item long press, inform the listener to start the drag selection.
mDragSelectTouchListener.startDragSelection(position);
Use the DragSelectionProcessor
, it implements the above mentioned interface and can be set up with 4 modes:
Simple
: simply selects each item you go by and unselects on move back
ToggleAndUndo
: toggles each items original state, reverts to the original state on move back
FirstItemDependent
: toggles the first item and applies the same state to each item you go by and applies inverted state on move back
FirstItemDependentToggleAndUndo
: toggles the item and applies the same state to each item you go by and reverts to the original state on move back
Provide a ISelectionHandler
in it's constructor and implement these functions:
onDragSelectionListener = new DragSelectionProcessor(new DragSelectionProcessor.ISelectionHandler() {
@Override
public Set<Integer> getSelection() {
// return a set of all currently selected indizes
return selection;
}
@Override
public boolean isSelected(int index) {
// return the current selection state of the index
return selected;
}
@Override
public void updateSelection(int start, int end, boolean isSelected, boolean calledFromOnStart) {
// update your selection
// range is inclusive start/end positions
// and the processor has already converted all events according to it'smode
}
})
// pass in one of the 4 modes, simple mode is selected by default otherwise
.withMode(DragSelectionProcessor.Mode.FirstItemDependentToggleAndUndo);
You can see a demo activity here.
Is there any other library for the same purpose?
Yes, you can also use Drag Select Recycler View by afollestad. And you can see practical implementation for afollestad's Drag select Recycler View here.