Javatpoint Logo
Javatpoint Logo

Android Swipe to Delete RecyclerView items with UNDU

In this tutorial, we will create the example to delete an item of RecyclerView by swiping the item with undo functionality. For this purpose, we will use ItemTouchHelper class which is added in Android Support Library V7. This class is used to create a swipe to delete the items of RecyclerView. It has a SimpleCallback class that configures the events which are performed to swipe or move the item of the RecyclerView.

ItemTouchHelper class

It is a utility class which provides the facility to add swipe to dismiss and drag & drops the items of RecyclerView. It overrides the callback methods onMove() or onSwipe() depending upon the functionality we implement.

Android Swipe to Delete RecyclerView items Example

Create a MainActivity.java class with the following code.

activity_main.xml


content_main.xml


MainActivity.java


Create a layout regular_item.xml which appears at normal mode.

regular_item.xml


Create a layout swipe_item.xml which appears at the period of swipe item.

swipe_item.xml


customlayout.xml


Create a utility class SwipeUtil.java which extends the ItemTouchHelper.SimpleCallback class and overrides its onMove(), onSwiped(), onChildDraw() methods. The onSwiped() method is called when we perform swipe operation on an item, and the onChildDraw() methods contain the implementation logic of drawing canvas while swiping the items of RecyclerView.

SwipeUtil.java


Create an ItemViewHolder.java class and extends RecyclerView.ViewHolder.

ItemViewHolder.java


Create an adapter class MyAdapter.java which extends the RecyclerView.Adapter class and overrides its onCreateViewHolder(), onBindViewHolder() methods.

MyAdapter.java


Create a MainActivityFragment.java class and extends the Fragment class. In this class, set the adapter MyAdapter.java class and utility SwipeUtil.java class.

fragment_main.xml


MainActivityFragment.java


strings.xml


dimens.xml


colors.xml

Output:

Android Swipe to Delete RecyclerView items with UNDU Android Swipe to Delete RecyclerView items with UNDU Android Swipe to Delete RecyclerView items with UNDU
Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials