ItemTouchHelper

public class ItemTouchHelper
extends RecyclerView.ItemDecoration implements RecyclerView.OnChildAttachStateChangeListener

java.lang.Object
   ↳ androidx.recyclerview.widget.RecyclerView.ItemDecoration
     ↳ androidx.recyclerview.widget.ItemTouchHelper


This is a utility class to add swipe to dismiss and drag & drop support to RecyclerView.

It works with a RecyclerView and a Callback class, which configures what type of interactions are enabled and also receives events when user performs these actions.

Depending on which functionality you support, you should override ItemTouchHelper.Callback.onMove(RecyclerView, ViewHolder, ViewHolder) and / or ItemTouchHelper.Callback.onSwiped(ViewHolder, int).

This class is designed to work with any LayoutManager but for certain situations, it can be optimized for your custom LayoutManager by extending methods in the ItemTouchHelper.Callback class or implementing ItemTouchHelper.ViewDropHandler interface in your LayoutManager.

By default, ItemTouchHelper moves the items' translateX/Y properties to reposition them. You can customize these behaviors by overriding ItemTouchHelper.Callback.onChildDraw(Canvas, RecyclerView, ViewHolder, float, float, int, boolean) or ItemTouchHelper.Callback.onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, boolean).

Most of the time you only need to override onChildDraw.

Summary

Nested classes

class ItemTouchHelper.Callback

This class is the contract between ItemTouchHelper and your application. 

class ItemTouchHelper.SimpleCallback

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

interface ItemTouchHelper.ViewDropHandler

An interface which can be implemented by LayoutManager for better integration with ItemTouchHelper

Constants

int ACTION_STATE_DRAG

A View is currently being dragged.

int ACTION_STATE_IDLE

ItemTouchHelper is in idle state.

int ACTION_STATE_SWIPE

A View is currently being swiped.

int ANIMATION_TYPE_DRAG

Animation type for views that were dragged and now will animate to their final position.

int ANIMATION_TYPE_SWIPE_CANCEL

Animation type for views which are not completely swiped thus will animate back to their original position.

int ANIMATION_TYPE_SWIPE_SUCCESS

Animation type for views which are swiped successfully.

int DOWN

Down direction, used for swipe & drag control.

int END

Horizontal end direction.

int LEFT

Left direction, used for swipe & drag control.

int RIGHT

Right direction, used for swipe & drag control.

int START

Horizontal start direction.

int UP

Up direction, used for swipe & drag control.

Public constructors

ItemTouchHelper(ItemTouchHelper.Callback callback)

Creates an ItemTouchHelper that will work with the given Callback.

Public methods

void attachToRecyclerView(RecyclerView recyclerView)

Attaches the ItemTouchHelper to the provided RecyclerView.

void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)

Retrieve any offsets for the given item.

void onChildViewAttachedToWindow(View view)

Called when a view is attached to the RecyclerView.

void onChildViewDetachedFromWindow(View view)

Called when a view is detached from RecyclerView.

void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)

Draw any appropriate decorations into the Canvas supplied to the RecyclerView.

void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)

Draw any appropriate decorations into the Canvas supplied to the RecyclerView.

void startDrag(RecyclerView.ViewHolder viewHolder)

Starts dragging the provided ViewHolder.

void startSwipe(RecyclerView.ViewHolder viewHolder)

Starts swiping the provided ViewHolder.

Inherited methods