belongs to Maven artifact com.android.support:recyclerview-v7:28.0.0-alpha1
ItemTouchHelper
  public
  
  
  
  class
  ItemTouchHelper
  
  
  
  
    extends RecyclerView.ItemDecoration
  
  
  
  
  
      implements
      
        RecyclerView.OnChildAttachStateChangeListener
      
  
  
| java.lang.Object | ||
| ↳ | android.support.v7.widget.RecyclerView.ItemDecoration | |
| ↳ | android.support.v7.widget.helper.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
 onMove(RecyclerView, ViewHolder, ViewHolder) and / or
 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 onChildDraw(Canvas, RecyclerView, ViewHolder, float, float, int, boolean)
 or onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, boolean).
 
onChildDraw.
Summary
| Nested classes | |
|---|---|
| 
        
        
        
        
        class | ItemTouchHelper.CallbackThis class is the contract between ItemTouchHelper and your application. | 
| 
        
        
        
        
        class | ItemTouchHelper.SimpleCallbackA 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.ViewDropHandlerAn interface which can be implemented by LayoutManager for better integration with
  | 
| Constants | |
|---|---|
| int | ACTION_STATE_DRAGA View is currently being dragged. | 
| int | ACTION_STATE_IDLEItemTouchHelper is in idle state. | 
| int | ACTION_STATE_SWIPEA View is currently being swiped. | 
| int | ANIMATION_TYPE_DRAGAnimation type for views that were dragged and now will animate to their final position. | 
| int | ANIMATION_TYPE_SWIPE_CANCELAnimation type for views which are not completely swiped thus will animate back to their original position. | 
| int | ANIMATION_TYPE_SWIPE_SUCCESSAnimation type for views which are swiped successfully. | 
| int | DOWNDown direction, used for swipe & drag control. | 
| int | ENDHorizontal end direction. | 
| int | LEFTLeft direction, used for swipe & drag control. | 
| int | RIGHTRight direction, used for swipe & drag control. | 
| int | STARTHorizontal start direction. | 
| int | UPUp 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 | |
|---|---|
|  From
class 
  
    android.support.v7.widget.RecyclerView.ItemDecoration
  
 | |
|  From
class 
  
    java.lang.Object
  
 | |
|  From
interface 
  
    android.support.v7.widget.RecyclerView.OnChildAttachStateChangeListener
  
 | |
Constants
ACTION_STATE_DRAG
int ACTION_STATE_DRAG
A View is currently being dragged.
Constant Value: 2 (0x00000002)
ACTION_STATE_IDLE
int ACTION_STATE_IDLE
ItemTouchHelper is in idle state. At this state, either there is no related motion event by the user or latest motion events have not yet triggered a swipe or drag.
Constant Value: 0 (0x00000000)
ACTION_STATE_SWIPE
int ACTION_STATE_SWIPE
A View is currently being swiped.
Constant Value: 1 (0x00000001)
ANIMATION_TYPE_DRAG
int ANIMATION_TYPE_DRAG
Animation type for views that were dragged and now will animate to their final position.
Constant Value: 8 (0x00000008)
ANIMATION_TYPE_SWIPE_CANCEL
int ANIMATION_TYPE_SWIPE_CANCEL
Animation type for views which are not completely swiped thus will animate back to their original position.
Constant Value: 4 (0x00000004)
ANIMATION_TYPE_SWIPE_SUCCESS
int ANIMATION_TYPE_SWIPE_SUCCESS
Animation type for views which are swiped successfully.
Constant Value: 2 (0x00000002)
DOWN
int DOWN
Down direction, used for swipe & drag control.
Constant Value: 2 (0x00000002)
END
int END
Horizontal end direction. Resolved to LEFT or RIGHT depending on RecyclerView's layout direction. Used for swipe & drag control.
Constant Value: 32 (0x00000020)
LEFT
int LEFT
Left direction, used for swipe & drag control.
Constant Value: 4 (0x00000004)
RIGHT
int RIGHT
Right direction, used for swipe & drag control.
Constant Value: 8 (0x00000008)
START
int START
Horizontal start direction. Resolved to LEFT or RIGHT depending on RecyclerView's layout direction. Used for swipe & drag control.
Constant Value: 16 (0x00000010)
UP
int UP
Up direction, used for swipe & drag control.
Constant Value: 1 (0x00000001)
Public constructors
ItemTouchHelper
ItemTouchHelper (ItemTouchHelper.Callback callback)
Creates an ItemTouchHelper that will work with the given Callback.
 You can attach ItemTouchHelper to a RecyclerView via
 attachToRecyclerView(RecyclerView). Upon attaching, it will add an item decoration,
 an onItemTouchListener and a Child attach / detach listener to the RecyclerView.
| Parameters | |
|---|---|
| callback | ItemTouchHelper.Callback: The Callback which controls the behavior of this touch helper. | 
Public methods
attachToRecyclerView
void attachToRecyclerView (RecyclerView recyclerView)
Attaches the ItemTouchHelper to the provided RecyclerView. If TouchHelper is already
 attached to a RecyclerView, it will first detach from the previous one. You can call this
 method with null to detach it from the current RecyclerView.
| Parameters | |
|---|---|
| recyclerView | RecyclerView: The RecyclerView instance to which you want to add this helper ornullif you want to remove ItemTouchHelper from the current
                     RecyclerView. | 
getItemOffsets
void getItemOffsets (Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
Retrieve any offsets for the given item. Each field of outRect specifies
 the number of pixels that the item view should be inset by, similar to padding or margin.
 The default implementation sets the bounds of outRect to 0 and returns.
 
 If this ItemDecoration does not affect the positioning of item views, it should set
 all four fields of outRect (left, top, right, bottom) to zero
 before returning.
 
 If you need to access Adapter for additional data, you can call
 getChildAdapterPosition(View) to get the adapter position of the
 View.
| Parameters | |
|---|---|
| outRect | Rect: Rect to receive the output. | 
| view | View: The child view to decorate | 
| parent | RecyclerView: RecyclerView this ItemDecoration is decorating | 
| state | RecyclerView.State: The current state of RecyclerView. | 
onChildViewAttachedToWindow
void onChildViewAttachedToWindow (View view)
Called when a view is attached to the RecyclerView.
| Parameters | |
|---|---|
| view | View: The View which is attached to the RecyclerView | 
onChildViewDetachedFromWindow
void onChildViewDetachedFromWindow (View view)
Called when a view is detached from RecyclerView.
| Parameters | |
|---|---|
| view | View: The View which is being detached from the RecyclerView | 
onDraw
void onDraw (Canvas c, RecyclerView parent, RecyclerView.State state)
Draw any appropriate decorations into the Canvas supplied to the RecyclerView. Any content drawn by this method will be drawn before the item views are drawn, and will thus appear underneath the views.
| Parameters | |
|---|---|
| c | Canvas: Canvas to draw into | 
| parent | RecyclerView: RecyclerView this ItemDecoration is drawing into | 
| state | RecyclerView.State: The current state of RecyclerView | 
onDrawOver
void onDrawOver (Canvas c, RecyclerView parent, RecyclerView.State state)
Draw any appropriate decorations into the Canvas supplied to the RecyclerView. Any content drawn by this method will be drawn after the item views are drawn and will thus appear over the views.
| Parameters | |
|---|---|
| c | Canvas: Canvas to draw into | 
| parent | RecyclerView: RecyclerView this ItemDecoration is drawing into | 
| state | RecyclerView.State: The current state of RecyclerView. | 
startDrag
void startDrag (RecyclerView.ViewHolder viewHolder)
Starts dragging the provided ViewHolder. By default, ItemTouchHelper starts a drag when a
 View is long pressed. You can disable that behavior by overriding
 isLongPressDragEnabled().
 
For this method to work:
- The provided ViewHolder must be a child of the RecyclerView to which this ItemTouchHelper is attached.
- ItemTouchHelper.Callbackmust have dragging enabled.
- There must be a previous touch event that was reported to the ItemTouchHelper through RecyclerView's ItemTouchListener mechanism. As long as no other ItemTouchListener grabs previous events, this should work as expected.
     viewHolder.dragButton.setOnTouchListener(new View.OnTouchListener() {
         public boolean onTouch(View v, MotionEvent event) {
             if (MotionEvent.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                 mItemTouchHelper.startDrag(viewHolder);
             }
             return false;
         }
     });
 | Parameters | |
|---|---|
| viewHolder | RecyclerView.ViewHolder: The ViewHolder to start dragging. It must be a direct child of
                   RecyclerView. | 
See also:
startSwipe
void startSwipe (RecyclerView.ViewHolder viewHolder)
Starts swiping the provided ViewHolder. By default, ItemTouchHelper starts swiping a View
 when user swipes their finger (or mouse pointer) over the View. You can disable this
 behavior
 by overriding ItemTouchHelper.Callback
 
For this method to work:
- The provided ViewHolder must be a child of the RecyclerView to which this ItemTouchHelper is attached.
- ItemTouchHelper.Callbackmust have swiping enabled.
- There must be a previous touch event that was reported to the ItemTouchHelper through RecyclerView's ItemTouchListener mechanism. As long as no other ItemTouchListener grabs previous events, this should work as expected.
     viewHolder.dragButton.setOnTouchListener(new View.OnTouchListener() {
         public boolean onTouch(View v, MotionEvent event) {
             if (MotionEvent.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                 mItemTouchHelper.startSwipe(viewHolder);
             }
             return false;
         }
     });
 | Parameters | |
|---|---|
| viewHolder | RecyclerView.ViewHolder: The ViewHolder to start swiping. It must be a direct child of
                   RecyclerView. | 
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
