diff --git a/library/src/main/java/com/chad/library/adapter/base/callback/ItemDragAndSwipeCallback.java b/library/src/main/java/com/chad/library/adapter/base/callback/ItemDragAndSwipeCallback.java index 63346c0e0..f1705e748 100644 --- a/library/src/main/java/com/chad/library/adapter/base/callback/ItemDragAndSwipeCallback.java +++ b/library/src/main/java/com/chad/library/adapter/base/callback/ItemDragAndSwipeCallback.java @@ -12,12 +12,15 @@ public class ItemDragAndSwipeCallback extends ItemTouchHelper.Callback { // private static final String TAG = ItemDragAndSwipeCallback.class.getSimpleName(); - private BaseQuickAdapter mAdapter; + BaseQuickAdapter mAdapter; - private static final float THRESHOLD_SWIPE = 0.7f; - private static final float THRESHOLD_MOVE = 0.1f; + float mMoveThreshold = 0.1f; + float mSwipeThreshold = 0.7f; - private int mActionState = ItemTouchHelper.ACTION_STATE_IDLE; + int mDragMoveFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; + int mSwipeMoveFlags = ItemTouchHelper.END; + + int mActionState = ItemTouchHelper.ACTION_STATE_IDLE; public ItemDragAndSwipeCallback(BaseQuickAdapter adapter) { mAdapter = adapter; @@ -59,9 +62,7 @@ public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHol @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { - int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; - int swipeFlags = ItemTouchHelper.END; - return makeMovementFlags(dragFlags, swipeFlags); + return makeMovementFlags(mDragMoveFlags, mSwipeMoveFlags); } @Override @@ -85,11 +86,58 @@ public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { @Override public float getMoveThreshold(RecyclerView.ViewHolder viewHolder) { - return THRESHOLD_MOVE; + return mMoveThreshold; } @Override public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) { - return THRESHOLD_SWIPE; + return mSwipeThreshold; + } + + /** + * Set the fraction that the user should move the View to be considered as swiped. + * The fraction is calculated with respect to RecyclerView's bounds. + *

+ * Default value is .5f, which means, to swipe a View, user must move the View at least + * half of RecyclerView's width or height, depending on the swipe direction. + * + * @param swipeThreshold A float value that denotes the fraction of the View size. Default value + * is .8f . + */ + public void setSwipeThreshold(float swipeThreshold) { + mSwipeThreshold = swipeThreshold; + } + + + /** + * Set the fraction that the user should move the View to be considered as it is + * dragged. After a view is moved this amount, ItemTouchHelper starts checking for Views + * below it for a possible drop. + * + * @param moveThreshold A float value that denotes the fraction of the View size. Default value is + * .1f . + */ + public void setMoveThreshold(float moveThreshold) { + mMoveThreshold = moveThreshold; + } + + /** + *

Set the drag movement direction.

+ *

The value should be ItemTouchHelper.UP, ItemTouchHelper.DOWN, ItemTouchHelper.LEFT, ItemTouchHelper.RIGHT or their combination.

+ * You can combine them like ItemTouchHelper.UP | ItemTouchHelper.DOWN, it means that the item could only move up and down when dragged. + * @param dragMoveFlags the drag movement direction. Default value is ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT. + */ + public void setDragMoveFlags(int dragMoveFlags) { + mDragMoveFlags = dragMoveFlags; + } + + /** + *

Set the swipe movement direction.

+ *

The value should be ItemTouchHelper.START, ItemTouchHelper.END or their combination.

+ * You can combine them like ItemTouchHelper.START | ItemTouchHelper.END, it means that the item could swipe to both left or right. + * @param swipeMoveFlags the swipe movement direction. Default value is ItemTouchHelper.END. + */ + public void setSwipeMoveFlags(int swipeMoveFlags) { + mSwipeMoveFlags = swipeMoveFlags; } }