Callback
This class is the contract between ItemTouchHelper and your application. It lets you control which touch behaviors are enabled per each ViewHolder and also receive callbacks when user performs these actions.
To control which actions user can take on each view, you should override getMovementFlags and return appropriate set of direction flags. (LEFT, RIGHT, START, END, UP, DOWN). You can use makeMovementFlags to easily construct it. Alternatively, you can use SimpleCallback.
If user drags an item, ItemTouchHelper will call onMove(recyclerView, dragged, target). Upon receiving this callback, you should move the item from the old position (dragged.getAdapterPosition()
) to new position (target.getAdapterPosition()
) in your adapter and also call notifyItemMoved. To control where a View can be dropped, you can override canDropOver. When a dragging View overlaps multiple other views, Callback chooses the closest View with which dragged View might have changed positions. Although this approach works for many use cases, if you have a custom LayoutManager, you can override chooseDropTarget to select a custom drop target.
When a View is swiped, ItemTouchHelper animates it until it goes out of bounds, then calls onSwiped. At this point, you should update your adapter (e.g. remove the item) and call related Adapter#notify event.