Drag and Swipe with RecyclerView

Part One: Basic ItemTouchHelper Example

June 23, 2015


ItemTouchHelper is a powerful utility that takes care of everything you need to add both drag & drop and swipe-to-dismiss to your RecyclerView. It’s a subclass of RecyclerView.ItemDecoration, which means it’s easily added to almost-any existing LayoutManager and Adapter(!). It also works with existing item animations, and gives you type-restricted dragging, drop settling animations, and much more. In this article, I’ll demonstrate a simple implementation of ItemTouchHelper. Later, in this series, we’ll expand the scope and explore more features.

Skip ahead

Just interested in seeing the completed source? Jump to Github: Android-ItemTouchHelper-Demo. The first commit lines up with this article. Download the demo apk from here.

Setting up

First thing we need is a basic RecyclerView setup. If you haven’t already, update your build.gradle to include the RecyclerView dependency.

compile 'com.android.support:recyclerview-v7:22.2.0'

Using ItemTouchHelper and ItemTouchHelper.Callback

In order to use ItemTouchHelper, you’ll create an ItemTouchHelper.Callback. This is the interface that allows you to listen for “move” and “swipe” events. It’s also where you are able to control the state of the view selected, and override the default animations. There’s a helper class that you can use if you want a basic implementation, SimpleCallback, but for the purposes of learning how it works, we’ll make our own.

getMovementFlags(RecyclerView, ViewHolder)onMove(RecyclerView, ViewHolder, ViewHolder)onSwiped(ViewHolder, int)
public int getMovementFlags(RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
public boolean isLongPressDragEnabled() {
return true;
public boolean isItemViewSwipeEnabled() {
return true;
public interface ItemTouchHelperAdapter {

void onItemMove(int fromPosition, int toPosition);

void onItemDismiss(int position);
public class RecyclerListAdapter extends 
implements ItemTouchHelperAdapter {
// ... code from gist@Override
public void onItemDismiss(int position) {

public boolean onItemMove(int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(mItems, i, i + 1);
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(mItems, i, i - 1);
notifyItemMoved(fromPosition, toPosition);
return true;
private final ItemTouchHelperAdapter mAdapter;

public SimpleItemTouchHelperCallback(
ItemTouchHelperAdapter adapter) {
mAdapter = adapter;
public boolean onMove(RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target) {
return true;
public void onSwiped(RecyclerView.ViewHolder viewHolder,
int direction) {
ItemTouchHelper.Callback callback = 
new SimpleItemTouchHelperCallback(adapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);


This is a bare-bones implementation of ItemTouchHelper. However, it should be clear that a third-party library is not needed for basic drag & drop and swipe-to-dismiss with RecyclerView. In the next part, we’ll take more control over the appearance of the items while being dragged or swiped.

Source code

I created a project on GitHub to demonstrate the things covered in this article series: Android-ItemTouchHelper-Demo. The first commit corresponds to this part, and also has a bit of part 2 in it .

Next Parts

Part Two: Handles, Grids, and Custom Animations

Android App Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store