# ItemTouchHelper

Artifact: [androidx.recyclerview:recyclerview](https://developer.android.com/jetpack/androidx/releases/recyclerview) [View Source](https://cs.android.com/search?q=file:androidx/recyclerview/widget/ItemTouchHelper.java+class:androidx.recyclerview.widget.ItemTouchHelper) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

*** ** * ** ***


```
public class ItemTouchHelper extends RecyclerView.ItemDecoration implements RecyclerView.OnChildAttachStateChangeListener
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.recyclerview.widget.RecyclerView.ItemDecoration](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ItemDecoration) ||
|   | ↳ | [androidx.recyclerview.widget.ItemTouchHelper](https://developer.android.com/reference/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 `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.Callback#onMove(androidx.recyclerview.widget.RecyclerView,androidx.recyclerview.widget.RecyclerView.ViewHolder,androidx.recyclerview.widget.RecyclerView.ViewHolder)` and / or `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.Callback#onSwiped(androidx.recyclerview.widget.RecyclerView.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 `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.Callback` class or implementing `https://developer.android.com/reference/androidx/recyclerview/widget/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 `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.Callback#onChildDraw(android.graphics.Canvas,androidx.recyclerview.widget.RecyclerView,androidx.recyclerview.widget.RecyclerView.ViewHolder,float,float,int,boolean)` or `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.Callback#onChildDrawOver(android.graphics.Canvas,androidx.recyclerview.widget.RecyclerView,androidx.recyclerview.widget.RecyclerView.ViewHolder,float,float,int,boolean)`.
Most of the time you only need to override `onChildDraw`.

## Summary

| ### Nested types |
|---|
| `public abstract class https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.Callback` This class is the contract between ItemTouchHelper and your application. |
| `public abstract class https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.SimpleCallback extends https://developer.android.com/reference/androidx/recyclerview/widget/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. |
| `public interface https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.ViewDropHandler` An interface which can be implemented by LayoutManager for better integration with `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper`. |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#ACTION_STATE_DRAG() = 2` A View is currently being dragged. |
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#ACTION_STATE_IDLE() = 0` ItemTouchHelper is in idle state. |
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#ACTION_STATE_SWIPE() = 1` A View is currently being swiped. |
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#ANIMATION_TYPE_DRAG() = 8` Animation type for views that were dragged and now will animate to their final position. |
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#ANIMATION_TYPE_SWIPE_CANCEL() = 4` Animation type for views which are not completely swiped thus will animate back to their original position. |
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#ANIMATION_TYPE_SWIPE_SUCCESS() = 2` Animation type for views which are swiped successfully. |
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#DOWN() = 2` Down direction, used for swipe \&drag control. |
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#END() = 32` Horizontal end direction. |
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#LEFT() = 4` Left direction, used for swipe \&drag control. |
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#RIGHT() = 8` Right direction, used for swipe \&drag control. |
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#START() = 16` Horizontal start direction. |
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#UP() = 1` Up direction, used for swipe \&drag control. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#ItemTouchHelper(androidx.recyclerview.widget.ItemTouchHelper.Callback)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.Callback callback)` Creates an ItemTouchHelper that will work with the given Callback. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#attachToRecyclerView(androidx.recyclerview.widget.RecyclerView)(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView recyclerView)` Attaches the ItemTouchHelper to the provided RecyclerView. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#getItemOffsets(android.graphics.Rect,android.view.View,androidx.recyclerview.widget.RecyclerView,androidx.recyclerview.widget.RecyclerView.State)( https://developer.android.com/reference/android/graphics/Rect.html outRect, https://developer.android.com/reference/android/view/View.html view, https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView parent, https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.State state )` Retrieve any offsets for the given item. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#onChildViewAttachedToWindow(android.view.View)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html view)` Called when a view is attached to the RecyclerView. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#onChildViewDetachedFromWindow(android.view.View)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html view)` Called when a view is detached from RecyclerView. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#onDraw(android.graphics.Canvas,androidx.recyclerview.widget.RecyclerView,androidx.recyclerview.widget.RecyclerView.State)(https://developer.android.com/reference/android/graphics/Canvas.html c, https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView parent, https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.State state)` Draw any appropriate decorations into the Canvas supplied to the RecyclerView. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#onDrawOver(android.graphics.Canvas,androidx.recyclerview.widget.RecyclerView,androidx.recyclerview.widget.RecyclerView.State)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Canvas.html c, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView parent, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.State state )` Draw any appropriate decorations into the Canvas supplied to the RecyclerView. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#startDrag(androidx.recyclerview.widget.RecyclerView.ViewHolder)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder viewHolder)` Starts dragging the provided ViewHolder. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#startSwipe(androidx.recyclerview.widget.RecyclerView.ViewHolder)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder viewHolder)` Starts swiping the provided ViewHolder. |

| ### Inherited methods |
|---|
| From [androidx.recyclerview.widget.RecyclerView.ItemDecoration](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ItemDecoration) |---|---| | `void` | `[getItemOffsets](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ItemDecoration#getItemOffsets(android.graphics.Rect,int,androidx.recyclerview.widget.RecyclerView))( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Rect.html outRect, int itemPosition, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView parent )` **This method is deprecated.** Use `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ItemDecoration#getItemOffsets(android.graphics.Rect,android.view.View,androidx.recyclerview.widget.RecyclerView,androidx.recyclerview.widget.RecyclerView.State)` <br /> | | `void` | `[onDraw](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ItemDecoration#onDraw(android.graphics.Canvas,androidx.recyclerview.widget.RecyclerView))(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Canvas.html c, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView parent)` **This method is deprecated.** Override `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ItemDecoration#onDraw(android.graphics.Canvas,androidx.recyclerview.widget.RecyclerView,androidx.recyclerview.widget.RecyclerView.State)` <br /> | | `void` | `[onDrawOver](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ItemDecoration#onDrawOver(android.graphics.Canvas,androidx.recyclerview.widget.RecyclerView))(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Canvas.html c, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView parent)` **This method is deprecated.** Override `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ItemDecoration#onDrawOver(android.graphics.Canvas,androidx.recyclerview.widget.RecyclerView,androidx.recyclerview.widget.RecyclerView.State)` <br /> | |

## Constants

### ACTION_STATE_DRAG

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public static final int ACTION_STATE_DRAG = 2
```

A View is currently being dragged.

### ACTION_STATE_IDLE

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public static final int ACTION_STATE_IDLE = 0
```

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.

### ACTION_STATE_SWIPE

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public static final int ACTION_STATE_SWIPE = 1
```

A View is currently being swiped.

### ANIMATION_TYPE_DRAG

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public static final int ANIMATION_TYPE_DRAG = 8
```

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

### ANIMATION_TYPE_SWIPE_CANCEL

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public static final int ANIMATION_TYPE_SWIPE_CANCEL = 4
```

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

### ANIMATION_TYPE_SWIPE_SUCCESS

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public static final int ANIMATION_TYPE_SWIPE_SUCCESS = 2
```

Animation type for views which are swiped successfully.

### DOWN

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public static final int DOWN = 2
```

Down direction, used for swipe \&drag control.

### END

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public static final int END = 32
```

Horizontal end direction. Resolved to LEFT or RIGHT depending on RecyclerView's layout direction. Used for swipe \&drag control.

### LEFT

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public static final int LEFT = 4
```

Left direction, used for swipe \&drag control.

### RIGHT

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public static final int RIGHT = 8
```

Right direction, used for swipe \&drag control.

### START

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public static final int START = 16
```

Horizontal start direction. Resolved to LEFT or RIGHT depending on RecyclerView's layout direction. Used for swipe \&drag control.

### UP

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public static final int UP = 1
```

Up direction, used for swipe \&drag control.

## Public constructors

### ItemTouchHelper

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public ItemTouchHelper(@NonNull ItemTouchHelper.Callback callback)
```

Creates an ItemTouchHelper that will work with the given Callback.

You can attach ItemTouchHelper to a RecyclerView via `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper#attachToRecyclerView(androidx.recyclerview.widget.RecyclerView)`. Upon attaching, it will add an item decoration, an onItemTouchListener and a Child attach / detach listener to the RecyclerView.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.Callback callback` | The Callback which controls the behavior of this touch helper. |

## Public methods

### attachToRecyclerView

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void attachToRecyclerView(@Nullable 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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView recyclerView` | The RecyclerView instance to which you want to add this helper or `null` if you want to remove ItemTouchHelper from the current RecyclerView. |

### getItemOffsets

```
public 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 `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#getChildAdapterPosition(android.view.View)` to get the adapter position of the View.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/graphics/Rect.html outRect` | Rect to receive the output. |
| `https://developer.android.com/reference/android/view/View.html view` | The child view to decorate |
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView parent` | RecyclerView this ItemDecoration is decorating |
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.State state` | The current state of RecyclerView. |

### onChildViewAttachedToWindow

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.4.0)

```
public void onChildViewAttachedToWindow(@NonNull View view)
```

Called when a view is attached to the RecyclerView.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html view` | The View which is attached to the RecyclerView |

### onChildViewDetachedFromWindow

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.4.0)

```
public void onChildViewDetachedFromWindow(@NonNull View view)
```

Called when a view is detached from RecyclerView.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html view` | The View which is being detached from the RecyclerView |

### onDraw

```
public 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 |
|---|---|
| `https://developer.android.com/reference/android/graphics/Canvas.html c` | Canvas to draw into |
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView parent` | RecyclerView this ItemDecoration is drawing into |
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.State state` | The current state of RecyclerView |

### onDrawOver

```
public void onDrawOver(
    @NonNull Canvas c,
    @NonNull RecyclerView parent,
    @NonNull 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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Canvas.html c` | Canvas to draw into |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView parent` | RecyclerView this ItemDecoration is drawing into |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.State state` | The current state of RecyclerView. |

### startDrag

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void startDrag(@NonNull 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 `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.Callback#isLongPressDragEnabled()`.

For this method to work:

- The provided ViewHolder must be a child of the RecyclerView to which this ItemTouchHelper is attached.
- `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.Callback` must 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.

For example, if you would like to let your user to be able to drag an Item by touching one of its descendants, you may implement it as follows:

```
    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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder viewHolder` | The ViewHolder to start dragging. It must be a direct child of RecyclerView. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.Callback#isItemViewSwipeEnabled()` |   |

### startSwipe

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

```
public void startSwipe(@NonNull 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 `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.Callback`

For this method to work:

- The provided ViewHolder must be a child of the RecyclerView to which this ItemTouchHelper is attached.
- `https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper.Callback` must 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.

For example, if you would like to let your user to be able to swipe an Item by touching one of its descendants, you may implement it as follows:

```
    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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder viewHolder` | The ViewHolder to start swiping. It must be a direct child of RecyclerView. |