Package 

Class ItemTouchHelperCallback


  • 
    public final class ItemTouchHelperCallback
    extends ItemTouchHelper.Callback
                        

    Author: liecoder Date: 2024/8/13 周二 Version: 1.0

    • Method Summary

      Modifier and Type Method Description
      Boolean isLongPressDragEnabled() Returns whether ItemTouchHelper should start a drag and drop operation if an item is long pressed.
      Boolean isItemViewSwipeEnabled() Returns whether ItemTouchHelper should start a swipe operation if a pointer is swiped over the View.
      Unit onSwiped(RecyclerView.ViewHolder viewHolder, Integer direction) Called when a ViewHolder is swiped by the user.
      Integer getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) Should return a composite flag which defines the enabled move directions in each state (idle, swiping, dragging).
      Boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) Called when ItemTouchHelper wants to move the dragged item from its old position to the new position.
      Unit onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, Float dX, Float dY, Integer actionState, Boolean isCurrentlyActive) Called by ItemTouchHelper on RecyclerView's onDraw callback.
      Unit onSelectedChanged(RecyclerView.ViewHolder viewHolder, Integer actionState) Called when the ViewHolder swiped or dragged by the ItemTouchHelper is changed.
      Unit clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) Called by the ItemTouchHelper when the user interaction with an element is over and it also completed its animation.
      • Methods inherited from class com.liecoder.framework.drag.ItemTouchHelperCallback

        canDropOver, chooseDropTarget, convertToAbsoluteDirection, getAbsoluteMovementFlags, getAnimationDuration, getBoundingBoxMargin, getMoveThreshold, getSwipeEscapeVelocity, getSwipeThreshold, getSwipeVelocityThreshold, hasDragFlag, hasSwipeFlag, interpolateOutOfBoundsScroll, onChildDrawOver, onDraw, onDrawOver, onMoved
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isLongPressDragEnabled

         Boolean isLongPressDragEnabled()

        Returns whether ItemTouchHelper should start a drag and drop operation if an item is long pressed.

        Default value returns true but you may want to disable this if you want to start dragging on a custom view touch using .startDrag.

      • isItemViewSwipeEnabled

         Boolean isItemViewSwipeEnabled()

        Returns whether ItemTouchHelper should start a swipe operation if a pointer is swiped over the View.

        Default value returns true but you may want to disable this if you want to start swiping on a custom view touch using .startSwipe.

      • onSwiped

         Unit onSwiped(RecyclerView.ViewHolder viewHolder, Integer direction)

        Called when a ViewHolder is swiped by the user.

        If you are returning relative directions (.START , .END) from the .getMovementFlags method, this method will also use relative directions. Otherwise, it will use absolute directions.

        If you don't support swiping, this method will never be called.

        ItemTouchHelper will keep a reference to the View until it is detached from RecyclerView. As soon as it is detached, ItemTouchHelper will call .clearView.

        Parameters:
        viewHolder - The ViewHolder which has been swiped by the user.
        direction - The direction to which the ViewHolder is swiped.
      • getMovementFlags

         Integer getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)

        Should return a composite flag which defines the enabled move directions in each state (idle, swiping, dragging).

        Instead of composing this flag manually, you can use .makeMovementFlags or .makeFlag.

        This flag is composed of 3 sets of 8 bits, where first 8 bits are for IDLE state, next 8 bits are for SWIPE state and third 8 bits are for DRAG state. Each 8 bit sections can be constructed by simply OR'ing direction flags defined in ItemTouchHelper.

        For example, if you want it to allow swiping LEFT and RIGHT but only allow starting to swipe by swiping RIGHT, you can return:

        <pre> makeFlag(ACTION_STATE_IDLE, RIGHT) | makeFlag(ACTION_STATE_SWIPE, LEFT | RIGHT); </pre> *

        This means, allow right movement while IDLE and allow right and left movement while swiping.

        Parameters:
        recyclerView - The RecyclerView to which ItemTouchHelper is attached.
        viewHolder - The ViewHolder for which the movement information is necessary.
      • onMove

         Boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target)

        Called when ItemTouchHelper wants to move the dragged item from its old position to the new position.

        If this method returns true, ItemTouchHelper assumes viewHolder has been moved to the adapter position of target ViewHolder (ViewHolder.getAdapterPosition).

        If you don't support drag & drop, this method will never be called.

        Parameters:
        recyclerView - The RecyclerView to which ItemTouchHelper is attached to.
        target - The ViewHolder over which the currently active item is being dragged.
      • onChildDraw

         Unit onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, Float dX, Float dY, Integer actionState, Boolean isCurrentlyActive)

        Called by ItemTouchHelper on RecyclerView's onDraw callback.

        If you would like to customize how your View's respond to user interactions, this is a good place to override.

        Default implementation translates the child by the given dX, dY. ItemTouchHelper also takes care of drawing the child after other children if it is being dragged. This is done using child re-ordering mechanism. On platforms prior to L, this is achieved via android.view.ViewGroup.getChildDrawingOrder and on L and after, it changes View's elevation value to be greater than all other children.)

        Parameters:
        c - The canvas which RecyclerView is drawing its children
        recyclerView - The RecyclerView to which ItemTouchHelper is attached to
        viewHolder - The ViewHolder which is being interacted by the User or it was interacted and simply animating to its original position
        dX - The amount of horizontal displacement caused by user's action
        dY - The amount of vertical displacement caused by user's action
        actionState - The type of interaction on the View.
        isCurrentlyActive - True if this view is currently being controlled by the user or false it is simply animating back to its original state.
      • onSelectedChanged

         Unit onSelectedChanged(RecyclerView.ViewHolder viewHolder, Integer actionState)

        Called when the ViewHolder swiped or dragged by the ItemTouchHelper is changed.

        If you override this method, you should call super.

        Parameters:
        viewHolder - The new ViewHolder that is being swiped or dragged.
        actionState - One of ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.ACTION_STATE_SWIPE or ItemTouchHelper.ACTION_STATE_DRAG.
      • clearView

         Unit clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)

        Called by the ItemTouchHelper when the user interaction with an element is over and it also completed its animation.

        This is a good place to clear all changes on the View that was done in .onSelectedChanged, .onChildDraw or .onChildDrawOver.

        Parameters:
        recyclerView - The RecyclerView which is controlled by the ItemTouchHelper.
        viewHolder - The View that was interacted by the user.