Callback
abstract class Callback
kotlin.Any | |
↳ | androidx.recyclerview.widget.ItemTouchHelper.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(RecyclerView, ViewHolder)
and return appropriate set of direction flags. (LEFT
, RIGHT
, START
, END
, UP
, DOWN
). You can use makeMovementFlags(int, int)
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 RecyclerView.Adapter#notifyItemMoved(int, int)
. To control where a View can be dropped, you can override canDropOver(RecyclerView, ViewHolder, ViewHolder)
. 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(ViewHolder, java.util.List, int, int)
to select a custom drop target.
When a View is swiped, ItemTouchHelper animates it until it goes out of bounds, then calls onSwiped(ViewHolder, int)
. At this point, you should update your adapter (e.g. remove the item) and call related Adapter#notify event.
Summary
Constants | |
---|---|
static Int | |
static Int |
Public constructors | |
---|---|
<init>() This class is the contract between ItemTouchHelper and your application. |
Public methods | |
---|---|
open Boolean |
canDropOver(@NonNull recyclerView: RecyclerView, @NonNull current: RecyclerView.ViewHolder, @NonNull target: RecyclerView.ViewHolder) Return true if the current ViewHolder can be dropped over the the target ViewHolder. |
open RecyclerView.ViewHolder! |
chooseDropTarget(@NonNull selected: RecyclerView.ViewHolder, @NonNull dropTargets: MutableList<RecyclerView.ViewHolder!>, curX: Int, curY: Int) Called by ItemTouchHelper to select a drop target from the list of ViewHolders that are under the dragged View. |
open Unit |
clearView(@NonNull recyclerView: RecyclerView, @NonNull viewHolder: RecyclerView.ViewHolder) Called by the ItemTouchHelper when the user interaction with an element is over and it also completed its animation. |
open Int |
convertToAbsoluteDirection(flags: Int, layoutDirection: Int) Converts a given set of flags to absolution direction which means |
open static Int |
convertToRelativeDirection(flags: Int, layoutDirection: Int) Replaces a movement direction with its relative version by taking layout direction into account. |
open Long |
getAnimationDuration(@NonNull recyclerView: RecyclerView, animationType: Int, animateDx: Float, animateDy: Float) Called by the ItemTouchHelper when user action finished on a ViewHolder and now the View will be animated to its final position. |
open Int |
When finding views under a dragged view, by default, ItemTouchHelper searches for views that overlap with the dragged View. |
open static ItemTouchUIUtil |
Returns the |
open Float |
getMoveThreshold(@NonNull viewHolder: RecyclerView.ViewHolder) Returns the fraction that the user should move the View to be considered as it is dragged. |
abstract Int |
getMovementFlags(@NonNull recyclerView: RecyclerView, @NonNull viewHolder: RecyclerView.ViewHolder) Should return a composite flag which defines the enabled move directions in each state (idle, swiping, dragging). |
open Float |
getSwipeEscapeVelocity(defaultValue: Float) Defines the minimum velocity which will be considered as a swipe action by the user. |
open Float |
getSwipeThreshold(@NonNull viewHolder: RecyclerView.ViewHolder) Returns the fraction that the user should move the View to be considered as swiped. |
open Float |
getSwipeVelocityThreshold(defaultValue: Float) Defines the maximum velocity ItemTouchHelper will ever calculate for pointer movements. |
open Int |
interpolateOutOfBoundsScroll(@NonNull recyclerView: RecyclerView, viewSize: Int, viewSizeOutOfBounds: Int, totalSize: Int, msSinceStartScroll: Long) Called by the ItemTouchHelper when user is dragging a view out of bounds. |
open Boolean |
Returns whether ItemTouchHelper should start a swipe operation if a pointer is swiped over the View. |
open Boolean |
Returns whether ItemTouchHelper should start a drag and drop operation if an item is long pressed. |
open static Int |
Shifts the given direction flags to the offset of the given action state. |
open static Int |
makeMovementFlags(dragFlags: Int, swipeFlags: Int) Convenience method to create movement flags. |
open Unit |
onChildDraw(@NonNull c: Canvas, @NonNull recyclerView: RecyclerView, @NonNull viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) Called by ItemTouchHelper on RecyclerView's onDraw callback. |
open Unit |
onChildDrawOver(@NonNull c: Canvas, @NonNull recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder!, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) Called by ItemTouchHelper on RecyclerView's onDraw callback. |
abstract Boolean |
onMove(@NonNull recyclerView: RecyclerView, @NonNull viewHolder: RecyclerView.ViewHolder, @NonNull target: RecyclerView.ViewHolder) Called when ItemTouchHelper wants to move the dragged item from its old position to the new position. |
open Unit |
onMoved(@NonNull recyclerView: RecyclerView, @NonNull viewHolder: RecyclerView.ViewHolder, fromPos: |