From 936b5fdd96fd854c18e7fdd47e1b568ee46a4cab Mon Sep 17 00:00:00 2001 From: Sven Braune Date: Fri, 31 Mar 2017 19:18:05 +0200 Subject: [PATCH] # Fixed strange behaviour if only one drag view available --- .../com/demo/example/EasyExample.java | 4 +- .../main/res/layout/easy_example_fragment.xml | 7 +-- .../com/swipelibrary/DraggingProxy.java | 11 ++-- .../swipelibrary/SwipeDirectionDetector.java | 13 +++-- .../com/swipelibrary/SwipeLayout.java | 52 +++++++------------ .../dragengine/LeftDragViewEngine.java | 2 +- .../dragengine/RightDragViewEngine.java | 15 ++++-- .../dragengine/SurfaceViewEngine.java | 6 +-- 8 files changed, 57 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/kaufland/com/demo/example/EasyExample.java b/app/src/main/java/kaufland/com/demo/example/EasyExample.java index 1e61e0c..bed65c1 100644 --- a/app/src/main/java/kaufland/com/demo/example/EasyExample.java +++ b/app/src/main/java/kaufland/com/demo/example/EasyExample.java @@ -38,14 +38,14 @@ public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) view.findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - mQuantityTextView.setText(Integer.valueOf(mQuantityTextView.getText().toString()) +1); + mQuantityTextView.setText(String.valueOf(Integer.valueOf(mQuantityTextView.getText().toString()) +1)); } }); view.findViewById(R.id.btn_minus).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - mQuantityTextView.setText(Integer.valueOf(mQuantityTextView.getText().toString()) -1); + mQuantityTextView.setText(String.valueOf(Integer.valueOf(mQuantityTextView.getText().toString()) -1)); } }); diff --git a/app/src/main/res/layout/easy_example_fragment.xml b/app/src/main/res/layout/easy_example_fragment.xml index 8bcbf92..8fb5ca9 100644 --- a/app/src/main/res/layout/easy_example_fragment.xml +++ b/app/src/main/res/layout/easy_example_fragment.xml @@ -67,7 +67,7 @@ diff --git a/swipelibrary/src/main/java/kaufland/com/swipelibrary/DraggingProxy.java b/swipelibrary/src/main/java/kaufland/com/swipelibrary/DraggingProxy.java index b68a7a5..3c9459c 100644 --- a/swipelibrary/src/main/java/kaufland/com/swipelibrary/DraggingProxy.java +++ b/swipelibrary/src/main/java/kaufland/com/swipelibrary/DraggingProxy.java @@ -101,14 +101,14 @@ public boolean isCapturedViewDraggable(View child) { return draggable; } - public boolean canSwipe(float x1, float y1, float x2, float y2, SwipeState.DragViewState state) { + public boolean canSwipe(SwipeDirectionDetector swipeDirectionDetector, SwipeState.DragViewState state) { boolean canSwipe = false; - float absDiffX = Math.abs(x1 - x2); - float absDiffY = Math.abs(y1 - y2); - float diffX = x2 - x1; - float diffY = y2 - y1; + float absDiffX = Math.abs(swipeDirectionDetector.getDifX()); + float absDiffY = Math.abs(swipeDirectionDetector.getDifY()); + float diffX = swipeDirectionDetector.getDifX(); + float diffY = swipeDirectionDetector.getDifY(); boolean isLeftDraggable = mSwipeViewLayouter.getDragViewEngineByPosition(LEFT_DRAG_VIEW) != null ? ((DragView)mSwipeViewLayouter.getViews().get(LEFT_DRAG_VIEW)).isDraggable() : false; boolean isRightDraggable = mSwipeViewLayouter.getDragViewEngineByPosition(RIGHT_DRAG_VIEW) != null ? ((DragView)mSwipeViewLayouter.getViews().get(RIGHT_DRAG_VIEW)).isDraggable() : false; @@ -135,4 +135,5 @@ public View getSurfaceView() { public int getSurfaceOpenOffsetByDragView(int dragView) { return mSwipeViewLayouter.getViewEngines().get(dragView).getOpenOffset(); } + } diff --git a/swipelibrary/src/main/java/kaufland/com/swipelibrary/SwipeDirectionDetector.java b/swipelibrary/src/main/java/kaufland/com/swipelibrary/SwipeDirectionDetector.java index 29958a4..e397704 100644 --- a/swipelibrary/src/main/java/kaufland/com/swipelibrary/SwipeDirectionDetector.java +++ b/swipelibrary/src/main/java/kaufland/com/swipelibrary/SwipeDirectionDetector.java @@ -36,15 +36,20 @@ public void onActionDown(float x, float y, final SwipeLayout swipeLayout) { } public void onActionUp(float x, float y, final SwipeLayout swipeLayout) { - xUp = (int) x; - yUp = (int) y; + + onAction(x, y); upRect = new Rect(); swipeLayout.getGlobalVisibleRect(upRect); } + public void onAction(float x, float y){ + xUp = (int) x; + yUp = (int) y; + } + public int getDifX() { - return xDown - xUp; + return xUp - xDown; } public int getDifY() { @@ -52,7 +57,7 @@ public int getDifY() { } public int getSwipeDirection(){ - return getDifX() < 0 ? SWIPE_DIRECTION_RIGHT : SWIPE_DIRECTION_LEFT; + return getDifX() > 0 ? SWIPE_DIRECTION_RIGHT : SWIPE_DIRECTION_LEFT; } public boolean isHorizontalScrollChangedWhileDragging() { diff --git a/swipelibrary/src/main/java/kaufland/com/swipelibrary/SwipeLayout.java b/swipelibrary/src/main/java/kaufland/com/swipelibrary/SwipeLayout.java index 7c58959..81a17bc 100644 --- a/swipelibrary/src/main/java/kaufland/com/swipelibrary/SwipeLayout.java +++ b/swipelibrary/src/main/java/kaufland/com/swipelibrary/SwipeLayout.java @@ -58,10 +58,6 @@ public class SwipeLayout extends FrameLayout { private boolean mSwipeEnabled = true; - private float mDownX; - - private float mDownY; - private float mDragHelperTouchSlop; @@ -102,19 +98,20 @@ public boolean onTouchEvent(MotionEvent ev) { mDragHelper.abort(); mSwipeDirectionDetector.onActionDown(ev.getX(), ev.getY(), this); - mDownX = ev.getX(); - mDownY = ev.getY(); mDragHelper.processTouchEvent(ev); return true; case MotionEvent.ACTION_MOVE: - float moveX = ev.getX(); - float moveY = ev.getY(); + mSwipeDirectionDetector.onAction(ev.getX(), ev.getY()); - boolean canSwipe = mDraggingProxy.canSwipe(mDownX, mDownY, moveX, moveY, mSwipeState.getState()); + boolean canSwipe = mDraggingProxy.canSwipe(mSwipeDirectionDetector, mSwipeState.getState()); mDragAllowed = canSwipe; - mIsDragging = true; + + boolean isClick = mDragHelperTouchSlop > Math.abs(mSwipeDirectionDetector.getDifX()); + if (!isClick) { + mIsDragging = true; + } if (mIsDragging && mDragAllowed) { getParent().requestDisallowInterceptTouchEvent(true); @@ -145,22 +142,16 @@ public boolean onTouchEvent(MotionEvent ev) { case MotionEvent.ACTION_CANCEL: mIsDragging = false; - float lastX = ev.getX(); - float lastY = ev.getY(); -// boolean swipeable = SwipeUtil.canSwipe(mDownX, mDownY, lastX, lastY, mSwipeState.getState(), mViewLayouter); -// if (swipeable) { mDragHelper.processTouchEvent(ev); -// } else { -// mDragHelper.cancel(); -// } + break; default: mDragHelper.processTouchEvent(ev); } - return mDragAllowed; + return mIsDragging; } @Override @@ -173,24 +164,23 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = MotionEventCompat.getActionMasked(ev); - final float x = ev.getX(); - final float y = ev.getY(); - switch (action) { case MotionEvent.ACTION_DOWN: + mSwipeDirectionDetector.onActionDown(ev.getX(), ev.getY(), this); mDragHelper.abort(); mDragHelper.processTouchEvent(ev); - mDownX = x; - mDownY = y; mIsDragging = false; break; case MotionEvent.ACTION_MOVE: - mDragHelper.processTouchEvent(ev); - float point = mDraggingProxy.getDragDirection() == HORIZONTAL ? x : y; - float oldPoint = mDraggingProxy.getDragDirection() == HORIZONTAL ? mDownX : mDownY; - boolean isClick = mDragHelperTouchSlop > Math.abs(point - oldPoint); + try { + mDragHelper.processTouchEvent(ev); + } catch (IllegalArgumentException e) { + // https://code.google.com/p/android/issues/detail?id=64553 + } + + boolean isClick = mDragHelperTouchSlop > Math.abs(mSwipeDirectionDetector.getDifX()); if (!mIsDragging && isClick) { return false; @@ -207,15 +197,13 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: - mIsDragging = false; - float diffX = Math.abs(mDownX - ev.getX()); - float diffY = Math.abs(mDownY - ev.getY()); + mIsDragging = false; mDragHelper.processTouchEvent(ev); break; } - return mDragHelper.shouldInterceptTouchEvent(ev); + return mIsDragging; } @@ -359,7 +347,7 @@ public int getViewVerticalDragRange(View child) { public void markForRestoreOnDraw(SwipeState.DragViewState swipeState) { mSwipeState.setState(swipeState); mRestoreOnDraw = true; - if(mDragHelper.getViewDragState() != KDragViewHelper.STATE_IDLE){ + if (mDragHelper.getViewDragState() != KDragViewHelper.STATE_IDLE) { mDragHelper.abort(); } } diff --git a/swipelibrary/src/main/java/kaufland/com/swipelibrary/dragengine/LeftDragViewEngine.java b/swipelibrary/src/main/java/kaufland/com/swipelibrary/dragengine/LeftDragViewEngine.java index a6994f6..d22a63a 100644 --- a/swipelibrary/src/main/java/kaufland/com/swipelibrary/dragengine/LeftDragViewEngine.java +++ b/swipelibrary/src/main/java/kaufland/com/swipelibrary/dragengine/LeftDragViewEngine.java @@ -115,7 +115,7 @@ public int getOpenOffset() { @Override public SwipeResult determineSwipeHorizontalState(float velocity, SwipeDirectionDetector swipeDirectionDetector, SwipeState swipeState, final SwipeLayout.SwipeListener swipeListener, View releasedChild) { - if (mDragView.equals(releasedChild) && swipeDirectionDetector.getSwipeDirection() == SWIPE_DIRECTION_LEFT) { + if (mDragView.equals(releasedChild) && swipeDirectionDetector.getSwipeDirection() == SWIPE_DIRECTION_LEFT && Math.abs(swipeDirectionDetector.getDifX()) > (getDragDistance() / 2)) { swipeState.setState(SwipeState.DragViewState.CLOSED); return new SwipeResult(-mDragView.getWidth(), new Runnable() { @Override diff --git a/swipelibrary/src/main/java/kaufland/com/swipelibrary/dragengine/RightDragViewEngine.java b/swipelibrary/src/main/java/kaufland/com/swipelibrary/dragengine/RightDragViewEngine.java index 99852cf..2e54ae6 100644 --- a/swipelibrary/src/main/java/kaufland/com/swipelibrary/dragengine/RightDragViewEngine.java +++ b/swipelibrary/src/main/java/kaufland/com/swipelibrary/dragengine/RightDragViewEngine.java @@ -13,6 +13,7 @@ import static kaufland.com.swipelibrary.SwipeDirectionDetector.SWIPE_DIRECTION_RIGHT; import static kaufland.com.swipelibrary.SwipeState.DragViewState.CLOSED; +import static kaufland.com.swipelibrary.SwipeState.DragViewState.RIGHT_OPEN; /** * Created by sbra0902 on 29.03.17. @@ -39,7 +40,7 @@ public RightDragViewEngine(SwipeViewLayouter layouter) { public void moveView(float offset, SurfaceView view, View changedView) { if (!mDragView.equals(changedView)) { - mDragView.setX(view.getX() + mDragView.getWidth()); + mDragView.setX(view.getX() + mSurfaceView.getWidth()); } } @@ -49,7 +50,7 @@ public void initializePosition(SwipeViewLayouter.DragDirection orientation) { mDragView = (DragView) mLayouter.getViews().get(SwipeLayout.RIGHT_DRAG_VIEW); mSurfaceView = mLayouter.getSurfaceView(); mDragDistance = mDragView.getWidth(); - mInitialXPos = (int) (mSurfaceView.getX() + mDragView.getWidth()); + mInitialXPos = (int) (mSurfaceView.getX() + mSurfaceView.getWidth()); mIntermmediateDistance = mDragView.getSettlePointResourceId() != -1 ? mDragView.findViewById(mDragView.getSettlePointResourceId()).getRight() : mDragView.getWidth(); moveToInitial(); @@ -103,7 +104,7 @@ public int getIntermmediateDistance() { @Override public SwipeResult determineSwipeHorizontalState(float velocity, SwipeDirectionDetector swipeDirectionDetector, SwipeState swipeState, final SwipeLayout.SwipeListener swipeListener, View releasedChild) { - if (releasedChild.equals(getDragView()) && swipeDirectionDetector.getSwipeDirection() == SWIPE_DIRECTION_RIGHT) { + if (releasedChild.equals(getDragView()) && swipeDirectionDetector.getSwipeDirection() == SWIPE_DIRECTION_RIGHT && Math.abs(swipeDirectionDetector.getDifX()) > (getDragDistance() / 2)) { swipeState.setState(SwipeState.DragViewState.CLOSED); return new SwipeResult(mSurfaceView.getWidth(), new Runnable() { @Override @@ -111,6 +112,14 @@ public void run() { swipeListener.onSwipeClosed(CLOSED); } }); + }else if(releasedChild.equals(getDragView())){ + swipeState.setState(RIGHT_OPEN); + return new SwipeResult(mSurfaceView.getWidth() - getDragDistance(), new Runnable() { + @Override + public void run() { + swipeListener.onSwipeClosed(RIGHT_OPEN); + } + }); } return null; diff --git a/swipelibrary/src/main/java/kaufland/com/swipelibrary/dragengine/SurfaceViewEngine.java b/swipelibrary/src/main/java/kaufland/com/swipelibrary/dragengine/SurfaceViewEngine.java index a4ecc64..8d2c4b0 100644 --- a/swipelibrary/src/main/java/kaufland/com/swipelibrary/dragengine/SurfaceViewEngine.java +++ b/swipelibrary/src/main/java/kaufland/com/swipelibrary/dragengine/SurfaceViewEngine.java @@ -188,14 +188,14 @@ public int clampViewPositionHorizontal(View child, int left) { } else { boolean isOutsideRightRangeAndBounceNotPossible = left < -mRightDragViewEngine.getDragDistance() && !getDragViewForEngine(mRightDragViewEngine).isBouncePossible(); if (isOutsideRightRangeAndBounceNotPossible) { - return mRightDragViewEngine.getDragDistance(); + return -mRightDragViewEngine.getDragDistance(); } } if (mLeftDragViewEngine == null) { - if (left >= mSurfaceView.getWidth()) { - return 0; + if (left <= -mRightDragViewEngine.getDragDistance()) { + return -mRightDragViewEngine.getDragDistance(); } } else { boolean isOutsideLeftRangeAndBounceNotPossible = left > mLeftDragViewEngine.getDragDistance() && !getDragViewForEngine(mLeftDragViewEngine).isBouncePossible();