java.lang.Object | |
↳ | android.support.v7.widget.helper.ItemTouchHelper.Callback |
Known Direct Subclasses |
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.
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | DEFAULT_DRAG_ANIMATION_DURATION | ||||||||||
int | DEFAULT_SWIPE_ANIMATION_DURATION |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Return true if the current ViewHolder can be dropped over the the target ViewHolder.
| |||||||||||
Called by ItemTouchHelper to select a drop target from the list of ViewHolders that
are under the dragged View.
| |||||||||||
Called by the ItemTouchHelper when the user interaction with an element is over and it
also completed its animation.
| |||||||||||
Replaces a movement direction with its relative version by taking layout direction into
account.
| |||||||||||
Called by the ItemTouchHelper when user action finished on a ViewHolder and now the View
will be animated to its final position.
| |||||||||||
When finding views under a dragged view, by default, ItemTouchHelper searches for views
that overlap with the dragged View.
| |||||||||||
Returns the
ItemTouchUIUtil that is used by the ItemTouchHelper.Callback class for visual
changes on Views in response to user interactions.
| |||||||||||
Returns the fraction that the user should move the View to be considered as it is
dragged.
| |||||||||||
Should return a composite flag which defines the enabled move directions in each state
(idle, swiping, dragging).
| |||||||||||
Returns the fraction that the user should move the View to be considered as swiped.
| |||||||||||
Called by the ItemTouchHelper when user is dragging a view out of bounds.
| |||||||||||
Returns whether ItemTouchHelper should start a swipe operation if a pointer is swiped
over the View.
| |||||||||||
Returns whether ItemTouchHelper should start a drag and drop operation if an item is
long pressed.
| |||||||||||
Shifts the given direction flags to the offset of the given action state.
| |||||||||||
Convenience method to create movement flags.
| |||||||||||
Called by ItemTouchHelper on RecyclerView's onDraw callback.
| |||||||||||
Called by ItemTouchHelper on RecyclerView's onDraw callback.
| |||||||||||
Called when ItemTouchHelper wants to move the dragged item from its old position to
the new position.
| |||||||||||
Called when
onMove(RecyclerView, ViewHolder, ViewHolder) returns true.
| |||||||||||
Called when the ViewHolder swiped or dragged by the ItemTouchHelper is changed.
| |||||||||||
Called when a ViewHolder is swiped by the user.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
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.
recyclerView | The RecyclerView to which ItemTouchHelper is attached to. |
---|---|
current | The ViewHolder that user is dragging. |
target | The ViewHolder which is below the dragged ViewHolder. |
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.
selected | The ViewHolder being dragged by the user. |
---|---|
dropTargets | The list of ViewHolder that are under the dragged View and candidate as a drop. |
curX | 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 | The updated top value of the dragged View after drag translations
are applied. This value does not include margins added by
RecyclerView.ItemDecoration s. |
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
onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, boolean)
.
recyclerView | The RecyclerView which is controlled by the ItemTouchHelper. |
---|---|
viewHolder | The View that was interacted by the user. |
Converts a given set of flags to absolution direction which means START
and
END
are replaced with LEFT
and RIGHT
depending on the layout
direction.
flags | The flag value that include any number of movement flags. |
---|---|
layoutDirection | The layout direction of the RecyclerView. |
Replaces a movement direction with its relative version by taking layout direction into account.
flags | The flag value that include any number of movement flags. |
---|---|
layoutDirection | The layout direction of the View. Can be obtained from
getLayoutDirection(android.view.View) . |
Called by the ItemTouchHelper when user action finished on a ViewHolder and now the View will be animated to its final position.
Default implementation uses ItemAnimator's duration values. If
animationType
is ANIMATION_TYPE_DRAG
, it returns
getMoveDuration()
, otherwise, it returns
getRemoveDuration()
. If RecyclerView does not have
any RecyclerView.ItemAnimator
attached, this method returns
DEFAULT_DRAG_ANIMATION_DURATION
or DEFAULT_SWIPE_ANIMATION_DURATION
depending on the animation type.
recyclerView | The RecyclerView to which the ItemTouchHelper is attached to. |
---|---|
animationType | The type of animation. Is one of ANIMATION_TYPE_DRAG ,
ANIMATION_TYPE_SWIPE_CANCEL or
ANIMATION_TYPE_SWIPE_SUCCESS . |
animateDx | The horizontal distance that the animation will offset |
animateDy | The vertical distance that the animation will offset |
When finding views under a dragged view, by default, ItemTouchHelper searches for views that overlap with the dragged View. By overriding this method, you can extend or shrink the search box.
Returns the ItemTouchUIUtil
that is used by the ItemTouchHelper.Callback
class for visual
changes on Views in response to user interactions. ItemTouchUIUtil
has different
implementations for different platform versions.
By default, ItemTouchHelper.Callback
applies these changes on
itemView
.
For example, if you have a use case where you only want the text to move when user swipes over the view, you can do the following:
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder){ getDefaultUIUtil().clearView(((ItemTouchViewHolder) viewHolder).textView); } public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { if (viewHolder != null){ getDefaultUIUtil().onSelected(((ItemTouchViewHolder) viewHolder).textView); } } public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { getDefaultUIUtil().onDraw(c, recyclerView, ((ItemTouchViewHolder) viewHolder).textView, dX, dY, actionState, isCurrentlyActive); return true; } public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { getDefaultUIUtil().onDrawOver(c, recyclerView, ((ItemTouchViewHolder) viewHolder).textView, dX, dY, actionState, isCurrentlyActive); return true; }
ItemTouchUIUtil
instance that is used by the ItemTouchHelper.Callback
Returns the fraction that the user should move the View to be considered as it is dragged. After a view is moved this amount, ItemTouchHelper starts checking for Views below it for a possible drop.
viewHolder | The ViewHolder that is being dragged. |
---|
Should return a composite flag which defines the enabled move directions in each state (idle, swiping, dragging).
Instead of composing this flag manually, you can use makeMovementFlags(int, int)
or makeFlag(int, int)
.
This flag is composed of 3 sets of 8 bits, where first 8 bits are for IDLE state, next
8 bits are for SWIPE state and third 8 bits are for DRAG state.
Each 8 bit sections can be constructed by simply OR'ing direction flags defined in
ItemTouchHelper
.
For example, if you want it to allow swiping LEFT and RIGHT but only allow starting to swipe by swiping RIGHT, you can return:
makeFlag(ACTION_STATE_IDLE, RIGHT) | makeFlag(ACTION_STATE_SWIPE, LEFT | RIGHT);This means, allow right movement while IDLE and allow right and left movement while swiping.
recyclerView | The RecyclerView to which ItemTouchHelper is attached. |
---|---|
viewHolder | The ViewHolder for which the movement information is necessary. |
Returns the fraction that the user should move the View to be considered as swiped. The fraction is calculated with respect to RecyclerView's bounds.
Default value is .5f, which means, to swipe a View, user must move the View at least half of RecyclerView's width or height, depending on the swipe direction.
viewHolder | The ViewHolder that is being dragged. |
---|
Called by the ItemTouchHelper when user is dragging a view out of bounds.
You can override this method to decide how much RecyclerView should scroll in response to this action. Default implementation calculates a value based on the amount of View out of bounds and the time it spent there. The longer user keeps the View out of bounds, the faster the list will scroll. Similarly, the larger portion of the View is out of bounds, the faster the RecyclerView will scroll.
recyclerView | The RecyclerView instance to which ItemTouchHelper is attached to. |
---|---|
viewSize | The total size of the View in scroll direction, excluding item decorations. |
viewSizeOutOfBounds | The total size of the View that is out of bounds. This value is negative if the View is dragged towards left or top edge. |
totalSize | The total size of RecyclerView in the scroll direction. |
msSinceStartScroll | The time passed since View is kept out of bounds. |
scrollBy(int, int)
method.
Returns whether ItemTouchHelper should start a swipe operation if a pointer is swiped over the View.
Default value returns true but you may want to disable this if you want to start
swiping on a custom view touch using startSwipe(ViewHolder)
.
true
.Returns whether ItemTouchHelper should start a drag and drop operation if an item is long pressed.
Default value returns true but you may want to disable this if you want to start
dragging on a custom view touch using startDrag(ViewHolder)
.
true
.Shifts the given direction flags to the offset of the given action state.
actionState | The action state you want to get flags in. Should be one of
ACTION_STATE_IDLE , ACTION_STATE_SWIPE or
ACTION_STATE_DRAG . |
---|---|
directions | The direction flags. Can be composed from UP , DOWN ,
RIGHT , LEFT START and END . |
Convenience method to create movement flags.
For instance, if you want to let your items be drag & dropped vertically and swiped
left to be dismissed, you can call this method with:
makeMovementFlags(UP | DOWN, LEFT);
dragFlags | The directions in which the item can be dragged. |
---|---|
swipeFlags | The directions in which the item can be swiped. |
Called by ItemTouchHelper on RecyclerView's onDraw callback.
If you would like to customize how your View's respond to user interactions, this is a good place to override.
Default implementation translates the child by the given dX
,
dY
.
ItemTouchHelper also takes care of drawing the child after other children if it is being
dragged. This is done using child re-ordering mechanism. On platforms prior to L, this
is
achieved via getChildDrawingOrder(int, int)
and on L
and after, it changes View's elevation value to be greater than all other children.)
c | The canvas which RecyclerView is drawing its children |
---|---|
recyclerView | The RecyclerView to which ItemTouchHelper is attached to |
viewHolder | The ViewHolder which is being interacted by the User or it was interacted and simply animating to its original position |
dX | The amount of horizontal displacement caused by user's action |
dY | The amount of vertical displacement caused by user's action |
actionState | The type of interaction on the View. Is either ACTION_STATE_DRAG or ACTION_STATE_SWIPE . |
isCurrentlyActive | True if this view is currently being controlled by the user or false it is simply animating back to its original state. |
Called by ItemTouchHelper on RecyclerView's onDraw callback.
If you would like to customize how your View's respond to user interactions, this is a good place to override.
Default implementation translates the child by the given dX
,
dY
.
ItemTouchHelper also takes care of drawing the child after other children if it is being
dragged. This is done using child re-ordering mechanism. On platforms prior to L, this
is
achieved via getChildDrawingOrder(int, int)
and on L
and after, it changes View's elevation value to be greater than all other children.)
c | The canvas which RecyclerView is drawing its children |
---|---|
recyclerView | The RecyclerView to which ItemTouchHelper is attached to |
viewHolder | The ViewHolder which is being interacted by the User or it was interacted and simply animating to its original position |
dX | The amount of horizontal displacement caused by user's action |
dY | The amount of vertical displacement caused by user's action |
actionState | The type of interaction on the View. Is either ACTION_STATE_DRAG or ACTION_STATE_SWIPE . |
isCurrentlyActive | True if this view is currently being controlled by the user or false it is simply animating back to its original state. |
Called when ItemTouchHelper wants to move the dragged item from its old position to the new position.
If this method returns true, ItemTouchHelper assumes viewHolder
has been moved
to the adapter position of target
ViewHolder
(ViewHolder#getAdapterPosition()
).
If you don't support drag & drop, this method will never be called.
recyclerView | The RecyclerView to which ItemTouchHelper is attached to. |
---|---|
viewHolder | The ViewHolder which is being dragged by the user. |
target | The ViewHolder over which the currently active item is being dragged. |
viewHolder
has been moved to the adapter position of
target
.Called when onMove(RecyclerView, ViewHolder, ViewHolder)
returns true.
ItemTouchHelper does not create an extra Bitmap or View while dragging, instead, it modifies the existing View. Because of this reason, it is important that the View is still part of the layout after it is moved. This may not work as intended when swapped Views are close to RecyclerView bounds or there are gaps between them (e.g. other Views which were not eligible for dropping over).
This method is responsible to give necessary hint to the LayoutManager so that it will
keep the View in visible area. For example, for LinearLayoutManager, this is as simple
as calling scrollToPositionWithOffset(int, int)
.
Default implementation calls scrollToPosition(int)
if the View's
new position is likely to be out of bounds.
It is important to ensure the ViewHolder will stay visible as otherwise, it might be removed by the LayoutManager if the move causes the View to go out of bounds. In that case, drag will end prematurely.
recyclerView | The RecyclerView controlled by the ItemTouchHelper. |
---|---|
viewHolder | The ViewHolder under user's control. |
fromPos | The previous adapter position of the dragged item (before it was moved). |
target | The ViewHolder on which the currently active item has been dropped. |
toPos | The new adapter position of the dragged item. |
x | The updated left value of the dragged View after drag translations
are applied. This value does not include margins added by
RecyclerView.ItemDecoration s. |
y | The updated top value of the dragged View after drag translations
are applied. This value does not include margins added by
RecyclerView.ItemDecoration s.
|
Called when the ViewHolder swiped or dragged by the ItemTouchHelper is changed.
If you override this method, you should call super.viewHolder | The new ViewHolder that is being swiped or dragged. Might be null if it is cleared. |
---|---|
actionState | One of ACTION_STATE_IDLE ,
ACTION_STATE_SWIPE or
ACTION_STATE_DRAG . |
Called when a ViewHolder is swiped by the user.
If you are returning relative directions (START
, END
) from the
getMovementFlags(RecyclerView, ViewHolder)
method, this method
will also use relative directions. Otherwise, it will use absolute directions.
If you don't support swiping, this method will never be called.
ItemTouchHelper will keep a reference to the View until it is detached from
RecyclerView.
As soon as it is detached, ItemTouchHelper will call
clearView(RecyclerView, ViewHolder)
.
viewHolder | The ViewHolder which has been swiped by the user. |
---|---|
direction | The direction to which the ViewHolder is swiped. It is one of
UP , DOWN ,
LEFT or RIGHT . If your
getMovementFlags(RecyclerView, ViewHolder)
method
returned relative flags instead of LEFT / RIGHT ;
`direction` will be relative as well. (START or END ).
|