Most visited

Recently visited

ItemTouchHelper.SimpleCallback

public static abstract class ItemTouchHelper.SimpleCallback
extends ItemTouchHelper.Callback

java.lang.Object
   ↳ android.support.v7.widget.helper.ItemTouchHelper.Callback
     ↳ android.support.v7.widget.helper.ItemTouchHelper.SimpleCallback


一个简单的包装到默认回调,你可以用拖拽和滑动方向来构造这个类,这个类将处理标志回调。 根据您的使用情况,您仍然应该覆盖onMove或onSwiped。

 ItemTouchHelper mIth = new ItemTouchHelper(
     new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,
         ItemTouchHelper.LEFT) {
         public abstract boolean onMove(RecyclerView recyclerView,
             ViewHolder viewHolder, ViewHolder target) {
             final int fromPos = viewHolder.getAdapterPosition();
             final int toPos = viewHolder.getAdapterPosition();
             // move item in `fromPos` to `toPos` in adapter.
             return true;// true if moved, false otherwise
         }
         public void onSwiped(ViewHolder viewHolder, int direction) {
             // remove from adapter
         }
 });
 

Summary

Inherited constants

From class android.support.v7.widget.helper.ItemTouchHelper.Callback

Public constructors

ItemTouchHelper.SimpleCallback(int dragDirs, int swipeDirs)

为给定的拖动和滑动限额创建一个回叫。

Public methods

int getDragDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)

返回提供的ViewHolder的拖动方向。

int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)

应该返回一个组合标志,该标志定义每个状态下的启用移动方向(空闲,滑动,拖动)。

int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)

返回提供的ViewHolder的滑动方向。

void setDefaultDragDirs(int defaultDragDirs)

更新默认的拖动方向。

void setDefaultSwipeDirs(int defaultSwipeDirs)

更新默认滑动方向。

Inherited methods

From class android.support.v7.widget.helper.ItemTouchHelper.Callback
From class java.lang.Object

Public constructors

ItemTouchHelper.SimpleCallback

ItemTouchHelper.SimpleCallback (int dragDirs, 
                int swipeDirs)

为给定的拖动和滑动限额创建一个回叫。 这些值用作默认值,如果您想为每个ViewHolder定制行为,则可以覆盖getSwipeDirs(RecyclerView, ViewHolder)和/或getDragDirs(RecyclerView, ViewHolder)

Parameters
dragDirs int: Binary OR of direction flags in which the Views can be dragged. Must be composed of LEFT, RIGHT, START, END, UP and DOWN.
swipeDirs int: Binary OR of direction flags in which the Views can be swiped. Must be composed of LEFT, RIGHT, START, END, UP and DOWN.

Public methods

getDragDirs

int getDragDirs (RecyclerView recyclerView, 
                RecyclerView.ViewHolder viewHolder)

返回提供的ViewHolder的拖动方向。 默认实现返回通过构造函数或setDefaultDragDirs(int)设置的拖动方向。

Parameters
recyclerView RecyclerView: The RecyclerView to which the ItemTouchHelper is attached to.
viewHolder RecyclerView.ViewHolder: The RecyclerView for which the swipe drection is queried.
Returns
int A binary OR of direction flags.

getMovementFlags

int getMovementFlags (RecyclerView recyclerView, 
                RecyclerView.ViewHolder viewHolder)

应该返回一个组合标志,该标志定义每个状态下的启用移动方向(空闲,滑动,拖动)。

您可以使用 makeMovementFlags(int, int)makeFlag(int, int)而不是手动组合此标志。

该标志由3组8位组成,其中前8位用于IDLE状态,后8位用于SWIPE状态,第3位8位用于DRAG状态。 每个8位段可以通过在ItemTouchHelper定义的简单或方向标志来ItemTouchHelper

例如,如果您希望允许向左和向右滑动但只允许通过向右滑动来开始滑动,则可以返回:

      makeFlag(ACTION_STATE_IDLE, RIGHT) | makeFlag(ACTION_STATE_SWIPE, LEFT | RIGHT);
 
This means, allow right movement while IDLE and allow right and left movement while swiping.

Parameters
recyclerView RecyclerView: The RecyclerView to which ItemTouchHelper is attached.
viewHolder RecyclerView.ViewHolder: The ViewHolder for which the movement information is necessary.
Returns
int flags specifying which movements are allowed on this ViewHolder.

getSwipeDirs

int getSwipeDirs (RecyclerView recyclerView, 
                RecyclerView.ViewHolder viewHolder)

返回提供的ViewHolder的滑动方向。 默认实现返回通过构造函数或setDefaultSwipeDirs(int)设置的滑动方向。

Parameters
recyclerView RecyclerView: The RecyclerView to which the ItemTouchHelper is attached to.
viewHolder RecyclerView.ViewHolder: The RecyclerView for which the swipe drection is queried.
Returns
int A binary OR of direction flags.

setDefaultDragDirs

void setDefaultDragDirs (int defaultDragDirs)

更新默认的拖动方向。 例如,您可以使用此方法根据您的用例切换某些方向。

Parameters
defaultDragDirs int: Binary OR of directions in which the ViewHolders can be dragged.

setDefaultSwipeDirs

void setDefaultSwipeDirs (int defaultSwipeDirs)

更新默认滑动方向。 例如,您可以使用此方法根据您的用例切换某些方向。

Parameters
defaultSwipeDirs int: Binary OR of directions in which the ViewHolders can be swiped.

Hooray!