RadioButton
RadioGroup
CheckBox

If there are dozens of files, is it very time-consuming? If there are dozens of files, is it very time-consuming? The experience is not as good as it would be on a PC, where you can just drag the mouse and select multiple files that you want to select. Can the APP be dragged like a PC to achieve multiple selection? The answer is yes, today we introduce a cool library, drag-select-recyclerView, can easily achieve recyclerView drag multiple selection.

drag-select-recyclerview

Github address: github.com/afollestad/…

If you’ve ever used Google Albums, you’ll remember that it has a very handy feature that allows you to select multiple photos by dragging your finger across the screen. Drag-select-recyclerview makes it easy to use in your own app.

DragSelectTouchListener is the core class of this library. This library will handle drag event interception and automatic scrolling logic. When dragging to the top of recyclerView, lists will continue to scroll and vice versa.

When used, DragSelectTouchListener attache to RecyclerView, which handles the interception of touch events and then returns the results and updates the UI via a receiver.

val receiver: DragSelectReceiver = // ... val touchListener = DragSelectTouchListener.create(context, receiver)Copy the code

rendering

How to use it?

Add dependencies:

dependencies {

  implementation 'com. Afollestad: drag - select - recyclerview: 2.4.0'
}
Copy the code

The receiver code is as follows:

class MyReceiver : DragSelectReceiver {

  override fun setSelected(index: Int, selected: Boolean) {
    // do something to mark this index as selected/unselected
    if(selected && ! selectedIndices.contains(index)) { selectedIndices.add(index) }else if(! selected) { selectedIndices.remove(index) } } override fun isSelected(index: Int): Boolean { //return true if this index is currently selected
    return selectedItems.contains(index)
  }
  
  override fun isIndexSelectable(index: Int): Boolean {
    // if you return false, this index can't be used with setIsActive() return true } override fun getItemCount(): Int { // return size of your data set return 0 } }Copy the code

DragSelectReceiver is an interface. In practice, we can have Adapter implement DragSelectReceiver so that it is easy to record checked and unchecked.

Combined with RecyclerView, the use is as follows:

val recyclerView: RecyclerView = // ...
val receiver: DragSelectReceiver = // ...

val touchListener = DragSelectTouchListener.create(context, receiver)
recyclerView.addOnItemTouchListener(touchListener) // important!!

// true for active = true, 0 is the initial selected index
touchListener.setIsActive(true, 0)
Copy the code

By setting setIsActive, when the user holds down a list Item, the event is triggered to start dragging the selection.

For more information, see Github or run Demo.

Isn’t that cool? Introduce your own project and give it a try!