Skip to content

Commit

Permalink
Fixed hard to do onClick
Browse files Browse the repository at this point in the history
  • Loading branch information
chthai64 committed Jan 24, 2018
1 parent 3bc271f commit 4a40648
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
6 changes: 3 additions & 3 deletions swipe-reveal-layout/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 24
versionCode 7
versionName "1.4.0"
versionCode 8
versionName "1.4.1"
}
buildTypes {
release {
Expand Down Expand Up @@ -38,7 +38,7 @@ ext {
siteUrl = 'https://github.com/chthai64/SwipeRevealLayout'
gitUrl = 'https://github.com/chthai64/SwipeRevealLayout.git'

libraryVersion = '1.4.0'
libraryVersion = '1.4.1'

developerId = 'chthai64'
developerName = 'Chau Thai'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ of this software and associated documentation files (the "Software"), to deal
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

@SuppressLint("RtlHardcoded")
public class SwipeRevealLayout extends ViewGroup {
Expand Down Expand Up @@ -118,6 +117,10 @@ public class SwipeRevealLayout extends ViewGroup {

private int mDragEdge = DRAG_EDGE_LEFT;

private float mDragDist = 0;
private float mPrevX = -1;
private float mPrevY = -1;

private ViewDragHelper mDragHelper;
private GestureDetectorCompat mGestureDetector;

Expand Down Expand Up @@ -189,14 +192,25 @@ public boolean onTouchEvent(MotionEvent event) {

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isDragLocked()) {
return super.onInterceptTouchEvent(ev);
}

mDragHelper.processTouchEvent(ev);
mGestureDetector.onTouchEvent(ev);
accumulateDragDist(ev);

boolean couldBecomeClick = couldBecomeClick(ev);
boolean settling = mDragHelper.getViewDragState() == ViewDragHelper.STATE_SETTLING;
boolean idleAfterScrolled = mDragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE
&& mIsScrolling;

return settling || idleAfterScrolled;
// must be placed as the last statement
mPrevX = ev.getX();
mPrevY = ev.getY();

// return true => intercept, cannot trigger onClick event
return !couldBecomeClick && (settling || idleAfterScrolled);
}

@Override
Expand Down Expand Up @@ -684,6 +698,45 @@ private void initRects() {
);
}

private boolean couldBecomeClick(MotionEvent ev) {
return isInMainView(ev) && !shouldInitiateADrag();
}

private boolean isInMainView(MotionEvent ev) {
float x = ev.getX();
float y = ev.getY();

boolean withinVertical = mMainView.getTop() <= y && y <= mMainView.getBottom();
boolean withinHorizontal = mMainView.getLeft() <= x && x <= mMainView.getRight();

return withinVertical && withinHorizontal;
}

private boolean shouldInitiateADrag() {
float minDistToInitiateDrag = mDragHelper.getTouchSlop();
return mDragDist >= minDistToInitiateDrag;
}

private void accumulateDragDist(MotionEvent ev) {
final int action = ev.getAction();
if (action == MotionEvent.ACTION_DOWN) {
mDragDist = 0;
return;
}

boolean dragHorizontally = getDragEdge() == DRAG_EDGE_LEFT ||
getDragEdge() == DRAG_EDGE_RIGHT;

float dragged;
if (dragHorizontally) {
dragged = Math.abs(ev.getX() - mPrevX);
} else {
dragged = Math.abs(ev.getY() - mPrevY);
}

mDragDist += dragged;
}

private void init(Context context, AttributeSet attrs) {
if (attrs != null && context != null) {
TypedArray a = context.getTheme().obtainStyledAttributes(
Expand Down

0 comments on commit 4a40648

Please sign in to comment.