ItemTouchHelper

(SESL variant) This is a utility class to add swipe to dismiss and drag &drop support to RecyclerView.

It works with a RecyclerView and a Callback class, which configures what type of interactions are enabled and also receives events when user performs these actions.

Depending on which functionality you support, you should override onMove and / or onSwiped.

This class is designed to work with any LayoutManager but for certain situations, it can be optimized for your custom LayoutManager by extending methods in the ItemTouchHelper.Callback class or implementing ItemTouchHelper.ViewDropHandler interface in your LayoutManager.

By default, ItemTouchHelper moves the items' translateX/Y properties to reposition them. You can customize these behaviors by overriding onChildDraw or onChildDrawOver.

Most of the time you only need to override onChildDraw.

Constructors

Link copied to clipboard
constructor(@NonNull callback: ItemTouchHelper.Callback)
Creates an ItemTouchHelper that will work with the given Callback.

Types

Link copied to clipboard
abstract class Callback
This class is the contract between ItemTouchHelper and your application.
Link copied to clipboard
A simple wrapper to the default Callback which you can construct with drag and swipe directions and this class will handle the flag callbacks.
Link copied to clipboard
interface ViewDropHandler
An interface which can be implemented by LayoutManager for better integration with ItemTouchHelper.

Properties

Link copied to clipboard
A View is currently being dragged.
Link copied to clipboard
ItemTouchHelper is in idle state.
Link copied to clipboard
A View is currently being swiped.
Link copied to clipboard
Animation type for views that were dragged and now will animate to their final position.
Link copied to clipboard
Animation type for views which are not completely swiped thus will animate back to their original position.
Link copied to clipboard
Animation type for views which are swiped successfully.
Link copied to clipboard
val DOWN: Int = 2
Down direction, used for swipe &drag control.
Link copied to clipboard
val END: Int = 32
Horizontal end direction.
Link copied to clipboard
val LEFT: Int = 4
Left direction, used for swipe &drag control.
Link copied to clipboard
val RIGHT: Int = 8
Right direction, used for swipe &drag control.
Link copied to clipboard
val START: Int = 16
Horizontal start direction.
Link copied to clipboard
val UP: Int = 1
Up direction, used for swipe &drag control.

Functions

Link copied to clipboard
open fun attachToRecyclerView(@Nullable recyclerView: RecyclerView)
Attaches the ItemTouchHelper to the provided RecyclerView.
Link copied to clipboard
open fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State)
Retrieve any offsets for the given item.
Link copied to clipboard
Called when a view is attached to the RecyclerView.
Link copied to clipboard
Called when a view is detached from RecyclerView.
Link copied to clipboard
open fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State)
Draw any appropriate decorations into the Canvas supplied to the RecyclerView.
Link copied to clipboard
Draw any appropriate decorations into the Canvas supplied to the RecyclerView.
Link copied to clipboard
Link copied to clipboard
open fun setEndDraggingText(text: String)
Link copied to clipboard
open fun setMoveDraggingText(text: String)
Link copied to clipboard
Link copied to clipboard
open fun startDrag(@NonNull viewHolder: RecyclerView.ViewHolder)
Starts dragging the provided ViewHolder.
Link copied to clipboard
Starts swiping the provided ViewHolder.