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

Code

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="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager" />


</LinearLayout>

The code for the MainActivity.java is given below:


package com.journaldev.androidrecyclerviewdraganddrop;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);

        populateRecyclerView();
    }

    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);
        touchHelper.attachToRecyclerView(recyclerView);

        recyclerView.setAdapter(mAdapter);
    }

}

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

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

Let’s look at each of these files.

The code for the ItemMoveCallback.java class is given below:


package com.journaldev.androidrecyclerviewdraganddrop;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

public class ItemMoveCallback extends ItemTouchHelper.Callback {

    private final ItemTouchHelperContract mAdapter;

    public ItemMoveCallback(ItemTouchHelperContract adapter) {
        mAdapter = adapter;
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return false;
    }



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

    }

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

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

    @Override
    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;
                mAdapter.onRowSelected(myViewHolder);
            }

        }

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

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

    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 RecyclerViewAdapter.java class is given below:


package com.journaldev.androidrecyclerviewdraganddrop;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
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) {
            super(itemView);

            rowView = itemView;
            mTitle = itemView.findViewById(R.id.txtTitle);
        }
    }

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

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

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.mTitle.setText(data.get(position));
    }


    @Override
    public int getItemCount() {
        return data.size();
    }


    @Override
    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);
    }

    @Override
    public void onRowSelected(MyViewHolder myViewHolder) {
        myViewHolder.rowView.setBackgroundColor(Color.GRAY);

    }

    @Override
    public void onRowClear(MyViewHolder myViewHolder) {
        myViewHolder.rowView.setBackgroundColor(Color.WHITE);

    }
}


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.


@Override
    public void requestDrag(RecyclerView.ViewHolder viewHolder) {
        touchHelper.startDrag(viewHolder);
    }

mAdapter = new RecyclerViewAdapter(stringArrayList,this);

Inside the RecyclerViewAdapter.java do the following:


holder.imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() ==
                        MotionEvent.ACTION_DOWN) {
                    mStartDragListener.requestDrag(holder);
                }
                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:

Comments

  1. 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?

  2. Cesar Bibriesca says:

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

    Have a nice day!

  3. rohan says:

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

  4. Prashant says:

    Thanks for the article. Helped me a lot.

  5. 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:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages