public final class ItemSwipeHelper extends RecyclerViewItemHelper<ItemSwipeHelper.Interactor>
RecyclerViewItemHelper that may be used to support swipe feature for items
displayed in a RecyclerView widget. Instance of this helper may be simply created via
ItemSwipeHelper() constructor.
In order to support swipe feature for a desired adapter, such adapter must implement ItemSwipeHelper.SwipeAdapter
interface and needs to be attached to its corresponding RecyclerView before call to
RecyclerViewItemHelper.attachToRecyclerView(RecyclerView) which will attach such adapter to the swipe helper
automatically. Also all ViewHolders of that adapter of which item
views are desired to be swiped, must implement ItemSwipeHelper.SwipeViewHolder interface as other view
holder implementations will be ignored by the ItemSwipeHelper API. When the ItemSwipeHelper.SwipeAdapter
is properly attached, the item helper will delegate to it all swipe gesture related callbacks/events
whenever appropriate via the adapter's interface.
ItemSwipeHelper.OnSwipeListener may be registered for the helper's ItemSwipeHelper.Interactor
via ItemSwipeHelper.Interactor#addOnSwipeListener(OnSwipeListener) in order to receive
callbacks about started, finished or canceled swipe gesture for a particular
ViewHolder. If the listener is no more needed it should be
unregistered via ItemSwipeHelper.Interactor#removeOnSwipeListener(OnSwipeListener).| Modifier and Type | Class and Description |
|---|---|
static class |
ItemSwipeHelper.Interactor
A
ItemInteractor implementation used by ItemSwipeHelper to handle swipe
gesture related callbacks and to delegate swipe events to the view holder that is being swiped. |
static interface |
ItemSwipeHelper.OnSwipeListener
Listener which may be used to receive callbacks about started, finished or
canceled swipe gesture for a specific
RecyclerView.ViewHolder instance. |
static interface |
ItemSwipeHelper.SwipeAdapter
Required interface for adapters which want to support swipe feature for theirs
ViewHolders. |
static class |
ItemSwipeHelper.SwipeItemAnimator
A
DefaultItemAnimator extension which overrides default implementation of
ItemSwipeHelper.SwipeItemAnimator.animateChange(RecyclerView.ViewHolder, RecyclerView.ViewHolder, int, int, int, int)
in a way that does not cause any drawing artifacts to occur when state of item view for swiped
view holder is about to be restored via restoreHolder(RecyclerView.ViewHolder, int, Runnable). |
static interface |
ItemSwipeHelper.SwipeViewHolder
Required interface for all
ViewHolders which want to support
swipe feature for theirs corresponding item views. |
RecyclerViewItemHelper.Direction, RecyclerViewItemHelper.Interaction, RecyclerViewItemHelper.InteractiveViewHolder, RecyclerViewItemHelper.ItemInteractor, RecyclerViewItemHelper.MovementItemTouchHelper.Callback, ItemTouchHelper.SimpleCallback, ItemTouchHelper.ViewDropHandler| Modifier and Type | Field and Description |
|---|---|
static int |
INTERACTION
Interaction constant specific for
ItemSwipeHelper. |
static long |
RESTORE_HOLDER_ANIMATION_DURATION
Default duration for animation used to restore position of the holder's item view.
|
static float |
SWIPE_THRESHOLD
Default swipe threshold for the swipe gesture.
|
ACTION_STATE_DRAG, ACTION_STATE_IDLE, ACTION_STATE_SWIPE, ANIMATION_TYPE_DRAG, ANIMATION_TYPE_SWIPE_CANCEL, ANIMATION_TYPE_SWIPE_SUCCESS, DOWN, END, LEFT, RIGHT, START, UP| Constructor and Description |
|---|
ItemSwipeHelper()
Creates a new instance of ItemSwipeHelper.
|
| Modifier and Type | Method and Description |
|---|---|
long |
getRestoreHolderAnimationDuration()
Returns the duration of holder's restore animation.
|
Interpolator |
getRestoreHolderAnimationInterpolator()
Returns the interpolator for holder's restore animation.
|
static int |
makeSwipeFlags(int movementFlags)
Makes movement flags for the swipe gesture.
|
boolean |
restoreHolder(RecyclerView.ViewHolder viewHolder,
int direction)
Same as
restoreHolder(RecyclerView.ViewHolder, int, Runnable) with null
callback. |
boolean |
restoreHolder(RecyclerView.ViewHolder viewHolder,
int direction,
Runnable animationCallback)
Restores position of the given viewHolder (of its item view) to its initial state.
|
void |
setRestoreHolderAnimationDuration(long duration)
Sets a duration for animation used to restore position of holder's item view after swipe
gesture for that holder has been canceled.
|
void |
setRestoreHolderAnimationInterpolator(Interpolator interpolator)
Sets an interpolator for animation used to restore position of holder's item view after swipe
gesture for that holder has been canceled.
|
attachToRecyclerView, getInteractorgetItemOffsets, onChildViewAttachedToWindow, onChildViewDetachedFromWindow, onDraw, onDrawOver, startDrag, startSwipegetItemOffsets, onDraw, onDrawOverpublic static final int INTERACTION
ItemSwipeHelper.public static final float SWIPE_THRESHOLD
public static final long RESTORE_HOLDER_ANIMATION_DURATION
public ItemSwipeHelper()
public static int makeSwipeFlags(int movementFlags)
movementFlags - The desired movement flags. One of flags defined by
@Movement annotation.ItemSwipeHelper.Callback#makeMovementFlags(int, int)public void setRestoreHolderAnimationDuration(long duration)
Default value: RESTORE_HOLDER_ANIMATION_DURATION
duration - The desired duration.getRestoreHolderAnimationDuration(),
restoreHolder(RecyclerView.ViewHolder, int, Runnable),
ItemSwipeHelper.OnSwipeListener.onSwipeCanceled(ItemSwipeHelper, RecyclerView.ViewHolder)public long getRestoreHolderAnimationDuration()
setRestoreHolderAnimationDuration(long)public void setRestoreHolderAnimationInterpolator(@NonNull Interpolator interpolator)
Default value: FastOutSlowInInterpolator
interpolator - The desired interpolator.getRestoreHolderAnimationInterpolator(),
ItemSwipeHelper.OnSwipeListener.onSwipeCanceled(ItemSwipeHelper, RecyclerView.ViewHolder)@NonNull public Interpolator getRestoreHolderAnimationInterpolator()
setRestoreHolderAnimationInterpolator(Interpolator)public boolean restoreHolder(@NonNull RecyclerView.ViewHolder viewHolder, int direction)
restoreHolder(RecyclerView.ViewHolder, int, Runnable) with null
callback.public boolean restoreHolder(@NonNull RecyclerView.ViewHolder viewHolder, int direction, @Nullable Runnable animationCallback)
When the restore animation finishes, the adapter of the RecyclerViewItemHelper to which
is this item helper attached, will be notified via RecyclerView.Adapter#notifyItemChanged(int)
with position of the restored holder.
This method should be called to restore holder's position whenever swipe gesture for such holder is canceled to prevent case when holder remains in the swiped state, that is off screen.
viewHolder - The view holder of which position to restore.direction - The direction for which to restore the holder's state.animationCallback - Callback to be fired when the restore animation finishes.
May be null.True restoring of the holder has been performed, false otherwise.ItemSwipeHelper.OnSwipeListener.onSwipeCanceled(ItemSwipeHelper, RecyclerView.ViewHolder)