DragSelectRecyclerView


Source link: https://github.com/MFlisar/DragSelectRecyclerView

DragSelectRecyclerView

What is it / What does it do?

It's a simple one class TouchListener that can be attached to any RecyclerView and handles multi selection in google photos style via long pressing on an item and moving the finger up/down to select more items (it even scrolls if you reach the edges of the RecyclerView)

Gradle (via JitPack.io)

  1. add jitpack to your project's build.gradle:
repositories {
  maven {
 url "https://jitpack.io" 
}
 
}
  1. add the compile statement to your module's build.gradle:
dependencies {
  compile 'com.github.MFlisar:DragSelectRecyclerView:0.3' 
}

Usage - General

  1. Create the a touch listener like following
mDragSelectTouchListener = new DragSelectTouchListener()  // check region OnDragSelectListener for more infos  .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);

 // default: false;
  1. attach it to the RecyclerView
recyclerView.addOnItemTouchListener(mDragSelectTouchListener);
  1. on item long press, inform the listener so that it can start doing it's magic
// if one item is long pressed, we start the drag selection like following: // we just call this function and pass in the position of the first selected item mDragSelectTouchListener.startDragSelection(position);

Usage - OnDragSelectListener

You have 3 options:

  • use a simple DragSelectTouchListener.OnDragSelectListener => you get notified over which items the user dragged or dragged back

    onDragSelectionListener = new DragSelectTouchListener.OnDragSelectListener() {
    
    @Override
    public void onSelectChange(int start, int end, boolean isSelected) {
    
     // update your selection
     // range is inclusive start/end positions
    
    }
    
     
    }
    
  • use a DragSelectTouchListener.OnAdvancedDragSelectListener => this is an extended version of the DragSelectTouchListener.OnDragSelectListener which will notify you about the start and end of the drag selection as well

     onDragSelectionListener = new DragSelectTouchListener.OnAdvancedDragSelectListener()  {
    
    @Override
    public void onSelectChange(int start, int end, boolean isSelected) {
    
     // update your selection
     // range is inclusive start/end positions
    
    }
    
     @Override
    public void onSelectionStarted(int start) {
    
     // drag selection was started at index start
    
    }
    
     @Override
    public void onSelectionFinished(int end) {
    
     // drag selection was finished at index start
    
    }
      
    }
    ;
  • Preferred option: 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

The DragSelectionProcessor will take care to transform each event to the correct select/deselect event that must be handled by you afterwards. Therefore you must provide a ISelectionHandler in it's constructor. Just implement it's 3 simple functions and you're done.

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);

A demo can be found here: MainActivity.java

TODO

  • support horizontal RecyclerViews... should be quite simple, but is not yet implemented

Credits

This library is heavily inspired and based on https://github.com/weidongjian/AndroidDragSelect-SimulateGooglePhoto

Resources

Use one line to apply almost famous libraries of proguard configurations.

RxDownloader is an RxWrapped Download Manager.

Simple JUnit rule for overriding RxJava/RxAndroid schedulers during unit tests.

A MultiScreen-Support-Library for Android.

A raised button that lowers down to 0dp of elevation.

Typer/writer TextView component.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes