Design + Code an Android App from Scratch
This video shows how to implement Swipe to Delete for RecyclerView using the itemTouchHelper.Callback interface. In the first step we create a class that extends from ItemTouchHelper.Callback. Override the getMovementFlags to call makeMovementFlags where we specify the dragFlags and swipeFlags. In our case, we don’t support dragging and hence return 0 and for the swipe flags we specify ItemTouchHelper.END to indicate that our RecyclerView should be capable of being swiped from left to right in most languages and right to left while the user is using Arabic or Hebrew. To disable dragging, return false from the isLongPressDragEnabled method and to enable swiping return true from the isItemViewSwipeEnabled. Finally override the onSwipe method and forward the event to the RecyclerView.Adapter where we can delete the item from the database by calling the removeFromRealm method and notifyDataSetChanged method to refresh the RecyclerView.