Android RecyclerView Drag and Drop With Examples

In this tutorial, we’ll be discussing and implementing the Drag and Drop functionality over our RecyclerView in an Android Application. We’ve already discussed Swipe to Dismiss feature in our previous tutorial.

RecyclerView Drag and Drop

Drag and Drop can be added in a RecyclerView using the ItemTouchHelper utility class.

Following are the important methods in the ItemTouchHelper.Callback interface which needs to be implemented:

  • isLongPressDragEnabled – return true here to enable long press on the RecyclerView rows for drag and drop.
  • isItemViewSwipeEnabled – This is used to enable or disable swipes. In this tutorial, we’ll disable this.
  • getMovementFlags – Here we pass the flags for the directions of drag and swipe. Since swipe is disable we pass 0 for it.
  • onMove – Here we set the code for the drag and drop.
    onSwipe – Here we implement the code for swiping. We’ll keep this empty in the current tutorial.
  • onSelectedChanged – Based on the current state of the RecyclerView and whether it’s pressed or swiped, this method gets triggered. Here we can customize the RecyclerView row. For example, changing the background color.
  • clearView – This method gets triggered when the user interaction stops with the RecyclerView row.

Let’s start building our android application with the drag and drop feature on the RecyclerView.

Project Structure



The code for the activity_main.xml layout which contains a RecyclerView only is given below:

The code for the is given below:

In this, we’ve populated a class with an ArrayList of Strings.

We’ve attached an instance of the class on the RecyclerView to start drag and drop.

Let’s look at each of these files.

The code for the class is given below:

Here, we’ve defined an interface ItemTouchHelperContract. Each of its methods get called from the implemented methods of the ItemTouchHelper.Callback interface.

The code for the class is given below:

onRowMoved defined in the Contract interface earlier gets called when the drag and drop is done.
Here we swap the positions of the two rows present in the ArrayList and call notifyItemMoved to refresh the adapter.

The output of the above application in action is given below:

android recyclerview drag drop output

Up until now, we’ve done the drag and drop by pressing anywhere in the RecyclerView rows.
Next, we’ll see how to do the same by pressing only a particular view inside the RecyclerView row.

Drag And Drop using Handles

In order to use a specific handle view to drag and drop we need to do the following things:

Set isLongPressDragEnabled to false to disable the default drag and drop.

Create an interface like:

Implement it on the MainActivity and pass it to the Adapter.

Inside the do the following:

You can find the updated code in the download link at the end of this tutorial.

The output of the application with the updated code is given below:

android recyclerview drag drop handle output

This brings an end to this tutorial. The complete source code of the project is given below:

By admin

Leave a Reply