ItemTouchHelper.SimpleCallback


public abstract class ItemTouchHelper.SimpleCallback extends 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(int dragDirs, int swipeDirs)

Creates a Callback for the given drag and swipe allowance.

Public methods

int
getDragDirs(
    @NonNull RecyclerView recyclerView,
    @NonNull RecyclerView.ViewHolder viewHolder
)

Returns the drag directions for the provided ViewHolder.

int
getMovementFlags(
    @NonNull RecyclerView recyclerView,
    @NonNull RecyclerView.ViewHolder viewHolder
)

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

int
getSwipeDirs(
    @NonNull RecyclerView recyclerView,
    @NonNull RecyclerView.ViewHolder viewHolder
)

Returns the swipe directions for the provided ViewHolder.

void
setDefaultDragDirs(int defaultDragDirs)

Updates the default drag directions.

void
setDefaultSwipeDirs(int defaultSwipeDirs)

Updates the default swipe directions.

Inherited methods

From androidx.recyclerview.widget.ItemTouchHelper.Callback
boolean
canDropOver(
    @NonNull RecyclerView recyclerView,
    @NonNull RecyclerView.ViewHolder current,
    @NonNull RecyclerView.ViewHolder target
)

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

RecyclerView.ViewHolder
chooseDropTarget(
    @NonNull RecyclerView.ViewHolder selected,
    @NonNull 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(
    @NonNull RecyclerView recyclerView,
    @NonNull 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 START and END are replaced with LEFT and RIGHT depending on the layout direction.

static int
convertToRelativeDirection(int flags, int layoutDirection)

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

long
getAnimationDuration(
    @NonNull 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

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

static @NonNull 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
getSwipeEscapeVelocity(float defaultValue)

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
getSwipeVelocityThreshold(float defaultValue)

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

int
interpolateOutOfBoundsScroll(
    @NonNull RecyclerView recyclerView,
    int viewSize,
    int viewSizeOutOfBounds,
    int totalSize,
    long msSinceStartScroll
)

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.

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(
    @NonNull Canvas c,
    @NonNull RecyclerView recyclerView,
    @NonNull RecyclerView.ViewHolder viewHolder,
    float dX,
    float dY,
    int actionState,
    boolean isCurrentlyActive
)

Called by ItemTouchHelper on RecyclerView's onDraw callback.

void
onChildDrawOver(
    @NonNull Canvas c,
    @NonNull RecyclerView recyclerView,
    RecyclerView.ViewHolder viewHolder,
    float dX,
    float dY,
    int actionState,
    boolean isCurrentlyActive
)

Called by ItemTouchHelper on RecyclerView's onDraw callback.

abstract boolean
onMove(
    @NonNull RecyclerView recyclerView,
    @NonNull RecyclerView.ViewHolder viewHolder,
    @NonNull RecyclerView.ViewHolder target
)

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

void
onMoved(
    @NonNull RecyclerView recyclerView,
    @NonNull RecyclerView.ViewHolder viewHolder,
    int fromPos,
    @NonNull RecyclerView.ViewHolder target,
    int toPos,
    int x,
    int y
)

Called when onMove returns true.

void
onSelectedChanged(
    @Nullable RecyclerView.ViewHolder viewHolder,
    int actionState
)

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

abstract void
onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction)

Called when a ViewHolder is swiped by the user.

Public constructors

SimpleCallback

Added in 1.0.0
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 getSwipeDirs and / or getDragDirs.

Parameters
int dragDirs

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

int swipeDirs

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

Public methods

getDragDirs

Added in 1.0.0
public int getDragDirs(
    @NonNull RecyclerView recyclerView,
    @NonNull RecyclerView.ViewHolder viewHolder
)

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

Parameters
@NonNull RecyclerView recyclerView

The RecyclerView to which the ItemTouchHelper is attached to.

@NonNull RecyclerView.ViewHolder viewHolder

The ViewHolder for which the swipe direction is queried.

Returns
int

A binary OR of direction flags.

getMovementFlags

Added in 1.4.0-rc01
public int getMovementFlags(
    @NonNull RecyclerView recyclerView,
    @NonNull 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 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
@NonNull RecyclerView recyclerView

The RecyclerView to which ItemTouchHelper is attached.

@NonNull RecyclerView.ViewHolder 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
public int getSwipeDirs(
    @NonNull RecyclerView recyclerView,
    @NonNull RecyclerView.ViewHolder viewHolder
)

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

Parameters
@NonNull RecyclerView recyclerView

The RecyclerView to which the ItemTouchHelper is attached to.

@NonNull RecyclerView.ViewHolder viewHolder

The ViewHolder for which the swipe direction is queried.

Returns
int

A binary OR of direction flags.

setDefaultDragDirs

Added in 1.0.0
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
int defaultDragDirs

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

setDefaultSwipeDirs

Added in 1.0.0
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
int defaultSwipeDirs

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