public class ItemTouchHelper
extends android.support.v7.widget.RecyclerView.ItemDecoration
implements android.support.v7.widget.RecyclerView.OnChildAttachStateChangeListener
Callback#onMove(RecyclerView, ViewHolder, ViewHolder)
and / or
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. On
platforms older than Honeycomb, ItemTouchHelper uses canvas translations and View's visibility
property to move items in response to touch events. You can customize these behaviors by
overriding Callback#onChildDraw(Canvas, RecyclerView, ViewHolder, float, float, int,
boolean)
or Callback#onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int,
boolean)
.
Most of the time, you only need to override onChildDraw
but due to limitations of
platform prior to Honeycomb, you may need to implement onChildDrawOver
as well.Modifier and Type | Class and Description |
---|---|
static class |
ItemTouchHelper.Callback
This class is the contract between ItemTouchHelper and your application.
|
static 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.
|
static interface |
ItemTouchHelper.ViewDropHandler
An interface which can be implemented by LayoutManager for better integration with
ItemTouchHelper . |
Modifier and Type | Field and Description |
---|---|
static int |
ACTION_STATE_DRAG
A View is currently being dragged.
|
static int |
ACTION_STATE_IDLE
ItemTouchHelper is in idle state.
|
static int |
ACTION_STATE_SWIPE
A View is currently being swiped.
|
static int |
ANIMATION_TYPE_DRAG
Animation type for views that were dragged and now will animate to their final position.
|
static int |
ANIMATION_TYPE_SWIPE_CANCEL
Animation type for views which are not completely swiped thus will animate back to their
original position.
|
static int |
ANIMATION_TYPE_SWIPE_SUCCESS
Animation type for views which are swiped successfully.
|
static int |
DOWN
Down direction, used for swipe & drag control.
|
static int |
END
Horizontal end direction.
|
static int |
LEFT
Left direction, used for swipe & drag control.
|
static int |
RIGHT
Right direction, used for swipe & drag control.
|
static int |
START
Horizontal start direction.
|
static int |
UP
Up direction, used for swipe & drag control.
|
Constructor and Description |
---|
ItemTouchHelper(ItemTouchHelper.Callback callback)
Creates an ItemTouchHelper that will work with the given Callback.
|
Modifier and Type | Method and Description |
---|---|
void |
attachToRecyclerView(android.support.v7.widget.RecyclerView recyclerView)
Attaches the ItemTouchHelper to the provided RecyclerView.
|
void |
getItemOffsets(android.graphics.Rect outRect,
android.view.View view,
android.support.v7.widget.RecyclerView parent,
android.support.v7.widget.RecyclerView.State state) |
void |
onChildViewAttachedToWindow(android.view.View view) |
void |
onChildViewDetachedFromWindow(android.view.View view) |
void |
onDraw(android.graphics.Canvas c,
android.support.v7.widget.RecyclerView parent,
android.support.v7.widget.RecyclerView.State state) |
void |
onDrawOver(android.graphics.Canvas c,
android.support.v7.widget.RecyclerView parent,
android.support.v7.widget.RecyclerView.State state) |
void |
startDrag(android.support.v7.widget.RecyclerView.ViewHolder viewHolder)
Starts dragging the provided RowViewHolder.
|
void |
startSwipe(android.support.v7.widget.RecyclerView.ViewHolder viewHolder)
Starts swiping the provided RowViewHolder.
|
public static final int UP
public static final int DOWN
public static final int LEFT
public static final int RIGHT
public static final int START
public static final int END
public static final int ACTION_STATE_IDLE
public static final int ACTION_STATE_SWIPE
public static final int ACTION_STATE_DRAG
public static final int ANIMATION_TYPE_SWIPE_SUCCESS
public static final int ANIMATION_TYPE_SWIPE_CANCEL
public static final int ANIMATION_TYPE_DRAG
public ItemTouchHelper(ItemTouchHelper.Callback callback)
attachToRecyclerView(RecyclerView)
. Upon attaching, it will add an item decoration,
an onItemTouchListener and a Child attach / detach listener to the RecyclerView.callback
- The Callback which controls the behavior of this touch helper.public void attachToRecyclerView(@Nullable android.support.v7.widget.RecyclerView recyclerView)
null
to detach it from the current 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.public void onDrawOver(android.graphics.Canvas c, android.support.v7.widget.RecyclerView parent, android.support.v7.widget.RecyclerView.State state)
onDrawOver
in class android.support.v7.widget.RecyclerView.ItemDecoration
public void onDraw(android.graphics.Canvas c, android.support.v7.widget.RecyclerView parent, android.support.v7.widget.RecyclerView.State state)
onDraw
in class android.support.v7.widget.RecyclerView.ItemDecoration
public void onChildViewAttachedToWindow(android.view.View view)
onChildViewAttachedToWindow
in interface android.support.v7.widget.RecyclerView.OnChildAttachStateChangeListener
public void onChildViewDetachedFromWindow(android.view.View view)
onChildViewDetachedFromWindow
in interface android.support.v7.widget.RecyclerView.OnChildAttachStateChangeListener
public void getItemOffsets(android.graphics.Rect outRect, android.view.View view, android.support.v7.widget.RecyclerView parent, android.support.v7.widget.RecyclerView.State state)
getItemOffsets
in class android.support.v7.widget.RecyclerView.ItemDecoration
public void startDrag(android.support.v7.widget.RecyclerView.ViewHolder viewHolder)
ItemTouchHelper.Callback.isLongPressDragEnabled()
.
For this method to work:
ItemTouchHelper.Callback
must have dragging enabled.viewHolder.dragButton.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) { mItemTouchHelper.startDrag(viewHolder); } return false; } });
viewHolder
- The RowViewHolder to start dragging. It must be a direct child of
RecyclerView.ItemTouchHelper.Callback.isItemViewSwipeEnabled()
public void startSwipe(android.support.v7.widget.RecyclerView.ViewHolder viewHolder)
ItemTouchHelper.Callback
For this method to work:
ItemTouchHelper.Callback
must have swiping enabled.viewHolder.dragButton.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) { mItemTouchHelper.startSwipe(viewHolder); } return false; } });
viewHolder
- The RowViewHolder to start swiping. It must be a direct child of
RecyclerView.