Example usage for android.view MotionEvent setAction

List of usage examples for android.view MotionEvent setAction

Introduction

In this page you can find the example usage for android.view MotionEvent setAction.

Prototype

public final void setAction(int action) 

Source Link

Document

Sets this event's action.

Usage

From source file:com.cocarechina.pullrefreshview.layout.FlingLayout.java

/******************************************************************/

@Override// w ww.  j  av  a  2s.c o m
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (mPullView != null && !ViewCompat.isNestedScrollingEnabled(mPullView)) {
        float moveY = getMoveY();
        int pointerCount = ev.getPointerCount();
        int pointerIndex = ev.getActionIndex();
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        switch (ev.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            mPointerId = ev.getPointerId(pointerIndex);
            float x = ev.getX(pointerIndex);
            float y = ev.getY(pointerIndex);
            tepmY = downY = y;
            tepmX = downX = x;
            tempStateType = SCROLL_STATE_TOUCH_SCROLL;
            if (moveY != 0) {
                return true;
            }
            lastY = ev.getY();
            Log.v("lastY", moveY + "");
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            mPointerId = ev.getPointerId(pointerIndex);
            tepmX = ev.getX(pointerIndex);
            tepmY = ev.getY(pointerIndex);
            break;
        case MotionEvent.ACTION_MOVE:
            pointerIndex = ev.findPointerIndex(mPointerId);
            float mx;
            float my;
            if (pointerCount > pointerIndex && pointerIndex >= 0) {
                mx = ev.getX(pointerIndex);
                my = ev.getY(pointerIndex);
            } else {
                mx = ev.getX();
                my = ev.getY();
            }
            //?????
            int dataX = (int) (mx - tepmX);
            int dataY = (int) (my - tepmY);
            tepmX = mx;
            tepmY = my;
            if (isScrolling || (Math.abs(dataY) > Math.abs(dataX))) {
                isScrolling = true;
                if (moveY == 0) {
                    // 0,0
                    //??
                    if ((dataY < 0 && canPullUp()) || (dataY > 0 && canPullDown())) {
                        moveBy(dataY);
                        return true;
                    }
                } else {
                    //?0,0
                    ev.setAction(MotionEvent.ACTION_CANCEL);//?

                    if ((moveY < 0 && moveY + dataY >= 0) || (moveY > 0 && moveY + dataY <= 0)) {
                        //0,0
                        ev.setAction(MotionEvent.ACTION_DOWN);
                        moveTo(0);
                    } else if ((moveY > 0 && dataY > 0) || (moveY < 0 && dataY < 0)) {
                        //??
                        if (maxDistance == 0 || Math.abs(moveY) < maxDistance) {
                            int ps = 0;
                            int hDataY = dataY / 2;
                            if (maxDistance == 0) {
                                ps = (int) (-hDataY * Math.abs(moveY) / (float) MAXDISTANCE) - hDataY;
                            } else {
                                ps = (int) (-hDataY * Math.abs(moveY) / (float) maxDistance) - hDataY;
                            }
                            moveBy(ps + dataY);
                        } else if (moveY > maxDistance) {

                            moveTo(maxDistance);
                        } else if (moveY < -maxDistance) {
                            moveTo(-maxDistance);
                        }
                    } else {
                        moveBy(dataY);
                    }
                }
            } else {
                ev.setLocation(mx, downY);
            }

            Log.v("flingLayout", "ev.getY()" + ev.getY() + "  ---  lastY " + lastY);
            if (ev.getY() - lastY > 5) {
                if (monScrollHeadListener != null) {
                    monScrollHeadListener.onScrollBottom();
                }
            } else if (ev.getY() - lastY < -10) {
                if (monScrollHeadListener != null) {
                    monScrollHeadListener.onScrollTop();
                }
            }

            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            startFling();
            isScrolling = false;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            // ??
            int pointerIdLeave = ev.getPointerId(pointerIndex);
            if (mPointerId == pointerIdLeave) {
                // ??????VelocityTracker
                int reIndex = pointerIndex == 0 ? 1 : 0;
                mPointerId = ev.getPointerId(reIndex);
                // ?
                tepmY = ev.getY(reIndex);
            }
        }
        return super.dispatchTouchEvent(ev) || isScrolling;
    } else {
        return super.dispatchTouchEvent(ev);
    }

}

From source file:com.app.gongza.libs.view.scrollablelayout.ScrollableLayout.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    float currentX = ev.getX();
    float currentY = ev.getY();
    float deltaY;
    int shiftX = (int) Math.abs(currentX - mDownX);
    int shiftY = (int) Math.abs(currentY - mDownY);
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mDisallowIntercept = false;/*from  w ww .  j a v a 2  s  . c  o m*/
        needCheckUpdown = true;
        updown = true;
        mDownX = currentX;
        mDownY = currentY;
        mLastY = currentY;
        checkIsClickHead((int) currentY, mHeadHeight, getScrollY());
        checkIsClickHeadExpand((int) currentY, mHeadHeight, getScrollY());
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        mScroller.forceFinished(true);
        break;
    case MotionEvent.ACTION_MOVE:
        if (mDisallowIntercept) {
            break;
        }
        initVelocityTrackerIfNotExists();
        mVelocityTracker.addMovement(ev);
        deltaY = mLastY - currentY;
        if (needCheckUpdown) {
            if (shiftX > mTouchSlop && shiftX > shiftY) {
                needCheckUpdown = false;
                updown = false;
            } else if (shiftY > mTouchSlop && shiftY > shiftX) {
                needCheckUpdown = false;
                updown = true;
            }
        }

        if (updown && shiftY > mTouchSlop && shiftY > shiftX
                && (!isSticked() || mHelper.isTop() || isClickHeadExpand)) {

            if (childViewPager != null) {
                childViewPager.requestDisallowInterceptTouchEvent(true);
            }
            scrollBy(0, (int) (deltaY + 0.5));
        }
        mLastY = currentY;
        break;
    case MotionEvent.ACTION_UP:
        if (updown && shiftY > shiftX && shiftY > mTouchSlop) {
            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            float yVelocity = -mVelocityTracker.getYVelocity();
            boolean dislowChild = false;
            if (Math.abs(yVelocity) > mMinimumVelocity) {
                mDirection = yVelocity > 0 ? DIRECTION.UP : DIRECTION.DOWN;
                if ((mDirection == DIRECTION.UP && isSticked())
                        || (!isSticked() && getScrollY() == 0 && mDirection == DIRECTION.DOWN)) {
                    dislowChild = true;
                } else {
                    mScroller.fling(0, getScrollY(), 0, (int) yVelocity, 0, 0, -Integer.MAX_VALUE,
                            Integer.MAX_VALUE);
                    mScroller.computeScrollOffset();
                    mLastScrollerY = getScrollY();
                    invalidate();
                }
            }
            if (!dislowChild && (isClickHead || !isSticked())) {
                int action = ev.getAction();
                ev.setAction(MotionEvent.ACTION_CANCEL);
                boolean dispathResult = super.dispatchTouchEvent(ev);
                ev.setAction(action);
                return dispathResult;
            }
        }
        break;
    default:
        break;
    }
    super.dispatchTouchEvent(ev);
    return true;
}

From source file:com.chengmeng.tools.views.slidepage.ScrollableLayout.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    float currentX = ev.getX();
    float currentY = ev.getY();
    float deltaY;
    int shiftX = (int) Math.abs(currentX - mDownX);
    int shiftY = (int) Math.abs(currentY - mDownY);
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        is_touching = 0;//  w ww.  j a va 2 s  .c  o m
        mDisallowIntercept = false;
        needCheckUpdown = true;
        updown = true;
        mDownX = currentX;
        mDownY = currentY;
        mLastY = currentY;
        checkIsClickHead((int) currentY, mHeadHeight, getScrollY());
        checkIsClickHeadExpand((int) currentY, mHeadHeight, getScrollY());
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        mScroller.forceFinished(true);
        break;
    case MotionEvent.ACTION_MOVE:
        is_touching++;
        if (mDisallowIntercept) {
            break;
        }
        initVelocityTrackerIfNotExists();
        mVelocityTracker.addMovement(ev);
        deltaY = mLastY - currentY;
        if (needCheckUpdown) {
            if (shiftX > mTouchSlop && shiftX > shiftY) {
                needCheckUpdown = false;
                updown = false;
            } else if (shiftY > mTouchSlop && shiftY > shiftX) {
                needCheckUpdown = false;
                updown = true;
            }
        }
        if (updown && shiftY > mTouchSlop && shiftY > shiftX
                && (!isSticked() || mHelper.isTop() || isClickHeadExpand)) {

            if (childViewPager != null) {
                childViewPager.requestDisallowInterceptTouchEvent(true);
            }
            scrollBy(0, (int) (deltaY + 0.5));
        }
        mLastY = currentY;
        break;
    case MotionEvent.ACTION_UP:
        if (updown && shiftY > shiftX && shiftY > mTouchSlop) {
            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            float yVelocity = -mVelocityTracker.getYVelocity();
            boolean dislowChild = false;
            if (Math.abs(yVelocity) > mMinimumVelocity) {
                mDirection = yVelocity > 0 ? DIRECTION.UP : DIRECTION.DOWN;
                if ((mDirection == DIRECTION.UP && isSticked())
                        || (!isSticked() && getScrollY() == 0 && mDirection == DIRECTION.DOWN)) {
                    dislowChild = true;
                } else {
                    mScroller.fling(0, getScrollY(), 0, (int) yVelocity, 0, 0, -Integer.MAX_VALUE,
                            Integer.MAX_VALUE);
                    mScroller.computeScrollOffset();
                    mLastScrollerY = getScrollY();
                    invalidate();
                }
            }
            if (!dislowChild && (isClickHead || !isSticked())) {
                int action = ev.getAction();
                ev.setAction(MotionEvent.ACTION_CANCEL);
                boolean dispathResult = super.dispatchTouchEvent(ev);
                ev.setAction(action);
                return dispathResult;
            }
        }
        break;
    default:
        break;
    }
    super.dispatchTouchEvent(ev);
    return true;
}

From source file:com.hks360.library.mtech.swipe.SwipeMenuLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mVelocityTracker == null)
        mVelocityTracker = VelocityTracker.obtain();
    mVelocityTracker.addMovement(ev);//w  w w.  ja  v  a 2  s . c  o  m
    int dx;
    int dy;
    int action = ev.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mLastX = (int) ev.getX();
        mLastY = (int) ev.getY();
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        if (!isSwipeEnable())
            break;
        int disX = (int) (mLastX - ev.getX());
        int disY = (int) (mLastY - ev.getY());
        if (!mDragging && Math.abs(disX) > mScaledTouchSlop && Math.abs(disX) > Math.abs(disY)) {
            mDragging = true;
        }
        if (mDragging) {
            if (mSwipeCurrentHorizontal == null || shouldResetSwipe) {
                if (disX < 0) {
                    if (mSwipeLeftHorizontal != null)
                        mSwipeCurrentHorizontal = mSwipeLeftHorizontal;
                    else
                        mSwipeCurrentHorizontal = mSwipeRightHorizontal;
                } else {
                    if (mSwipeRightHorizontal != null)
                        mSwipeCurrentHorizontal = mSwipeRightHorizontal;
                    else
                        mSwipeCurrentHorizontal = mSwipeLeftHorizontal;
                }
            }
            scrollBy(disX, 0);
            mLastX = (int) ev.getX();
            mLastY = (int) ev.getY();
            shouldResetSwipe = false;
        }
        break;
    }
    case MotionEvent.ACTION_UP: {
        dx = (int) (mDownX - ev.getX());
        dy = (int) (mDownY - ev.getY());
        mDragging = false;
        mVelocityTracker.computeCurrentVelocity(1000, mScaledMaximumFlingVelocity);
        int velocityX = (int) mVelocityTracker.getXVelocity();
        int velocity = Math.abs(velocityX);
        if (velocity > mScaledMinimumFlingVelocity) {
            if (mSwipeCurrentHorizontal != null) {
                int duration = getSwipeDuration(ev, velocity);
                if (mSwipeCurrentHorizontal instanceof SwipeRightHorizontal) {
                    if (velocityX < 0) {
                        smoothOpenMenu(duration);
                    } else {
                        smoothCloseMenu(duration);
                    }
                } else {
                    if (velocityX > 0) {
                        smoothOpenMenu(duration);
                    } else {
                        smoothCloseMenu(duration);
                    }
                }
                ViewCompat.postInvalidateOnAnimation(this);
            }
        } else {
            judgeOpenClose(dx, dy);
        }
        mVelocityTracker.clear();
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        if (Math.abs(mDownX - ev.getX()) > mScaledTouchSlop || Math.abs(mDownY - ev.getY()) > mScaledTouchSlop
                || isLeftMenuOpen() || isRightMenuOpen()) {
            ev.setAction(MotionEvent.ACTION_CANCEL);
            super.onTouchEvent(ev);
            return true;
        }
        break;
    }
    case MotionEvent.ACTION_CANCEL: {
        mDragging = false;
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        } else {
            dx = (int) (mDownX - ev.getX());
            dy = (int) (mDownY - ev.getY());
            judgeOpenClose(dx, dy);
        }
        break;
    }
    }
    return super.onTouchEvent(ev);
}

From source file:com.andview.refreshview.swipe.SwipeMenuLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mVelocityTracker == null)
        mVelocityTracker = VelocityTracker.obtain();
    mVelocityTracker.addMovement(ev);/*from  w  w  w  .j  a  va  2s  .  co  m*/
    int dx;
    int dy;
    int action = ev.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastX = (int) ev.getX();
        mLastY = (int) ev.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if (!isSwipeEnable())
            break;
        int disX = (int) (mLastX - ev.getX());
        int disY = (int) (mLastY - ev.getY());
        if (!mDragging && Math.abs(disX) > mScaledTouchSlop && Math.abs(disX) > Math.abs(disY)) {
            mDragging = true;
        }
        if (mDragging) {
            if (mSwipeCurrentHorizontal == null || shouldResetSwipe) {
                if (disX < 0) {
                    if (mSwipeLeftHorizontal != null)
                        mSwipeCurrentHorizontal = mSwipeLeftHorizontal;
                    else
                        mSwipeCurrentHorizontal = mSwipeRightHorizontal;
                } else {
                    if (mSwipeRightHorizontal != null)
                        mSwipeCurrentHorizontal = mSwipeRightHorizontal;
                    else
                        mSwipeCurrentHorizontal = mSwipeLeftHorizontal;
                }
            }
            scrollBy(disX, 0);
            mLastX = (int) ev.getX();
            mLastY = (int) ev.getY();
            shouldResetSwipe = false;
        }
        break;
    case MotionEvent.ACTION_UP:
        dx = (int) (mDownX - ev.getX());
        dy = (int) (mDownY - ev.getY());
        mDragging = false;
        mVelocityTracker.computeCurrentVelocity(1000, mScaledMaximumFlingVelocity);
        int velocityX = (int) mVelocityTracker.getXVelocity();
        int velocity = Math.abs(velocityX);
        if (velocity > mScaledMinimumFlingVelocity) {
            if (mSwipeCurrentHorizontal != null) {
                int duration = getSwipeDuration(ev, velocity);
                if (mSwipeCurrentHorizontal instanceof SwipeRightHorizontal) {
                    if (velocityX < 0) {
                        smoothOpenMenu(duration);
                    } else {
                        smoothCloseMenu(duration);
                    }
                } else {
                    if (velocityX > 0) {
                        smoothOpenMenu(duration);
                    } else {
                        smoothCloseMenu(duration);
                    }
                }
                ViewCompat.postInvalidateOnAnimation(this);
            }
        } else {
            judgeOpenClose(dx, dy);
        }
        mVelocityTracker.clear();
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        if (Math.abs(mDownX - ev.getX()) > mScaledTouchSlop || Math.abs(mDownY - ev.getY()) > mScaledTouchSlop
                || isMenuOpen()) {
            ev.setAction(MotionEvent.ACTION_CANCEL);
            super.onTouchEvent(ev);
            return true;
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        mDragging = false;
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        } else {
            dx = (int) (mDownX - ev.getX());
            dy = (int) (mDownY - ev.getY());
            judgeOpenClose(dx, dy);
        }
        break;
    }
    return super.onTouchEvent(ev);
}

From source file:com.example.administrator.myapplication.recycler_swipe.swpie.SwipeMenuLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mVelocityTracker == null)
        mVelocityTracker = VelocityTracker.obtain();
    mVelocityTracker.addMovement(ev);//from   www. j  a  v  a  2  s  .  co  m
    int dx;
    int dy;
    int action = ev.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mLastX = (int) ev.getX();
        mLastY = (int) ev.getY();
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        if (!isSwipeEnable())
            break;
        int disX = (int) (mLastX - ev.getX());
        int disY = (int) (mLastY - ev.getY());
        if (!mDragging && Math.abs(disX) > mScaledTouchSlop && Math.abs(disX) > Math.abs(disY)) {
            mDragging = true;
        }
        if (mDragging) {
            if (mSwipeCurrentHorizontal == null || shouldResetSwipe) {
                if (disX < 0) {
                    if (mSwipeLeftHorizontal != null)
                        mSwipeCurrentHorizontal = mSwipeLeftHorizontal;
                    else
                        mSwipeCurrentHorizontal = mSwipeRightHorizontal;
                } else {
                    if (mSwipeRightHorizontal != null)
                        mSwipeCurrentHorizontal = mSwipeRightHorizontal;
                    else
                        mSwipeCurrentHorizontal = mSwipeLeftHorizontal;
                }
            }
            scrollBy(disX, 0);
            mLastX = (int) ev.getX();
            mLastY = (int) ev.getY();
            shouldResetSwipe = false;
        }
        break;
    }
    case MotionEvent.ACTION_UP: {
        dx = (int) (mDownX - ev.getX());
        dy = (int) (mDownY - ev.getY());
        mDragging = false;
        mVelocityTracker.computeCurrentVelocity(1000, mScaledMaximumFlingVelocity);
        int velocityX = (int) mVelocityTracker.getXVelocity();
        int velocity = Math.abs(velocityX);
        //?menu
        if (velocity > mScaledMinimumFlingVelocity) {
            if (mSwipeCurrentHorizontal != null) {
                int duration = getSwipeDuration(ev, velocity);
                if (mSwipeCurrentHorizontal instanceof SwipeRightHorizontal) {
                    if (velocityX < 0) {
                        smoothOpenMenu(duration);
                    } else {
                        smoothCloseMenu(duration);
                    }
                } else {
                    if (velocityX > 0) {
                        smoothOpenMenu(duration);
                    } else {
                        smoothCloseMenu(duration);
                    }
                }
                ViewCompat.postInvalidateOnAnimation(this);
            }
        } else {
            judgeOpenClose(dx, dy);
        }
        mVelocityTracker.clear();
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        if (Math.abs(mDownX - ev.getX()) > mScaledTouchSlop || Math.abs(mDownY - ev.getY()) > mScaledTouchSlop
                || isLeftMenuOpen() || isRightMenuOpen()) {
            ev.setAction(MotionEvent.ACTION_CANCEL);
            super.onTouchEvent(ev);
            return true;
        }
        break;
    }
    case MotionEvent.ACTION_CANCEL: {
        mDragging = false;
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        } else {
            dx = (int) (mDownX - ev.getX());
            dy = (int) (mDownY - ev.getY());
            judgeOpenClose(dx, dy);
        }
        break;
    }
    }
    return super.onTouchEvent(ev);
}

From source file:com.lfq.pulltorefresh.library.sideslip.SwipeMenuLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mVelocityTracker == null)
        mVelocityTracker = VelocityTracker.obtain();
    mVelocityTracker.addMovement(ev);// w ww .  j  a v  a 2  s  .  c o m
    int dx;
    int dy;
    int action = ev.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastX = (int) ev.getX();
        mLastY = (int) ev.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if (!isSwipeEnable())
            break;
        int disX = (int) (mLastX - ev.getX());
        int disY = (int) (mLastY - ev.getY());
        if (!mDragging && Math.abs(disX) > mScaledTouchSlop && Math.abs(disX) > Math.abs(disY)) {
            mDragging = true;
        }
        if (mDragging) {
            // x view
            if (mSwipeCurrentHorizontal == null || shouldResetSwipe) {
                //                        if (disX < 0) {
                //                            if (mSwipeLeftHorizontal != null)
                //                                mSwipeCurrentHorizontal = mSwipeLeftHorizontal;
                //                            else
                //                                mSwipeCurrentHorizontal = mSwipeRightHorizontal;
                //                        } else {
                //                            if (mSwipeRightHorizontal != null)
                //                                mSwipeCurrentHorizontal = mSwipeRightHorizontal;
                //                            else
                //                                mSwipeCurrentHorizontal = mSwipeLeftHorizontal;
                //                        }
                mSwipeCurrentHorizontal = mSwipeRightHorizontal;
            }
            scrollBy(disX, 0);
            mLastX = (int) ev.getX();
            mLastY = (int) ev.getY();
            shouldResetSwipe = false;
        }
        break;
    case MotionEvent.ACTION_UP:
        dx = (int) (mDownX - ev.getX());
        dy = (int) (mDownY - ev.getY());
        mDragging = false;
        mVelocityTracker.computeCurrentVelocity(1000, mScaledMaximumFlingVelocity);
        int velocityX = (int) mVelocityTracker.getXVelocity();
        int velocity = Math.abs(velocityX);
        if (velocity > mScaledMinimumFlingVelocity) {
            if (mSwipeCurrentHorizontal != null) {
                int duration = getSwipeDuration(ev, velocity);
                if (mSwipeCurrentHorizontal instanceof SwipeRightHorizontal) {
                    if (velocityX < 0) {
                        smoothOpenMenu(duration);
                    } else {
                        smoothCloseMenu(duration);
                    }
                } else {
                    if (velocityX > 0) {
                        smoothOpenMenu(duration);
                    } else {
                        smoothCloseMenu(duration);
                    }
                }
                ViewCompat.postInvalidateOnAnimation(this);
            }
        } else {
            judgeOpenClose(dx, dy);
        }
        mVelocityTracker.clear();
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        if (Math.abs(mDownX - ev.getX()) > mScaledTouchSlop || Math.abs(mDownY - ev.getY()) > mScaledTouchSlop
                || isMenuOpen()) {
            ev.setAction(MotionEvent.ACTION_CANCEL);
            super.onTouchEvent(ev);
            return true;
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        mDragging = false;
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        } else {
            dx = (int) (mDownX - ev.getX());
            dy = (int) (mDownY - ev.getY());
            judgeOpenClose(dx, dy);
        }
        break;
    }
    return super.onTouchEvent(ev);
}

From source file:com.lh.biliclient.widget.ScrollableLayout.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    float currentX = ev.getX();
    float currentY = ev.getY();
    float deltaY;
    int shiftX;/*from  www.j  a  va 2 s.  c o  m*/
    int shiftY;
    shiftX = (int) Math.abs(currentX - mDownX);
    shiftY = (int) Math.abs(currentY - mDownY);
    //        initVelocityTrackerIfNotExists();
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mDisallowIntercept = false;
        flag1 = true;
        flag2 = true;
        mDownX = currentX;
        mDownY = currentY;
        mLastX = currentX;
        mLastY = currentY;
        mScrollY = getScrollY();
        checkIsClickHead((int) currentY, mHeadHeight, getScrollY());
        checkIsClickHeadExpand((int) currentY, mHeadHeight, getScrollY());
        //                Log.d(tag, "isClickHead:" + isClickHead);
        //                Log.d(tag, "checkIsClickHeadExpand:" + isClickHeadExpand);
        //                Log.d(tag, "ACTION_DOWN__mDownY:" + mDownY);
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        mScroller.forceFinished(true);
        break;
    case MotionEvent.ACTION_MOVE:
        if (mDisallowIntercept) {
            break;
        }
        initVelocityTrackerIfNotExists();
        mVelocityTracker.addMovement(ev);
        deltaY = mLastY - currentY;
        //                Log.d(tag, "mLastY:" + mLastY+"      currentY:" + currentY+"      deltaY:" + deltaY+"      shiftY:" + shiftY
        //                        +"      mTouchSlop:" + mTouchSlop+"      shiftX:" + shiftX);
        //                Log.d(tag, "deltaY:" + deltaY);
        if (flag1) {
            if (shiftX > mTouchSlop && shiftX > shiftY) {
                flag1 = false;
                flag2 = false;
            } else if (shiftY > mTouchSlop && shiftY > shiftX) {
                flag1 = false;
                flag2 = true;
            }
        }

        if (flag2 && shiftY > mTouchSlop && shiftY > shiftX
                && (!isSticked() || mHelper.isTop() || isClickHeadExpand)) {

            if (childViewPager != null) {
                childViewPager.requestDisallowInterceptTouchEvent(true);
            }
            scrollBy(0, (int) (deltaY + 0.5));
        }
        mLastX = currentX;
        mLastY = currentY;
        break;
    case MotionEvent.ACTION_UP:
        if (flag2 && shiftY > shiftX && shiftY > mTouchSlop) {
            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            float yVelocity = -mVelocityTracker.getYVelocity();
            //                    Log.d(tag, "ACTION:" + (ev.getAction() == MotionEvent.ACTION_UP ? "UP" : "CANCEL"));
            if (Math.abs(yVelocity) > mMinimumVelocity) {
                mDirection = yVelocity > 0 ? DIRECTION.UP : DIRECTION.DOWN;
                if (mDirection == DIRECTION.UP && isSticked()) {
                } else {
                    mScroller.fling(0, getScrollY(), 0, (int) yVelocity, 0, 0, -Integer.MAX_VALUE,
                            Integer.MAX_VALUE);
                    mScroller.computeScrollOffset();
                    mLastScrollerY = getScrollY();
                    //                            Log.d(tag, "StartFling1 yVelocity:" + yVelocity + " duration:" + mScroller.getDuration());
                    //                            Log.d(tag, "StartFling2 ScrollY():" + getScrollY() + "->FinalY:" + mScroller.getFinalY() + ",mScroller.curY:" + mScroller.getCurrY());
                    invalidate();
                }
            }
            //                    if ((shiftX > mTouchSlop || shiftY > mTouchSlop)) {
            if (isClickHead || !isSticked()) {
                int action = ev.getAction();
                ev.setAction(MotionEvent.ACTION_CANCEL);
                boolean dd = super.dispatchTouchEvent(ev);
                ev.setAction(action);
                return dd;
            }
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        //                Log.d(tag, "ACTION:" + (ev.getAction() == MotionEvent.ACTION_UP ? "UP" : "CANCEL"));
        if (flag2 && isClickHead && (shiftX > mTouchSlop || shiftY > mTouchSlop)) {
            //                    Log.d(tag, "ACTION_CANCEL isClickHead");
            int action = ev.getAction();
            ev.setAction(MotionEvent.ACTION_CANCEL);
            boolean dd = super.dispatchTouchEvent(ev);
            //                    Log.d(tag, "super.dispatchTouchEvent(ACTION_CANCEL):" + dd);
            ev.setAction(action);
            return dd;
        }
        break;
    default:
        break;
    }
    super.dispatchTouchEvent(ev);
    return true;
}

From source file:com.huaop2p.yqs.widget.scroll.ScrollableLayout.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    float currentX = ev.getX();
    float currentY = ev.getY();
    float deltaY;
    int shiftX;// ww  w. ja v  a 2 s.  c o  m
    int shiftY;
    shiftX = (int) Math.abs(currentX - mDownX);
    shiftY = (int) Math.abs(currentY - mDownY);
    //        initVelocityTrackerIfNotExists();
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mDisallowIntercept = false;
        flag1 = true;
        flag2 = true;
        mDownX = currentX;
        mDownY = currentY;
        mLastX = currentX;
        mLastY = currentY;
        mScrollY = getScrollY();
        checkIsClickHead((int) currentY, mHeadHeight, getScrollY());
        checkIsClickHeadExpand((int) currentY, mHeadHeight, getScrollY());
        //                Log.d(tag, "isClickHead:" + isClickHead);
        //                Log.d(tag, "checkIsClickHeadExpand:" + isClickHeadExpand);
        //                Log.d(tag, "ACTION_DOWN__mDownY:" + mDownY);
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        mScroller.forceFinished(true);
        break;
    case MotionEvent.ACTION_MOVE:
        View view = ((ViewGroup) this.getParent()).getChildAt(0);
        if (view.getTop() > -view.getHeight())
            break;
        if (mDisallowIntercept) {
            break;
        }
        initVelocityTrackerIfNotExists();
        mVelocityTracker.addMovement(ev);
        deltaY = mLastY - currentY;
        //                Log.d(tag, "mLastY:" + mLastY+"      currentY:" + currentY+"      deltaY:" + deltaY+"      shiftY:" + shiftY
        //                        +"      mTouchSlop:" + mTouchSlop+"      shiftX:" + shiftX);
        //                Log.d(tag, "deltaY:" + deltaY);
        if (flag1) {
            if (shiftX > mTouchSlop && shiftX > shiftY) {
                flag1 = false;
                flag2 = false;
            } else if (shiftY > mTouchSlop && shiftY > shiftX) {
                flag1 = false;
                flag2 = true;
            }
        }

        if (flag2 && shiftY > mTouchSlop && shiftY > shiftX
                && (!isSticked() || mHelper.isTop() || isClickHeadExpand)) {

            if (childViewPager != null) {
                childViewPager.requestDisallowInterceptTouchEvent(true);
            }
            scrollBy(0, (int) (deltaY + 0.5));
        }
        mLastX = currentX;
        mLastY = currentY;
        break;
    case MotionEvent.ACTION_UP:
        if (flag2 && shiftY > shiftX && shiftY > mTouchSlop) {
            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            float yVelocity = -mVelocityTracker.getYVelocity();
            //                    Log.d(tag, "ACTION:" + (ev.getAction() == MotionEvent.ACTION_UP ? "UP" : "CANCEL"));
            if (Math.abs(yVelocity) > mMinimumVelocity) {
                mDirection = yVelocity > 0 ? DIRECTION.UP : DIRECTION.DOWN;
                if (mDirection == DIRECTION.UP && isSticked()) {
                } else {
                    mScroller.fling(0, getScrollY(), 0, (int) yVelocity, 0, 0, -Integer.MAX_VALUE,
                            Integer.MAX_VALUE);
                    mScroller.computeScrollOffset();
                    mLastScrollerY = getScrollY();
                    //                            Log.d(tag, "StartFling1 yVelocity:" + yVelocity + " duration:" + mScroller.getDuration());
                    //                            Log.d(tag, "StartFling2 ScrollY():" + getScrollY() + "->FinalY:" + mScroller.getFinalY() + ",mScroller.curY:" + mScroller.getCurrY());
                    invalidate();
                }
            }
            //                    if ((shiftX > mTouchSlop || shiftY > mTouchSlop)) {
            if (isClickHead || !isSticked()) {
                int action = ev.getAction();
                ev.setAction(MotionEvent.ACTION_CANCEL);
                boolean dd = super.dispatchTouchEvent(ev);
                ev.setAction(action);
                return dd;
            }
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        //                Log.d(tag, "ACTION:" + (ev.getAction() == MotionEvent.ACTION_UP ? "UP" : "CANCEL"));
        if (flag2 && isClickHead && (shiftX > mTouchSlop || shiftY > mTouchSlop)) {
            //                    Log.d(tag, "ACTION_CANCEL isClickHead");
            int action = ev.getAction();
            ev.setAction(MotionEvent.ACTION_CANCEL);
            boolean dd = super.dispatchTouchEvent(ev);
            //                    Log.d(tag, "super.dispatchTouchEvent(ACTION_CANCEL):" + dd);
            ev.setAction(action);
            return dd;
        }
        break;
    default:
        break;
    }
    super.dispatchTouchEvent(ev);
    return true;
}

From source file:com.example.liuyang.moneyapp.widgets.ScrollableLayout.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    float currentX = ev.getX();
    float currentY = ev.getY();
    float deltaY;
    int shiftX = (int) Math.abs(currentX - mDownX);
    int shiftY = (int) Math.abs(currentY - mDownY);
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mDisallowIntercept = false;// w w  w  . j a  v a2  s . c  o m
        needCheckUpdown = true;
        updown = true;
        mDownX = currentX;
        mDownY = currentY;
        mLastY = currentY;
        checkIsClickHead((int) currentY, mHeadHeight, getScrollY());
        checkIsClickHeadExpand((int) currentY, mHeadHeight, getScrollY());
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        mScroller.forceFinished(true);
        break;
    case MotionEvent.ACTION_MOVE:
        if (mDisallowIntercept) {
            break;
        }
        initVelocityTrackerIfNotExists();
        mVelocityTracker.addMovement(ev);
        deltaY = mLastY - currentY;
        if (needCheckUpdown) {
            if (shiftX > mTouchSlop && shiftX > shiftY) {
                needCheckUpdown = false;
                updown = false;
            } else if (shiftY > mTouchSlop && shiftY > shiftX) {
                needCheckUpdown = false;
                updown = true;
            }
        }

        if (updown && shiftY > mTouchSlop && shiftY > shiftX
                && (!isSticked() || mHelper.isTop() || isClickHeadExpand)) {

            if (childViewPager != null) {
                childViewPager.requestDisallowInterceptTouchEvent(true);
            }
            scrollBy(0, (int) (deltaY + 0.5));
        }
        mLastY = currentY;
        break;
    case MotionEvent.ACTION_UP:
        if (updown && shiftY > shiftX && shiftY > mTouchSlop) {

            if (mLastY - mDownY > 10) {
            }

            mDirection = (mLastY - mDownY) < 20 ? DIRECTION.UP : DIRECTION.DOWN;
            //                    mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            //                    float yVelocity = -mVelocityTracker.getYVelocity();
            boolean dislowChild = false;
            //                    if (Math.abs(yVelocity) > mMinimumVelocity) {
            //                    if (Math.abs(yVelocity) > 10) {
            //                        mDirection = yVelocity > 0 ? DIRECTION.UP : DIRECTION.DOWN;
            if ((mDirection == DIRECTION.UP && isSticked())
                    || (!isSticked() && getScrollY() == 0 && mDirection == DIRECTION.DOWN)) {
                dislowChild = true;
            } else {
                //                            mScroller.fling(0, getScrollY(), 0, (int) yVelocity, 0, 0, -Integer.MAX_VALUE, Integer.MAX_VALUE);
                //                            mScroller.computeScrollOffset();
                //                            mLastScrollerY = getScrollY();
                //                            invalidate();
                if (mDirection == DIRECTION.DOWN) {
                    mScroller.startScroll(0, getScrollY(), 0, 0, 1500);
                    mScroller.computeScrollOffset();
                    //                                mLastScrollerY = getScrollY();
                    invalidate();
                } else {
                    mScroller.startScroll(0, getScrollY(), 0, maxY, 1500);
                    mScroller.computeScrollOffset();
                    mLastScrollerY = getScrollY();
                    invalidate();
                }
            }
            //                    }
            if (!dislowChild && (isClickHead || !isSticked())) {

                //                        if (mDirection == DIRECTION.DOWN) {
                //                            mScroller.startScroll(0, mLastScrollerY, 0, 0, 1500);
                //                            mScroller.computeScrollOffset();
                //                            mLastScrollerY = getScrollY();
                //                            invalidate();
                //                        } else {
                //                            mScroller.startScroll(0, mLastScrollerY, 0, maxY, 1500);
                //                            mScroller.computeScrollOffset();
                //                            mLastScrollerY = getScrollY();
                //                            invalidate();
                //                        }

                int action = ev.getAction();
                ev.setAction(MotionEvent.ACTION_CANCEL);
                boolean dispathResult = super.dispatchTouchEvent(ev);
                ev.setAction(action);
                return dispathResult;
            }
        }
        break;
    default:
        break;
    }
    super.dispatchTouchEvent(ev);
    return true;
}