From a8f0c6ddba0e002032d6fa78a2f4635c44db6bbc Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Thu, 6 Jul 2017 23:42:18 -0700 Subject: Support other axis --- .../ca/allanwang/kau/swipe/SwipeBackLayout.kt | 1 + .../ca/allanwang/kau/swipe/ViewDragHelper.java | 69 ++++++++++++++++++---- .../kotlin/ca/allanwang/kau/sample/AnimActivity.kt | 2 +- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt index 00aa33d..63efa01 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt @@ -115,6 +115,7 @@ class SwipeBackLayout @JvmOverloads constructor(context: Context, attrs: Attribu field = value horizontal = edgeFlag == SWIPE_EDGE_LEFT || edgeFlag == SWIPE_EDGE_RIGHT dragHelper.setEdgeTrackingEnabled(value) + dragHelper.edgeFlag = value } private var horizontal = true diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java b/core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java index 8f7f27c..3368e10 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/ViewDragHelper.java @@ -13,6 +13,11 @@ import android.widget.OverScroller; import java.util.Arrays; +import static ca.allanwang.kau.swipe.SwipeBackHelperKt.SWIPE_EDGE_BOTTOM; +import static ca.allanwang.kau.swipe.SwipeBackHelperKt.SWIPE_EDGE_LEFT; +import static ca.allanwang.kau.swipe.SwipeBackHelperKt.SWIPE_EDGE_RIGHT; +import static ca.allanwang.kau.swipe.SwipeBackHelperKt.SWIPE_EDGE_TOP; + /** * Created by Allan Wang on 2017-07-05. *

@@ -99,6 +104,8 @@ public class ViewDragHelper implements ViewDragHelperExtras { // Distance to travel before a drag may begin private int mTouchSlop; + public int edgeFlag = SWIPE_EDGE_LEFT; + // Last known position/pointer tracking private int mActivePointerId = INVALID_POINTER; private float[] mInitialMotionX; @@ -1221,24 +1228,64 @@ public class ViewDragHelper implements ViewDragHelperExtras { * @param child Child to check * @param dx Motion since initial position along X axis * @param dy Motion since initial position along Y axis - * @return 1 if the touch slop has been crossed on horizontal + * @return 1 if the touch slop has been crossed on our desired axis * - 0 if the touch slop has not been crossed - * - -1 if the touch slop has been crossed on vertical + * - -1 if the touch slop has been crossed on the other axis * - 2 if the touch slop has been crossed on both */ private int checkTouchSlop(View child, float dx, float dy) { if (child == null) { return 0; } - if (dx <= mTouchSlop && Math.abs(dy) <= mTouchSlop) { - return 0; - } else if (dx > mTouchSlop && Math.abs(dy) <= mTouchSlop) { - mDragState = STATE_DRAGGING; - return 1; - } else if (dx <= mTouchSlop && Math.abs(dy) > mTouchSlop) { - mDragState = STATE_IDLE; - cancel(); - return -1; + switch (edgeFlag) { + case SWIPE_EDGE_LEFT: + if (dx <= mTouchSlop && Math.abs(dy) <= mTouchSlop) { + return 0; + } else if (dx > mTouchSlop && Math.abs(dy) <= mTouchSlop) { + mDragState = STATE_DRAGGING; + return 1; + } else if (dx <= mTouchSlop && Math.abs(dy) > mTouchSlop) { + mDragState = STATE_IDLE; + cancel(); + return -1; + } + break; + case SWIPE_EDGE_RIGHT: + if (dx >= -mTouchSlop && Math.abs(dy) <= mTouchSlop) { + return 0; + } else if (dx < -mTouchSlop && Math.abs(dy) <= mTouchSlop) { + mDragState = STATE_DRAGGING; + return 1; + } else if (dx >= mTouchSlop && Math.abs(dy) > mTouchSlop) { + mDragState = STATE_IDLE; + cancel(); + return -1; + } + break; + case SWIPE_EDGE_TOP: + if (dy <= mTouchSlop && Math.abs(dx) <= mTouchSlop) { + return 0; + } else if (dy > mTouchSlop && Math.abs(dx) <= mTouchSlop) { + mDragState = STATE_DRAGGING; + return 1; + } else if (dy <= mTouchSlop && Math.abs(dx) > mTouchSlop) { + mDragState = STATE_IDLE; + cancel(); + return -1; + } + break; + case SWIPE_EDGE_BOTTOM: + if (dy >= -mTouchSlop && Math.abs(dx) <= mTouchSlop) { + return 0; + } else if (dy < -mTouchSlop && Math.abs(dx) <= mTouchSlop) { + mDragState = STATE_DRAGGING; + return 1; + } else if (dy >= mTouchSlop && Math.abs(dx) > mTouchSlop) { + mDragState = STATE_IDLE; + cancel(); + return -1; + } + break; } return 2; } diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt index 909e71b..707a244 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt @@ -93,7 +93,7 @@ class AnimActivity : AppCompatActivity() { true } kauSwipeOnCreate { - edgeFlag = SWIPE_EDGE_LEFT + edgeFlag = SWIPE_EDGE_BOTTOM } } -- cgit v1.2.3