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. 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 onChildDraw(Canvas, RecyclerView, ViewHolder, float, float, int, boolean)
or onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, boolean)
.
onChildDraw
but due to limitations of
platform prior to Honeycomb, you may need to implement onChildDrawOver
as well.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
ItemTouchHelper.Callback | This class is the contract between ItemTouchHelper and your application. | ||||||||||
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. | ||||||||||
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 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates an ItemTouchHelper that will work with the given Callback.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Attaches the ItemTouchHelper to the provided RecyclerView.
| |||||||||||
Retrieve any offsets for the given item.
| |||||||||||
Called when a view is attached to the RecyclerView.
| |||||||||||
Called when a view is detached from RecyclerView.
| |||||||||||
Draw any appropriate decorations into the Canvas supplied to the RecyclerView.
| |||||||||||
Draw any appropriate decorations into the Canvas supplied to the RecyclerView.
| |||||||||||
Starts dragging the provided ViewHolder.
| |||||||||||
Starts swiping the provided ViewHolder.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.support.v7.widget.RecyclerView.ItemDecoration
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
android.support.v7.widget.RecyclerView.OnChildAttachStateChangeListener
|
A View is currently being dragged.
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.
A View is currently being swiped.
Animation type for views that were dragged and now will animate to their final position.
Animation type for views which are not completely swiped thus will animate back to their original position.
Animation type for views which are swiped successfully.
Down direction, used for swipe & drag control.
Horizontal end direction. Resolved to LEFT or RIGHT depending on RecyclerView's layout direction. Used for swipe & drag control.
Left direction, used for swipe & drag control.
Right direction, used for swipe & drag control.
Horizontal start direction. Resolved to LEFT or RIGHT depending on RecyclerView's layout direction. Used for swipe & drag control.
Up direction, used for swipe & drag control.
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.
callback | The Callback which controls the behavior of this touch helper. |
---|
Attaches the ItemTouchHelper to the provided RecyclerView. If TouchHelper is already attached to a RecyclerView, it will first detach from the previous one.
recyclerView | The RecyclerView instance to which you want to add this helper. |
---|
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.
outRect | Rect to receive the output. |
---|---|
view | The child view to decorate |
parent | RecyclerView this ItemDecoration is decorating |
state | The current state of RecyclerView. |
Called when a view is attached to the RecyclerView.
view | The View which is attached to the RecyclerView |
---|
Called when a view is detached from RecyclerView.
view | The View which is being detached from the RecyclerView |
---|
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.
c | Canvas to draw into |
---|---|
parent | RecyclerView this ItemDecoration is drawing into |
state | The current state of RecyclerView |
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.
c | Canvas to draw into |
---|---|
parent | RecyclerView this ItemDecoration is drawing into |
state | The current state of RecyclerView. |
Starts dragging the provided ViewHolder. By default, ItemTouchHelper starts a drag when a
View is long pressed. You can disable that behavior via
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 ViewHolder to start dragging. It must be a direct child of RecyclerView. |
---|
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:
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 ViewHolder to start swiping. It must be a direct child of RecyclerView. |
---|