Drag Select Recycler View


Source link: https://github.com/afollestad/drag-select-recyclerview

Drag Select Recycler View

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.

Sample

You can download a sample APK or view it on Google Play!


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 {

  // ... other dependencies
  compile 'com.afollestad:drag-select-recyclerview:1.0.0' 
}

Tutorial

  1. Introduction
  2. DragSelectRecyclerView
    1. How to create a DragSelectRecyclerView in your layout. How to set it up from code.
  3. Adapter Implementation
    1. An example of how adapter's should be setup.
  4. User Activation, Initializing Drag Selection
    1. How drag selection mode is activated by a long press. How to maintain selected items through configuration changes, etc.
  5. Auto Scroll
    1. By default, this library will auto-scroll up or down if the user holds their finger at the top or bottom of the list during selection mode.

Introduction

DragSelectRecyclerView is the main classes 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.


DragSelectRecyclerView

DragSelectRecyclerView replaces the regular RecyclerView in your layouts. It intercepts touch events when you tell if selection mode is active, and automatically reports to your adapter.

<com.afollestad.dragselectrecyclerview.DragSelectRecyclerView

android:id="@+id/list"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scrollbars="vertical" />

Setup is basically the same as it would be for a regular RecyclerView. You just set a LayoutManager and RecyclerView.Adapter to it:

DragSelectRecyclerView list = (DragSelectRecyclerView) findViewById(R.id.list);
 list.setLayoutManager(new GridLayoutManager(this, 3));
 list.setAdapter(adapter);

Adapter Implementation

You use regular RecyclerView.Adapter's with the DragSelectRecyclerView. However, it has to implement the IDragSelectAdapter interface:

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder>

 implements IDragSelectAdapter {

 @Override
public void setSelected(int index, boolean selected) {

  // 1. Make this index as selected in your implementation.
  // 2. Tell the RecyclerView.Adapter to render this item's changes.
  notifyItemChanged(index);

}

@Override
public boolean isIndexSelectable(int index) {

  // Return false if you don't want this position to be selectable.
  // Useful for items like section headers.
  return true;

}

  // The rest of your regular adapter overrides 
}

Checkout the sample project for an in-depth example.


User Activation, Initializing Drag Selection

The library won't start selection mode unless you tell it to. You want the user to be able to active it. The click listener implementation setup in the adapter above will help with this.

public class MainActivity extends AppCompatActivity implements

 MainAdapter.ClickListener, DragSelectRecyclerViewAdapter.SelectionListener {

 private DragSelectRecyclerView listView;
private MainAdapter adapter;
 @Override
protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

// Setup adapter and callbacks
  adapter = new MainAdapter(this);

// Setup the recycler view
  listView = (DragSelectRecyclerView) findViewById(R.id.list);

  listView.setLayoutManager(

new GridLayoutManager(this,

  getResources().getInteger(R.integer.grid_width)));

  listView.setAdapter(adapter);

}

 /**  
* See the adapter in the sample project for a click listener implementation. Click listeners  
* aren't provided by this library. 
*/
@Override
public void onClick(int index) {

  // Single click will select or deselect an item
  adapter.toggleSelected(index);

}

 /**  
* See the adapter in the sample project for a click listener implementation. Click listeners  
* aren't provided by this library. 
*/
@Override
public void onLongClick(int index) {

  // Initialize drag selection -- also selects the initial item
  listView.setDragSelectActive(true, index);

}
 
}

Auto Scroll

By default, this library will auto scroll. During drag selection, moving your finger to the top of the list will scroll up. Moving your finger to the bottom of the list will scroll down.

At the start of the activation point at the top or bottom, the list will scroll slowly. The further you move into the activation area, the faster it will scroll.

You can disable auto scroll, or change the activation hotspot from your layout XML:

<com.afollestad.dragselectrecyclerview.DragSelectRecyclerView
  android:id="@+id/list"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:scrollbars="vertical"
  app:dsrv_autoScrollEnabled="true"
  app:dsrv_autoScrollHotspotHeight="56dp" />

56dp is the default hotspot height, you can raise or lower it if necessary. Smaller hotspots will scroll quickly since there's not much room for velocity change.

Resources

Base repository to demonstrate the process of uploading an aar/jar to JitPack.

Simple modal presenter that can be easily customized. The modal will be shown in the bottom half part of the screen.

Customizable Timeline View for Android, Create a simple timeline list with few lines of code. You can adjust the image, image size, line color and line size.

This Project contains source code of Weather Android Application, designed similar to Yahoo Weather App, Based on World Weather Online.

Meet detekt, a static code analysis tool for the Kotlin programming language. It operates on the abstract syntax tree provided by the Kotlin compiler.

Espresso is a great tool to test our Android apps via instrumental tests. With them, we can mimic user actions like clicking a button, scrolling a list, selecting an item on a spinner or swiping on a pager. Then, we can assert that a text appears in the screen, an image is visible or invisible, or a button is enabled or not.

On the other hand, if you tried Espresso, you’ll agree that its API is not discoverable.

Barista introduces a discoverable API for the Espresso features. So, you and all the Android team will write instrumental tests with no effort.

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