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.
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.
Create a layout regular_item.xml which appears at normal mode.
Create a layout swipe_item.xml which appears at the period of swipe item.
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.
Create an ItemViewHolder.java class and extends RecyclerView.ViewHolder.
Create an adapter class MyAdapter.java which extends the RecyclerView.Adapter
Create a MainActivityFragment.java class and extends the Fragment class. In this class, set the adapter MyAdapter.java class and utility SwipeUtil.java class.