Skip to content

Commit

Permalink
Merge pull request #125 from elfman/master
Browse files Browse the repository at this point in the history
Enable change the drag and swipe movement direction and threshold by setters.
  • Loading branch information
CymChad authored Jul 3, 2016
2 parents b49dd0e + 17068dc commit f65f8b7
Showing 1 changed file with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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.
* <p>
* 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;
}

/**
* <p>Set the drag movement direction.</p>
* <p>The value should be ItemTouchHelper.UP, ItemTouchHelper.DOWN, ItemTouchHelper.LEFT, ItemTouchHelper.RIGHT or their combination.</p>
* 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;
}

/**
* <p>Set the swipe movement direction.</p>
* <p>The value should be ItemTouchHelper.START, ItemTouchHelper.END or their combination.</p>
* 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;
}
}

0 comments on commit f65f8b7

Please sign in to comment.