Spread the love

List UI is a very common thing in Android Applications. Pretty much every single app has a list of something they want to display.RecyclerView Selection is a library that will allow you to handle item selection a lot easier. It will help you handle motion events and touch events, and convert them into selection in the RecyclerView.This is a fairly flexible library where it allows 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

As you can imagine, you add the new dependency to your build.gradle file. An important thing to note here is that we are using the new AndroidX artifact.
Add following dependency to your app’s build.gradle file.28.0.0-alpha is a pre-release version to support the Android P developer preview.

For the setup what you need to do is you want to create a new adapter and important thing is that it uses stable IDs.There is no selection code just yet. You need just basic RecyclerView setup.

2.Implement ItemKeyProvider

Developers must decide on the key type used to identify selected items. There are three key types: Parcelable, String, and Long.ItemKey provider conjunction of stable IDs. It will allow for a quick mapping between the IDs and the items that will handle the selection by the selection library.

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 a RecyclerView.ViewHolder instance.

It is actually a fairly simple class. You need to overwrite only one method. That returns the position and the selection key for the item that is for the given motion event.

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.

The adapter, as I said again, nothing super exciting. The only important thing here is that we’re doing stable IDS. This allows for a consistent mapping from ID to the item.

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.


RecyclerView has no default selection mechanism. You have to handle it in onBind.There you might want to change the background of a view or you can use it by setting activated state to get activates state working. You use background for the view and that background is a selectable drawable that has an activated state which will allow to indicate to the user that the item has been selected.

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

Now, what we need to set up is the selection tracker, which actually is the actual machinery behind the scenes, and we pass in RecyclerView, the key provider both of these that we just created.
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