• This repository has been archived on 17/May/2023
  • Stars
    star
    1,969
  • Rank 23,549 (Top 0.5 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created about 9 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ‘‡ Easy Google Photos style multi-selection for RecyclerViews, powered by Kotlin and AndroidX.

Drag Select Recycler View

Maven Central Android CI License

This library allows you to implement Google Photos style multi-selection in your apps! You start by long pressing an item in your list, then you drag your finger without letting go to select more.

Range Mode GIF

Sample

You can download a sample APK.


Gradle Dependency

The Gradle dependency is available via jCenter. jCenter is the default Maven repository used by Android Studio.

Dependency

Add the following to your module's build.gradle file:

dependencies {

  implementation 'com.afollestad:drag-select-recyclerview:2.4.0'
}

Introduction

DragSelectTouchListener is the main class of this library.

This library will handle drag interception and auto scroll logic - if you drag to the top of the RecyclerView, the list will scroll up, and vice versa.


DragSelectTouchListener

Basics

DragSelectTouchListener attaches to your RecyclerViews. It intercepts touch events when it's active, and reports to a receiver which handles updating UI

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

Configuration

There are a few things that you can configure, mainly around auto scroll.

DragSelectTouchListener.create(context, adapter) {
  // Configure the auto-scroll hotspot
  hotspotHeight = resources.getDimensionPixelSize(R.dimen.default_56dp)
  hotspotOffsetTop = 0 // default
  hotspotOffsetBottom = 0 // default
  
  // Listen for auto scroll start/end
  autoScrollListener = { isScrolling -> } 

  // Or instead of the above...
  disableAutoScroll()
  
  // The drag selection mode, RANGE is the default
  mode = RANGE
}

The auto-scroll hotspot is a invisible section at the top and bottom of your RecyclerView, when your finger is in one of those sections, auto scroll is triggered and the list will move up or down until you lift your finger.

If you use PATH as the mode instead of RANGE, the behavior is a bit different:

Path Mode GIF

Compare it to the GIF at the top.


Interaction

A receiver looks like this:

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
  }
}

In the sample project, our adapter is also our receiver.

To start drag selection, you use setIsActive, which should be triggered from user input such as a long press on a list item.

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)

More Repositories

1

material-dialogs

๐Ÿ˜ A beautiful, fluid, and extensible dialogs API for Kotlin & Android.
Kotlin
19,672
star
2

aesthetic

[DEPRECATED]
Kotlin
1,999
star
3

photo-affix

๐Ÿ“ท Stitch your photos together vertically or horizontally easily!
Kotlin
1,008
star
4

material-cab

๐Ÿš• An Android & Kotlin library for placing and manipulating Contextual Action Bars in your UI.
Kotlin
982
star
5

assent

๐Ÿ™ Android Runtime Permissions made easy and compact, for Kotlin and AndroidX. With coroutines support!
Kotlin
851
star
6

ason

[DEPRECATED]: Prefer Moshi, Jackson, Gson, or LoganSquare
Java
758
star
7

recyclical

๐Ÿš€ An easy-to-use, extensible Kotlin DSL for setting up and manipulating RecyclerViews.
Kotlin
716
star
8

vvalidator

๐Ÿค– An easy to use form validator for Kotlin & Android.
Kotlin
658
star
9

mnml

๐Ÿ“น A minimal, beautiful screen recorder for Android.
Kotlin
656
star
10

nock-nock

๐Ÿšช Monitor and validate your websites to maintain maximum uptime.
Kotlin
377
star
11

inline-activity-result

Receive Activity results inline, without any boilerplate. Optional coroutines and RxJava support.
Kotlin
316
star
12

rxkprefs

๐Ÿ›  A small Kotlin library to make shared preferences easy + RxJava and Coroutines support
Kotlin
277
star
13

ulfberht

๐Ÿ—ก๏ธ A small but powerful & opinionated DI library. Written in Kotlin, and powered by annotation processing.
Kotlin
251
star
14

viewpagerdots

๐Ÿ‘€ Simple, compact Kotlin library for ViewPager page indicators.
Kotlin
189
star
15

date-picker

๐Ÿ“… Custom responsive date picker widget for Android, written in Kotlin.
Kotlin
159
star
16

assure

A Kotlin library that makes biometric authentication quick and easy.
Kotlin
67
star
17

arctic-icon-request

An easy to use Icon Request API for Kotlin, with traditional email capabilities plus the Arctic Manager API.
Kotlin
60
star
18

af.codes

๐ŸŒ The source code of my personal website.
HTML
31
star
19

pi-feeder

My WIP Raspberry Pi auto pet feeder system.
CSS
31
star
20

pi-feeder-android

A simple client for my Pi Feeder IoT device. Used for discovery and basic setup, scheduling is left to the web dashboard.
Java
19
star
21

node-deploy

A Git and HTTP powered deployment system for Node.js apps.
JavaScript
8
star
22

web-recording-experiments

Experiments with recording audio and video from the web. This repo is incomplete, right now. I will be adding more.
JavaScript
4
star
23

trifles

JavaScript
3
star
24

color-render

JavaScript
3
star
25

afollestad

2
star