Skip to content

Commit df48a3c

Browse files
committed
add touchSlop for swiping
1 parent 7da3056 commit df48a3c

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

library/src/main/java/com/daimajia/swipe/SwipeLayout.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.view.GestureDetector;
1111
import android.view.MotionEvent;
1212
import android.view.View;
13+
import android.view.ViewConfiguration;
1314
import android.view.ViewGroup;
1415
import android.view.ViewParent;
1516
import android.widget.Adapter;
@@ -30,6 +31,8 @@ public class SwipeLayout extends FrameLayout {
3031
private static final int DRAG_TOP = 4;
3132
private static final int DRAG_BOTTOM = 8;
3233

34+
private int mTouchSlop;
35+
3336
private int mLeftIndex;
3437
private int mRightIndex;
3538
private int mTopIndex;
@@ -81,6 +84,7 @@ public SwipeLayout(Context context, AttributeSet attrs) {
8184
public SwipeLayout(Context context, AttributeSet attrs, int defStyle) {
8285
super(context, attrs, defStyle);
8386
mDragHelper = ViewDragHelper.create(this, mDragHelperCallback);
87+
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
8488

8589
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SwipeLayout);
8690
int dragEdgeChoices = a.getInt(R.styleable.SwipeLayout_drag_edge, DRAG_RIGHT);
@@ -839,15 +843,15 @@ public boolean onTouchEvent(MotionEvent event) {
839843
if (getOpenStatus() == Status.Close) {
840844
int lastCurrentDirectionIndex = currentDirectionIndex;
841845
if (angle < 45) {
842-
if (mLeftIndex != -1 && distanceX > 0) {
846+
if (mLeftIndex != -1 && distanceX > mTouchSlop) {
843847
currentDirectionIndex = mLeftIndex;
844-
} else if (mRightIndex != -1) {
848+
} else if (mRightIndex != -1 && distanceX < -mTouchSlop) {
845849
currentDirectionIndex = mRightIndex;
846850
}
847851
} else {
848-
if (mTopIndex != -1 && distanceY < 0) {
852+
if (mTopIndex != -1 && distanceY < -mTouchSlop) {
849853
currentDirectionIndex = mTopIndex;
850-
} else if (mBottomIndex != -1) {
854+
} else if (mBottomIndex != -1 && distanceY > mTouchSlop) {
851855
currentDirectionIndex = mBottomIndex;
852856
}
853857
}
@@ -858,8 +862,8 @@ public boolean onTouchEvent(MotionEvent event) {
858862

859863
boolean doNothing = false;
860864
if (mDragEdges.get(currentDirectionIndex) == DragEdge.Right) {
861-
boolean suitable = (status == Status.Open && distanceX > 0)
862-
|| (status == Status.Close && distanceX < 0);
865+
boolean suitable = (status == Status.Open && distanceX > mTouchSlop)
866+
|| (status == Status.Close && distanceX < -mTouchSlop);
863867
suitable = suitable || (status == Status.Middle);
864868

865869
if (angle > 30 || !suitable) {
@@ -868,8 +872,8 @@ public boolean onTouchEvent(MotionEvent event) {
868872
}
869873

870874
if (mDragEdges.get(currentDirectionIndex) == DragEdge.Left) {
871-
boolean suitable = (status == Status.Open && distanceX < 0)
872-
|| (status == Status.Close && distanceX > 0);
875+
boolean suitable = (status == Status.Open && distanceX < -mTouchSlop)
876+
|| (status == Status.Close && distanceX > mTouchSlop);
873877
suitable = suitable || status == Status.Middle;
874878

875879
if (angle > 30 || !suitable) {
@@ -878,8 +882,8 @@ public boolean onTouchEvent(MotionEvent event) {
878882
}
879883

880884
if (mDragEdges.get(currentDirectionIndex) == DragEdge.Top) {
881-
boolean suitable = (status == Status.Open && distanceY < 0)
882-
|| (status == Status.Close && distanceY > 0);
885+
boolean suitable = (status == Status.Open && distanceY < -mTouchSlop)
886+
|| (status == Status.Close && distanceY > mTouchSlop);
883887
suitable = suitable || status == Status.Middle;
884888

885889
if (angle < 60 || !suitable) {
@@ -888,8 +892,8 @@ public boolean onTouchEvent(MotionEvent event) {
888892
}
889893

890894
if (mDragEdges.get(currentDirectionIndex) == DragEdge.Bottom) {
891-
boolean suitable = (status == Status.Open && distanceY > 0)
892-
|| (status == Status.Close && distanceY < 0);
895+
boolean suitable = (status == Status.Open && distanceY > mTouchSlop)
896+
|| (status == Status.Close && distanceY < -mTouchSlop);
893897
suitable = suitable || status == Status.Middle;
894898

895899
if (angle < 60 || !suitable) {

0 commit comments

Comments
 (0)