Drag and Drop Reorder in RecyclerView | Android
By: Golap Gunjan Barman
Tags: Android App Development, Android, AndroidDev, Recyclerview, Android Recyclerview

In this tutorial, we’re going to learn how to handle drag and drop reordering inside a recyclerView. If you’re don’t know how to create recyclerView then go through my previous tutorial on recyclerView. Click Here.

Here we implement drag and drop reordering of the items in the recyclerView, for dragging the item long-press it and drop it where you want.

Before going to implement it make sure we have created the recyclerView. Otherwise, there is no use of these gestures. If you’re not created your recyclerView then you can create it by clicking the above click here button or if you have already created your recyclerView then you can follow the below steps to implement the drag and drop reordering into your recyclerView.

Create a class for drag and drop

After the onCreate() method create a class for dragging by using ItemTouchHelper.SimpleCallback() method. Simple callbacks need to parameters, one is the drag directions and another one is the swipe. Since we’re not implementing the swap directions in this tutorial so add “0” in the swipe parameter. And if you want to add the swipe parameter then go through my previous tutorial on swipe gestures in recyclerView. Click here.

For drag, if you’re using the down directions then add ItemTouchHelper.DOWN or if you want to use the up direction then we can use the ItemTouchHelper.UP. The application works even you use UP and DOWN gestures but we will also use START and END. For that use ItemTouchHelper.START and ItemTouchHelper.END.

(After onCreate() method)

ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback( ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END, 0) { @Override public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) { return false; } @Override public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { } } ;

Here onSwiped() method is used for swipe left and right. If you want to swipe your items in both directions in the recyclerView then we can use this method. We have already implemented it in our previous tutorial. Click Here.

Since we’re only implementing the dragging behavior here, so will add our codes inside the onMove() method.

Attach the simple callback to our recyclerView

After creating the simple callbacks methods simply attach the simple callback to your recyclerView inside onCreate() method, otherwise, you’re not able to swipe left or right your items in the recyclerView. The below lines will add a simple callback method into the recyclerView.

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback); itemTouchHelper.attachToRecyclerView(recyclerView);

Quick Look

As you see the background is transparent, but we don’t need the transparent background here.

For that, we add a white background in our row item.

Now there is no transparent background.

Handle the drag and drop

public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target)

As we see, we have two view holders; one for the selected position (from where we drag our item) and one for the target position (to drop the item).

We need both the selected and target positions. So we can get both positions by using

int fromPosition = viewHolder.getAdapterPosition(); int toPosition = target.getAdapterPosition();

Now we need to swap the item for that we are using Collections utility class. Here we need three parameters. List (moviesList), selected position (from position) and targeted position (to position).

Collections.swap(moviesList, fromPosition, toPosition);

Now we need to notify our recycler view adapter that the item was moved.

recyclerView.getAdapter().notifyItemMoved(fromPosition, toPosition);

Output