recyclerview-selection provides item selection support for RecyclerView.It support for creating, modifying, inspecting, and monitoring changes to items in a RecyclerView list.It enables users to select items in RecyclerView list using touch or mouse input.

To add selection support to a RecyclerView instance, follow these steps:

1.Add Dependency


Add following depedency to your app’s build.gradle file.28.0.0-alpha1 is a pre-release version to support the Android P developer preview.

2.Implement ItemKeyProvider


Developers must decide on the key type used to identify selected items. There are three key types: Parcelable,String, and Long.

3.Implement ItemDetails


ItemDetails implementation provides the selection library with access to information about a specific RecyclerView item. This class is a key component in controling the behaviors of the selection library in the context of a specific activity.

4.Implement ItemDetailsLookup


ItemDetailsLookup enables the selection library to access information about RecyclerView items given a MotionEvent. It is effectively a factory for ItemDetails instances that are backed up by aRecyclerView.ViewHolder instance.

The Selection library calls getItemDetails() when it needs access to information about the area. Your implementation must negotiate ViewHolder lookup with the corresponding RecyclerView instance and the subsequent conversion of the ViewHolder instance to an ItemDetailsLookup.ItemDetails instance.

5.Reflect Selected State


When the user selects an item the library will record that in SelectionTracker then notify RecyclerView that the state of the item has changed.The item must then be updated to reflect the new selection status. Without this, the user will not see that the item has been selected.

Update the styling of the view to represent the activated status with a color state list.

item_list.xml

The selection library does not provide a default visual decoration for the selected items. You must provide this when you implement onBindViewHolder().

6.Add Contextual Actions


ActionModes used to provide alternative interaction modes and replace parts of the normal UI until finished. Action modes include text selection and contextual actions.

7.Build SelectionTracker


In order to build a SelectionTracker instance, your app must supply the same Adapter that you used to initialize RecyclerView to SelectionTracker.Builder.

Register a SelectionTracker.SelectionObserver to be notified when the selection changes. When a selection is first created, start ActionMode to represent this to the user, and provide selection specific actions.

8.Activity lifecycle events


In order to preserve state you must handling of Activity lifecycle events. your app must call the selection tracker’sonSaveInstanceState() and onRestoreInstanceState() methods from the activity’s onSaveInstanceState()and onRestoreInstanceState() methods respectively.

 

Download this Project from GitHub