Most visited

Recently visited

ItemTouchHelper.SimpleCallback

public static abstract class ItemTouchHelper.SimpleCallback
extends ItemTouchHelper.Callback

java.lang.Object
   ↳ android.support.v7.widget.helper.ItemTouchHelper.Callback
     ↳ android.support.v7.widget.helper.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. 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 abstract boolean onMove(RecyclerView recyclerView,
             ViewHolder viewHolder, ViewHolder target) {
             final int fromPos = viewHolder.getAdapterPosition();
             final int toPos = viewHolder.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

Inherited constants

From class android.support.v7.widget.helper.ItemTouchHelper.Callback

Public constructors

ItemTouchHelper.SimpleCallback(int dragDirs, int swipeDirs)

Creates a Callback for the given drag and swipe allowance.

Public methods

int getDragDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)

Returns the drag directions for the provided ViewHolder.

int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)

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

int getSwipeDirs(RecyclerView recyclerView, 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 class android.support.v7.widget.helper.ItemTouchHelper.Callback
From class java.lang.Object

Public constructors

ItemTouchHelper.SimpleCallback

ItemTouchHelper.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(RecyclerView, ViewHolder) and / or getDragDirs(RecyclerView, ViewHolder).

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 methods

getDragDirs

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 setDefaultDragDirs(int).

Parameters
recyclerView RecyclerView: The RecyclerView to which the ItemTouchHelper is attached to.
viewHolder RecyclerView.ViewHolder: The RecyclerView for which the swipe drection is queried.
Returns
int A binary OR of direction flags.

getMovementFlags

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 makeMovementFlags(int, int) or 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 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

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 setDefaultSwipeDirs(int).

Parameters
recyclerView RecyclerView: The RecyclerView to which the ItemTouchHelper is attached to.
viewHolder RecyclerView.ViewHolder: The RecyclerView for which the swipe drection is queried.
Returns
int A binary OR of direction flags.

setDefaultDragDirs

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 int: Binary OR of directions in which the ViewHolders can be dragged.

setDefaultSwipeDirs

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 int: Binary OR of directions in which the ViewHolders can be swiped.

Hooray!