public abstract class

ItemTouchHelper.SimpleCallback

extends ItemTouchHelper.Callback

 java.lang.Object

androidx.recyclerview.widget.ItemTouchHelper.Callback

↳androidx.recyclerview.widget.ItemTouchHelper.SimpleCallback

Overview

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

Fields
from ItemTouchHelper.CallbackDEFAULT_DRAG_ANIMATION_DURATION, DEFAULT_SWIPE_ANIMATION_DURATION
Constructors
publicSimpleCallback(int dragDirs, int swipeDirs)

Creates a Callback for the given drag and swipe allowance.

Methods
public intgetDragDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)

Returns the drag directions for the provided ViewHolder.

public abstract intgetMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)

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

public intgetSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)

Returns the swipe directions for the provided ViewHolder.

public voidsetDefaultDragDirs(int defaultDragDirs)

Updates the default drag directions.

public voidsetDefaultSwipeDirs(int defaultSwipeDirs)

Updates the default swipe directions.

from ItemTouchHelper.CallbackcanDropOver, chooseDropTarget, clearView, convertToAbsoluteDirection, convertToRelativeDirection, getAnimationDuration, getBoundingBoxMargin, getDefaultUIUtil, getMoveThreshold, getSwipeEscapeVelocity, getSwipeThreshold, getSwipeVelocityThreshold, interpolateOutOfBoundsScroll, isItemViewSwipeEnabled, isLongPressDragEnabled, makeFlag, makeMovementFlags, onChildDraw, onChildDrawOver, onMove, onMoved, onSelectedChanged, onSwiped
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public SimpleCallback(int dragDirs, int swipeDirs)

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 ItemTouchHelper.SimpleCallback and / or ItemTouchHelper.SimpleCallback.

Parameters:

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

Methods

public void setDefaultSwipeDirs(int defaultSwipeDirs)

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

Parameters:

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

public void setDefaultDragDirs(int defaultDragDirs)

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

Parameters:

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

public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)

Returns the swipe directions for the provided ViewHolder. Default implementation returns the swipe directions that was set via constructor or ItemTouchHelper.SimpleCallback.setDefaultSwipeDirs(int).

Parameters:

recyclerView: The RecyclerView to which the ItemTouchHelper is attached to.
viewHolder: The ViewHolder for which the swipe direction is queried.

Returns:

A binary OR of direction flags.

public int getDragDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)

Returns the drag directions for the provided ViewHolder. Default implementation returns the drag directions that was set via constructor or ItemTouchHelper.SimpleCallback.setDefaultDragDirs(int).

Parameters:

recyclerView: The RecyclerView to which the ItemTouchHelper is attached to.
viewHolder: The ViewHolder for which the swipe direction is queried.

Returns:

A binary OR of direction flags.

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

Instead of composing this flag manually, you can use ItemTouchHelper.Callback.makeMovementFlags(int, int) or ItemTouchHelper.Callback.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.

Parameters:

recyclerView: The RecyclerView to which ItemTouchHelper is attached.
viewHolder: The ViewHolder for which the movement information is necessary.

Returns:

flags specifying which movements are allowed on this ViewHolder.

See also: ItemTouchHelper.Callback.makeMovementFlags(int, int), ItemTouchHelper.Callback.makeFlag(int, int)