belongs to Maven artifact com.android.support:recyclerview-v7:28.0.0-alpha1
ItemTouchHelper.Callback
public
static
abstract
class
ItemTouchHelper.Callback
extends Object
java.lang.Object | |
↳ | android.support.v7.widget.helper.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
ItemTouchHelper.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(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 | |
---|---|
int |
DEFAULT_DRAG_ANIMATION_DURATION
|
int |
DEFAULT_SWIPE_ANIMATION_DURATION
|
Public constructors | |
---|---|
ItemTouchHelper.Callback()
|
Public methods | |
---|---|
boolean
|
canDropOver(RecyclerView recyclerView, RecyclerView.ViewHolder current, RecyclerView.ViewHolder target)
Return true if the current ViewHolder can be dropped over the the target ViewHolder. |
RecyclerView.ViewHolder
|
chooseDropTarget(RecyclerView.ViewHolder selected, List<RecyclerView.ViewHolder> dropTargets, int curX, int curY)
Called by ItemTouchHelper to select a drop target from the list of ViewHolders that are under the dragged View. |
void
|
clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)
Called by the ItemTouchHelper when the user interaction with an element is over and it also completed its animation. |
int
|
convertToAbsoluteDirection(int flags, int layoutDirection)
Converts a given set of flags to absolution direction which means |
static
int
|
convertToRelativeDirection(int flags, int layoutDirection)
Replaces a movement direction with its relative version by taking layout direction into account. |
long
|
getAnimationDuration(RecyclerView recyclerView, int animationType, float animateDx, float animateDy)
Called by the ItemTouchHelper when user action finished on a ViewHolder and now the View will be animated to its final position. |
int
|
getBoundingBoxMargin()
When finding views under a dragged view, by default, ItemTouchHelper searches for views that overlap with the dragged View. |
static
ItemTouchUIUtil
|
getDefaultUIUtil()
Returns the |
float
|
getMoveThreshold(RecyclerView.ViewHolder viewHolder)
Returns the fraction that the user should move the View to be considered as it is dragged. |
abstract
int
|
getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)
Should return a composite flag which defines the enabled move directions in each state (idle, swiping, dragging). |
float
|
getSwipeEscapeVelocity(float defaultValue)
Defines the minimum velocity which will be considered as a swipe action by the user. |
float
|
getSwipeThreshold(RecyclerView.ViewHolder viewHolder)
Returns the fraction that the user should move the View to be considered as swiped. |
float
|
getSwipeVelocityThreshold(float defaultValue)
Defines the maximum velocity ItemTouchHelper will ever calculate for pointer movements. |
int
|
interpolateOutOfBoundsScroll(RecyclerView recyclerView, int viewSize, int viewSizeOutOfBounds, int totalSize, long msSinceStartScroll)
Called by the ItemTouchHelper when user is dragging a view out of bounds. |
boolean
|
isItemViewSwipeEnabled()
Returns whether ItemTouchHelper should start a swipe operation if a pointer is swiped over the View. |
boolean
|
isLongPressDragEnabled()
Returns whether ItemTouchHelper should start a drag and drop operation if an item is long pressed. |
static
int
|
makeFlag(int actionState, int directions)
Shifts the given direction flags to the offset of the given action state. |
static
int
|
makeMovementFlags(int dragFlags, int swipeFlags)
Convenience method to create movement flags. |
void
|
onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive)
Called by ItemTouchHelper on RecyclerView's onDraw callback. |
void
|
onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive)
Called by ItemTouchHelper on RecyclerView's onDraw callback. |
abstract
boolean
|
onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target)
Called when ItemTouchHelper wants to move the dragged item from its old position to the new position. |
void
|
onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y)
Called when |
void
|
onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState)
Called when the ViewHolder swiped or dragged by the ItemTouchHelper is changed. |
abstract
void
|
onSwiped(RecyclerView.ViewHolder viewHolder, int direction)
Called when a ViewHolder is swiped by the user. |
Inherited methods | |
---|---|
![]()
java.lang.Object
|
Constants
DEFAULT_DRAG_ANIMATION_DURATION
int DEFAULT_DRAG_ANIMATION_DURATION
Constant Value: 200 (0x000000c8)
DEFAULT_SWIPE_ANIMATION_DURATION
int DEFAULT_SWIPE_ANIMATION_DURATION
Constant Value: 250 (0x000000fa)
Public constructors
Public methods
canDropOver
boolean canDropOver (RecyclerView recyclerView, RecyclerView.ViewHolder current, RecyclerView.ViewHolder target)
Return true if the current ViewHolder can be dropped over the the target ViewHolder.
This method is used when selecting drop target for the dragged View. After Views are
eliminated either via bounds check or via this method, resulting set of views will be
passed to chooseDropTarget(ViewHolder, java.util.List, int, int)
.
Default implementation returns true.
Parameters | |
---|---|
recyclerView |
RecyclerView : The RecyclerView to which ItemTouchHelper is attached to. |
current |
RecyclerView.ViewHolder : The ViewHolder that user is dragging. |
target |
RecyclerView.ViewHolder : The ViewHolder which is below the dragged ViewHolder. |
Returns | |
---|---|
boolean |
True if the dragged ViewHolder can be replaced with the target ViewHolder, false otherwise. |
chooseDropTarget
RecyclerView.ViewHolder chooseDropTarget (RecyclerView.ViewHolder selected, List<RecyclerView.ViewHolder> dropTargets, int curX, int curY)
Called by ItemTouchHelper to select a drop target from the list of ViewHolders that are under the dragged View.
Default implementation filters the View with which dragged item have changed position
in the drag direction. For instance, if the view is dragged UP, it compares the
view.getTop()
of the two views before and after drag started. If that value
is different, the target view passes the filter.
Among these Views which pass the test, the one closest to the dragged view is chosen.
This method is called on the main thread every time user moves the View. If you want to override it, make sure it does not do any expensive operations.
Parameters | |
---|---|
selected |
RecyclerView.ViewHolder : The ViewHolder being dragged by the user. |
dropTargets |
List : The list of ViewHolder that are under the dragged View and
candidate as a drop. |
curX |
int : The updated left value of the dragged View after drag translations
are applied. This value does not include margins added by
RecyclerView.ItemDecoration s. |
curY |
int : The updated top value of the dragged View after drag translations
are applied. This value does not include margins added by
RecyclerView.ItemDecoration s. |
Returns | |
---|---|
RecyclerView.ViewHolder |
A ViewHolder to whose position the dragged ViewHolder should be moved to. |
clearView
void clearView (RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)
Called by the ItemTouchHelper when the user interaction with an element is over and it also completed its animation.
This is a good place to clear all changes on the View that was done in
onSelectedChanged(RecyclerView.ViewHolder, int)
,
onChildDraw(Canvas, RecyclerView, ViewHolder, float, float, int, boolean)
or