Android RecyclerView Drag and Drop

Filed Under: Android

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

android drag drop recyclerview


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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

        app:layoutManager="" />


The code for the is given below:

package com.journaldev.androidrecyclerviewdraganddrop;

import android.os.Bundle;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerViewAdapter mAdapter;
    ArrayList<String> stringArrayList = new ArrayList<>();

    protected void onCreate(Bundle savedInstanceState) {

        recyclerView = findViewById(;


    private void populateRecyclerView() {
        stringArrayList.add("Item 1");
        stringArrayList.add("Item 2");
        stringArrayList.add("Item 3");
        stringArrayList.add("Item 4");
        stringArrayList.add("Item 5");
        stringArrayList.add("Item 6");
        stringArrayList.add("Item 7");
        stringArrayList.add("Item 8");
        stringArrayList.add("Item 9");
        stringArrayList.add("Item 10");

        mAdapter = new RecyclerViewAdapter(stringArrayList);

        ItemTouchHelper.Callback callback =
                new ItemMoveCallback(mAdapter);
        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);



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:

package com.journaldev.androidrecyclerviewdraganddrop;


public class ItemMoveCallback extends ItemTouchHelper.Callback {

    private final ItemTouchHelperContract mAdapter;

    public ItemMoveCallback(ItemTouchHelperContract adapter) {
        mAdapter = adapter;

    public boolean isLongPressDragEnabled() {
        return true;

    public boolean isItemViewSwipeEnabled() {
        return false;

    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {


    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        return makeMovementFlags(dragFlags, 0);

    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                          RecyclerView.ViewHolder target) {
        mAdapter.onRowMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return true;

    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder,
                                  int actionState) {

        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            if (viewHolder instanceof RecyclerViewAdapter.MyViewHolder) {
                RecyclerViewAdapter.MyViewHolder myViewHolder=
                        (RecyclerViewAdapter.MyViewHolder) viewHolder;


        super.onSelectedChanged(viewHolder, actionState);
    public void clearView(RecyclerView recyclerView,
                          RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);

        if (viewHolder instanceof RecyclerViewAdapter.MyViewHolder) {
            RecyclerViewAdapter.MyViewHolder myViewHolder=
                    (RecyclerViewAdapter.MyViewHolder) viewHolder;

    public interface ItemTouchHelperContract {

        void onRowMoved(int fromPosition, int toPosition);
        void onRowSelected(RecyclerViewAdapter.MyViewHolder myViewHolder);
        void onRowClear(RecyclerViewAdapter.MyViewHolder myViewHolder);



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:

package com.journaldev.androidrecyclerviewdraganddrop;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> implements ItemMoveCallback.ItemTouchHelperContract {

    private ArrayList<String> data;

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView mTitle;
        View rowView;

        public MyViewHolder(View itemView) {

            rowView = itemView;
            mTitle = itemView.findViewById(;

    public RecyclerViewAdapter(ArrayList<String> data) { = data;

    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_row, parent, false);
        return new MyViewHolder(itemView);

    public void onBindViewHolder(MyViewHolder holder, int position) {

    public int getItemCount() {
        return data.size();

    public void onRowMoved(int fromPosition, int toPosition) {
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(data, i, i + 1);
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(data, i, i - 1);
        notifyItemMoved(fromPosition, toPosition);

    public void onRowSelected(MyViewHolder myViewHolder) {


    public void onRowClear(MyViewHolder myViewHolder) {


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:

public interface StartDragListener {
    void requestDrag(RecyclerView.ViewHolder viewHolder);

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

    public void requestDrag(RecyclerView.ViewHolder viewHolder) {

mAdapter = new RecyclerViewAdapter(stringArrayList,this);

Inside the do the following:

holder.imageView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() ==
                        MotionEvent.ACTION_DOWN) {
                return false;

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:


  1. Tharindu says:

    Thanks a lot,
    This was a big help to me for understand about ItemTouchHelper.Callback library as a beginner,
    Also that “Drag And Drop using Handles” implementation is really impressive and it was like 2 birds from 1 shot for my knowledge.
    Again thanks a lot.

  2. Raghav Sharma says:

    Not working with Horizontal RecyclerView. What should i do to make it work for HORIZONTAL view as well?

  3. Mohit Dholakia says:

    Is it possible to auto scroll recycleview while reaching at the bottom?

  4. Dhivya says:


    Can’t able to drag more than one position. Can move only one position for each drag.
    How to fix?

  5. Ahsaan Akhtar says:

    thanks brother its working for me.

  6. Gugan Sakthivel says:

    Dragging with large dataset causes serious lagging.Even the ui freezes for seconds!Any solutions for this?

  7. Neth says:

    I got this to work, except for the fact that the places aren’t saved. If I come back to the app/activity, they resent to numerical order. How do I get them to save their changed order, please?

  8. Cesar Bibriesca says:

    Thanks for share this, its a good example and easy to follow.

    Have a nice day!

  9. rohan says:

    what if I want to use this in a fragment. I got error of “this”, in “mAdapter = new RecyclerViewAdapter(stringArrayList,this);”

    1. Raj says:

      I guess you are not sending the correct context over here, try sending context like getActivity() or getApplicationContext()

  10. Prashant says:

    Thanks for the article. Helped me a lot.

  11. Baradwaj Varadharajan says:

    There are two errors with this code.
    The RecyclerVIewAdapter class should extend RecyclerView.Adapter and not just the RecyclerView.Adapter

    Second is, you have missed using the String.valueOf() set the SetText in onBindView method for the data.getPosition() since data is an ArrayList and no type is specified for it an explicit conversion is required.

    1. Baradwaj Varadharajan says:

      *Should extend RecyclerView.Adapter

      1. Anupam says:

        As you can see (in your comments as well), the brackets are getting escaped < >
        I’ve fixed the formatting issue.
        Thanks for informing.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors