Swipe To Dismiss RecyclerView Items

In my last post I broke down the differences between the RecyclerView and a ListView. One of the benefits of the RecyclerView that I touched on was the ItemTouchHelper. This class is used to handle the Swipe-To-Dismiss and Drag-N-Drop behaviors of a RecyclerView. In this post I am going to teach you how to swipe to dismiss RecyclerView items using the sample application seen here:

Swipe To Dismiss RecyclerView Items

I am going to move forward with the assumption that you are familiar with a RecyclerView and have already added one into your application. If you haven’t, you can check out this link. The sample list for this application will show a list of Movie objects, that have a single field for movieName.

The Adapter

We will not change the adapter much to allow for swiping, but we should add the following method along with all of the other required implementations:

This method will remove an item from our movie list, and notifies the adapter that an item has been removed. By notifying the adapter, we are not rebinding the data but simply altering the positions, as explained in the documentation:

This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.

If you would like to see the entire Adapter class, please see this file on Github.

ItemTouchHelper.SimpleCallback

To handle the swiping we will create an ItemTouchHelper.SimpleCallback class. This is a simple wrapper class using drag and swipe directions to handle those events. In this tutorial, we are only concerned with swiping. Here is our callback:

The required default constructor for the SimpleCallback class takes two parameters. The first is for the drag directions, the second for swiping directions. The two methods are required implementations. However, we are not implementing Drag-N-Drop for this example so we can leave the method empty. Inside the onSwiped method we remove the swiped item from the Adapter using the remove method we added earlier.

Add ItemTouchHelper to RecyclerView

Now that we’ve created our ItemTouchHelper class, we can easily attach it to our RecyclerView in just three lines of code. This code is found inside onCreate of the activity:

Now we’re done! You don’t have to add any animations, all of those are handled behind the scenes. I hope you enjoyed learning how to swipe to dismiss RecyclerView items. The full code for this sample application can be found on my Github page.

Share this post...
Share on Reddit0Share on Google+3Tweet about this on TwitterShare on LinkedIn0Share on Facebook2

9 Comments


  1. I like how simple this is; when I was creating my Food Saver app, I used Swipe-to-delete feature in my recyclerview but had to use a library because, well, I didn’t know how to do this. Now the next time I release an update, I will borrow from your code Adam.

    Thanks for your clear and to the point post on this.


      1. Daniel,

        Apologies for the late response. Yes, anytime you call ItemTouchHelper.attachToRecyclerView() it will attach to the given one, and detach itself from the previous. As a result, passing null will detach it from the previous RecyclerView.


    1. That’s a good question, and something that I personally don’t have experience with yet. I’ll do some digging into it and let you know if I find anything.


  2. Thank you for this. Works for me!
    Except 1 bug — When my Activity gets paused and then resumed, the RecyclerView list does not update after a card has been swiped. Any ideas why?

Leave a Reply