Skip to content

Commit

Permalink
# Fixed strange behaviour if only one drag view available
Browse files Browse the repository at this point in the history
  • Loading branch information
sbra0902 committed Mar 31, 2017
1 parent 736aff9 commit 936b5fd
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 53 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/kaufland/com/demo/example/EasyExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});

Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/layout/easy_example_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@


<kaufland.com.swipelibrary.SwipeLayout_
android:layout_width="200dp"
android:layout_width="match_parent"
android:id="@+id/example_optional"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
Expand All @@ -77,7 +77,7 @@
android:layout_alignParentStart="true">

<kaufland.com.swipelibrary.DragView_
android:layout_width="match_parent"
android:layout_width="wrap_content"
app:position="LEFT_DRAG_VIEW"
android:alpha="0.4"
android:background="@android:color/darker_gray"
Expand All @@ -90,8 +90,9 @@
android:text="-"/>

<TextView
android:layout_width="50dp"
android:layout_width="20dp"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/txt_quantity"
android:text="0"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -135,4 +135,5 @@ public View getSurfaceView() {
public int getSurfaceOpenOffsetByDragView(int dragView) {
return mSwipeViewLayouter.getViewEngines().get(dragView).getOpenOffset();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,28 @@ 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() {
return yDown - yUp;
}

public int getSwipeDirection(){
return getDifX() < 0 ? SWIPE_DIRECTION_RIGHT : SWIPE_DIRECTION_LEFT;
return getDifX() > 0 ? SWIPE_DIRECTION_RIGHT : SWIPE_DIRECTION_LEFT;
}

public boolean isHorizontalScrollChangedWhileDragging() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ public class SwipeLayout extends FrameLayout {

private boolean mSwipeEnabled = true;

private float mDownX;

private float mDownY;

private float mDragHelperTouchSlop;


Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
}


Expand Down Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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());
}
}

Expand All @@ -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();
Expand Down Expand Up @@ -103,14 +104,22 @@ 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
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 936b5fd

Please sign in to comment.