Android RecyclerView Swipe To Delete And Undo With Examples

In this tutorial, we’ll be discussing and implementing the Swipe to Delete feature on the RecyclerView in our Android Application.

Android Swipe To Delete

Swipe to delete feature is commonly used to delete rows from a RecyclerView.

In order to implement Swipe to delete feature, we need to use the ItemTouchHelper utility class.

ItemTouchHelper.Callback

In order to use ItemTouchHelper class, we need to implement the ItemTouchHelper.Callback.

ItemTouchHelper.Callback class is used primarily for drag and drop and swipe to delete behaviors.

In this tutorial, we’ll be sticking with the swipe to delete only.

Android framework provides us with a basic implementation of ItemTouchHelper.Callback which is SimpleCallback.

We will be creating our own implementation of the Swipe to delete feature.

Following are the major methods that need to be overridden in our class:

getMovementFlags – Here we set the direction of swipe. We return the direction flag in a static method makeMovementFlags.

onMove – This is used for drag and drop. If not needed, return false here.
onSwiped – This gets triggered when the swipe action is detected.

A complete swipe goes the full width of the screen. In order to set a consider a partial swipe as a swipe we need to override the following method:

getSwipeThreshold – Here we return the float value. example 0.5f means that a 50 percent swipe on the RecyclerView row would be considered as a swipe.

onChildDraw – Here we’ll create our custom view that shows that the swipe is happening.

ItemTouchHelper.Callback is used to just swipe the rows. It doesn’t delete them itself. We’ll need to delete it ourself using the RecyclerView Adapter.

Enough Talk. Let’s code.

In the next section, we’ll be creating our android application with a RecyclerView containing the Swipe to Dismiss feature. We’ll provide a Snackbar with the undo option.

Project Structure

android-swipe-to-dimiss-recyclerview-project

Add the following dependency in the app’s build.gradle:

Code

The code for the activity_main.xml layout is given below:

The code for the SwipeToDeleteCallback.java class which extends the ItemTouchHelper.Callback class is given below:

The class is abstract since we haven’t implemented the onSwipe method. We’ll do that in the MainActivity.java class.

Inside the onChildDraw method we check if the swipe is done or not using the isCancelled boolean.
Based on that we create a view with delete icon.

We’ve set the Swipe threshold to 0.7. That means if the row is swiped less than 70%, the onSwipe method won’t be triggered.

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

In order to set the ItemTouchHelper onto the RecyclerView, the attachToRecyclerView method is used.

When the Snackbar action is clicked we restore the item in the RecyclerView using the restoreItem method.

The restoreItem method is defined in the RecyclerViewAdapter class.

scrollToPosition scrolls the RecyclerView to the position specified. This is mainly used when an item is inserted at the top of the RecyclerView.

The code for the cardview_row.xml is given below:

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

The output of the above application in action is given below:

android recyclerview swipe to delete output

This brings an end to this tutorial. You can download the project from the link below:

By admin

Leave a Reply