Swipe gestures in RecyclerView | Swipe to Delete and Archive in RecyclerView

In this tutorial, we’re going to implement swiping in our recyclerView. If you’re don’t know how to create recyclerView then go through my previous tutorial on recyclerView. Click Here.

Here we implement swipe gestures for delete and archive, when we swipe from right to the left item of the recyclerView will delete and when we swipe from left to the right item of the recyclerView will archive. Also, we can undo the item back into the recyclerView.

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 swiping gestures into your recyclerView.

Create a class for swipe gesture

After the onCreate() method create a class for swipe by using ItemTouchHelper.SimpleCallback() method. Simple callbacks need two parameters, one is the drag directions and another one is the swipe. Since we’re not implementing the drag directions in this tutorial so add “0” in the drag parameter.

For swipe, if you’re using only the left directions then add ItemTouchHelper.LEFT or if you want to use the right direction then we can use the ItemTouchHelper.Right. Since we’re using both the directions, so add both in the swipe parameter.

(After onCreate() method)

Here onMove() method is used for drag and drop. If you want to move your items in the recyclerView then we can use this method to move your items. We’ll discuss it in our next tutorial.

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

Attach the simple callback to our recyclerView

After creating the simple callback 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.

Give the directions

Now inside the onSwiped() we’re using a switch case so that we can handle the directions. That means the LEFT and RIGHT directions.

Swipe LEFT to delete gesture

First, we need to get the positions of the items and remove them from the recyclerView using the recyclerView adapter which we already initialized in our previous tutorial. Click here to see.

Below lines of code delete the item from the recyclerView using the swipe gesture.

Now, to undo the same deleted item into your recyclerView we’ll use a snackbar with an undo button. For that create a String variable which will keep the deleted movie or item and specify it to “null”, because initially there is no deleted movie.

Now before going to delete the movie we need to store that the movie which will be deleted into the String variable that we are created inside the LEFT case and then remove the item.

For the snackbar, we need to add one material library inside the build.gradle app file.

Now use the snackbar to undo the item. Snackbar takes three parameters; first, the view that is the recyclerView, the second one is the text which is then deleted movie and the third one is the duration. After that we need to set action by using the setAction() method here we passing two things, one is the text of the action that is “Undo” and then onClickListener. After that show the snackbar by using the “.show()” method.

Inside onclick, what we will do, we will undo the movie inside our recyclerView. For that, we can simply add our deleted movie back to the position where the item is deleted by using moviesList.add(position, deletedMovie); method.

Now we need to add that row back to the recyclerView, for that notify the recyclerView that the item is inserted by using recyclerAdapter.notifyItemInserted(position);

The final look of the snackbar to undo the deleted movie:

Swipe RIGHT to archive gesture

For that first create a list of string of archived movies which is equal to the new array list. Initial this above the ItemTouchHelper where we initialize our deleted movie.

Now we need to get the movie which will be archived by using their positions inside the RIGHT case.

Here, we get the movies position and remove it from the recyclerView and store it inside the archivedMovies list.

Inside the snackbar, we handle the undo button for the archived movies.

Handle the background

Here we simply add the backgrounds for the deleted and archived movie. For that, we need to add a dependency in the build.gradle app file.

For background create onDraw() method inside the ItemTouchHelper class, inside that, simply add these lines.

** Note: First import the drawable files.


Hi everyone, myself Golap an Android app developer with UI/UX designer.