Callback

A simple wrapper to the default Callback which you can construct with drag and swipe directions and this class will handle the flag callbacks.

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.

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

DEFAULT_DRAG_ANIMATION_DURATION

DEFAULT_SWIPE_ANIMATION_DURATION

Public constructors

<init>

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.

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.

chooseDropTarget

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.

getDefaultUIUtil

Returns the ItemTouchUIUtil that is used by the Callback class for visual changes on Views in response to user interactions. ItemTouchUIUtil has different implementations for different platform versions.

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:

getSwipeEscapeVelocity

Defines the minimum velocity which will be considered as a swipe action by the user.

You can increase this value to make it harder to swipe or decrease it to make it easier. Keep in mind that ItemTouchHelper also checks the perpendicular velocity and makes sure current direction velocity is larger then the perpendicular one. Otherwise, user's movement is ambiguous. You can change the threshold by overriding getSwipeVelocityThreshold(float).

The velocity is calculated in pixels per second.

The default framework value is passed as a parameter so that you can modify it with a multiplier.

Parameters

defaultValue

Float: The default value (in pixels per second) used by the ItemTouchHelper.

getSwipeVelocityThreshold

To consider a movement as swipe, ItemTouchHelper requires it to be larger than the perpendicular movement. If both directions reach to the max threshold, none of them will be considered as a swipe because it is usually an indication that user rather tried to scroll then swipe.

The velocity is calculated in pixels per second.

You can customize this behavior by changing this method. If you increase the value, it will be easier for the user to swipe diagonally and if you decrease the value, user will need to make a rather straight finger movement to trigger a swipe.

Parameters

defaultValue

Float: The default value(in pixels per second) used by the ItemTouchHelper.

interpolateOutOfBoundsScroll

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.

Parameters

recyclerView

RecyclerView: The RecyclerView instance to which ItemTouchHelper is attached to.

viewSize

RecyclerView: The total size of the View in scroll direction, excluding item decorations.

viewSizeOutOfBounds

RecyclerView: 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.

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 android.view.ViewGroup#getChildDrawingOrder(int, int) and on L and after, it changes View's elevation value to be greater than all other children.)

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 android.view.ViewGroup#getChildDrawingOrder(int, int) and on L and after, it changes View's elevation value to be greater than all other children.)

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).

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.