ItemTouchHelper.SimpleCallback

abstract class ItemTouchHelper.SimpleCallback : ItemTouchHelper.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. You should still override onMove or onSwiped depending on your use case.

ItemTouchHelper mIth = new ItemTouchHelper(
    new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,
        ItemTouchHelper.LEFT) {
        public boolean onMove(RecyclerView recyclerView,
            ViewHolder viewHolder, ViewHolder target) {
            final int fromPos = viewHolder.getAdapterPosition();
            final int toPos = target.getAdapterPosition();
            // move item in `fromPos` to `toPos` in adapter.
            return true;// true if moved, false otherwise
        }
        public void onSwiped(ViewHolder viewHolder, int direction) {
            // remove from adapter
        }
});

Summary

Public constructors

SimpleCallback(dragDirs: Int, swipeDirs: Int)

Creates a Callback for the given drag and swipe allowance.

Public functions

Int
getDragDirs(
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder
)

Returns the drag directions for the provided ViewHolder.

Int
getMovementFlags(
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder
)

Should return a composite flag which defines the enabled move directions in each state (idle, swiping, dragging).

Int
getSwipeDirs(
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder
)

Returns the swipe directions for the provided ViewHolder.

Unit
setDefaultDragDirs(defaultDragDirs: Int)

Updates the default drag directions.

Unit
setDefaultSwipeDirs(defaultSwipeDirs: Int)

Updates the default swipe directions.

Inherited functions

From androidx.recyclerview.widget.ItemTouchHelper.Callback
Boolean
canDropOver(
    recyclerView: RecyclerView,
    current: RecyclerView.ViewHolder,
    target: RecyclerView.ViewHolder
)

Return true if the current ViewHolder can be dropped over the the target ViewHolder.

RecyclerView.ViewHolder!
chooseDropTarget(
    selected: RecyclerView.ViewHolder,
    dropTargets: (Mutable)List<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.

Unit
clearView(
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder
)

Called by the ItemTouchHelper when the user interaction with an element is over and it also completed its animation.

Int
convertToAbsoluteDirection(flags: Int, layoutDirection: Int)

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.

java-static Int
convertToRelativeDirection(flags: Int, layoutDirection: Int)

Replaces a movement direction with its relative version by taking layout direction into account.

Long
getAnimationDuration(
    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.

Int

When finding views under a dragged view, by default, ItemTouchHelper searches for views that overlap with the dragged View.

java-static ItemTouchUIUtil

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

Float

Returns the fraction that the user should move the View to be considered as it is dragged.

Float

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

Float

Returns the fraction that the user should move the View to be considered as swiped.

Float

Defines the maximum velocity ItemTouchHelper will ever calculate for pointer movements.

Int
interpolateOutOfBoundsScroll(
    recyclerView: RecyclerView,
    viewSize: Int,
    viewSizeOutOfBounds: Int,
    totalSize: Int,
    msSinceStartScroll: Long
)

Called by the ItemTouchHelper when user is dragging a view out of bounds.

Boolean

Returns whether ItemTouchHelper should start a swipe operation if a pointer is swiped over the View.

Boolean

Returns whether ItemTouchHelper should start a drag and drop operation if an item is long pressed.

java-static Int
makeFlag(actionState: Int, directions: Int)

Shifts the given direction flags to the offset of the given action state.

java-static Int
makeMovementFlags(dragFlags: Int, swipeFlags: Int)

Convenience method to create movement flags.

Unit
onChildDraw(
    c: Canvas,
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder,
    dX: Float,
    dY: Float,
    actionState: Int,
    isCurrentlyActive: Boolean
)

Called by ItemTouchHelper on RecyclerView's onDraw callback.

Unit
onChildDrawOver(
    c: Canvas,
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder!,
    dX: Float,
    dY: Float,
    actionState: Int,
    isCurrentlyActive: Boolean
)

Called by ItemTouchHelper on RecyclerView's onDraw callback.

abstract Boolean
onMove(
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder,
    target: RecyclerView.ViewHolder
)

Called when ItemTouchHelper wants to move the dragged item from its old position to the new position.

Unit
onMoved(
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder,
    fromPos: Int,
    target: RecyclerView.ViewHolder,
    toPos: Int,
    x: Int,
    y: Int
)

Called when onMove returns true.

Unit
onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int)

Called when the ViewHolder swiped or dragged by the ItemTouchHelper is changed.

abstract Unit
onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int)

Called when a ViewHolder is swiped by the user.

Public constructors

SimpleCallback

Added in 1.0.0
SimpleCallback(dragDirs: Int, swipeDirs: Int)

Creates a Callback for the given drag and swipe allowance. These values serve as defaults and if you want to customize behavior per ViewHolder, you can override getSwipeDirs and / or getDragDirs.

Parameters
dragDirs: Int

Binary OR of direction flags in which the Views can be dragged. Must be composed of LEFT, RIGHT, START, END, UP and DOWN.

swipeDirs: Int

Binary OR of direction flags in which the Views can be swiped. Must be composed of LEFT, RIGHT, START, END, UP and DOWN.

Public functions

getDragDirs

Added in 1.0.0
fun getDragDirs(
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder
): Int

Returns the drag directions for the provided ViewHolder. Default implementation returns the drag directions that was set via constructor or setDefaultDragDirs.

Parameters
recyclerView: RecyclerView

The RecyclerView to which the ItemTouchHelper is attached to.

viewHolder: RecyclerView.ViewHolder

The ViewHolder for which the swipe direction is queried.

Returns
Int

A binary OR of direction flags.

getMovementFlags

Added in 1.4.0-alpha01
fun getMovementFlags(
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder
): Int

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 or makeFlag.

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

The RecyclerView to which ItemTouchHelper is attached.

viewHolder: RecyclerView.ViewHolder

The ViewHolder for which the movement information is necessary.

Returns
Int

flags specifying which movements are allowed on this ViewHolder.

getSwipeDirs

Added in 1.0.0
fun getSwipeDirs(
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder
): Int

Returns the swipe directions for the provided ViewHolder. Default implementation returns the swipe directions that was set via constructor or setDefaultSwipeDirs.

Parameters
recyclerView: RecyclerView

The RecyclerView to which the ItemTouchHelper is attached to.

viewHolder: RecyclerView.ViewHolder

The ViewHolder for which the swipe direction is queried.

Returns
Int

A binary OR of direction flags.

setDefaultDragDirs

Added in 1.0.0
fun setDefaultDragDirs(defaultDragDirs: Int): Unit

Updates the default drag directions. For example, you can use this method to toggle certain directions depending on your use case.

Parameters
defaultDragDirs: Int

Binary OR of directions in which the ViewHolders can be dragged.

setDefaultSwipeDirs

Added in 1.0.0
fun setDefaultSwipeDirs(defaultSwipeDirs: Int): Unit

Updates the default swipe directions. For example, you can use this method to toggle certain directions depending on your use case.

Parameters
defaultSwipeDirs: Int

Binary OR of directions in which the ViewHolders can be swiped.