Example usage for android.support.v4.view MotionEventCompat ACTION_MASK

List of usage examples for android.support.v4.view MotionEventCompat ACTION_MASK

Introduction

In this page you can find the example usage for android.support.v4.view MotionEventCompat ACTION_MASK.

Prototype

int ACTION_MASK

To view the source code for android.support.v4.view MotionEventCompat ACTION_MASK.

Click Source Link

Document

Synonym for MotionEvent#ACTION_MASK .

Usage

From source file:cn.xiaowen.news.view.LazyViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*//w w  w .  j a v a 2  s.  c  o m
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onMotionEvent will be called and we do the actual
     * scrolling there.
     */

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    // Always take care of the touch gesture being complete.
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        // Release the drag.
        if (DEBUG)
            Log.v(TAG, "Intercept done!");
        mIsBeingDragged = false;
        mIsUnableToDrag = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        return false;
    }

    // Nothing more to do here if we have decided whether or not we
    // are dragging.
    if (action != MotionEvent.ACTION_DOWN) {
        if (mIsBeingDragged) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning true!");
            return true;
        }
        if (mIsUnableToDrag) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning false!");
            return false;
        }
    }

    switch (action) {
    case MotionEvent.ACTION_MOVE: {
        /*
         * mIsBeingDragged == false, otherwise the shortcut would have
         * caught it. Check whether the user has moved far enough from his
         * original down touch.
         */

        /*
         * Locally do absolute value. mLastMotionY is set to the y value of
         * the down event.
         */
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on
            // content.
            break;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float dx = x - mLastMotionX;
        final float xDiff = Math.abs(dx);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float yDiff = Math.abs(y - mLastMotionY);
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        if (canScroll(this, false, (int) dx, (int) x, (int) y)) {
            // Nested view has scrollable area under this point. Let it be
            // handled there.
            mInitialMotionX = mLastMotionX = x;
            mLastMotionY = y;
            return false;
        }
        if (xDiff > mTouchSlop && xDiff > yDiff) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionX = x;
            setScrollingCacheEnabled(true);
        } else {
            if (yDiff > mTouchSlop) {
                // The finger has moved enough in the vertical
                // direction to be counted as a drag... abort
                // any attempt to drag horizontally, to work correctly
                // with children that have scrolling containers.
                if (DEBUG)
                    Log.v(TAG, "Starting unable to drag!");
                mIsUnableToDrag = true;
            }
        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        /*
         * Remember location of down touch. ACTION_DOWN always refers to
         * pointer index 0.
         */
        mLastMotionX = mInitialMotionX = ev.getX();
        mLastMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);

        if (mScrollState == SCROLL_STATE_SETTLING) {
            // Let the user 'catch' the pager as it animates.
            mIsBeingDragged = true;
            mIsUnableToDrag = false;
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll();
            mIsBeingDragged = false;
            mIsUnableToDrag = false;
        }

        if (DEBUG)
            Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                    + "mIsUnableToDrag=" + mIsUnableToDrag);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

    if (!mIsBeingDragged) {
        // Track the velocity as long as we aren't dragging.
        // Once we start a real drag we will track in onTouchEvent.
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(ev);
    }

    /*
     * The only time we want to intercept motion events is if we are in the
     * drag mode.
     */
    return mIsBeingDragged;
}

From source file:com.artifex.mupdflib.TwoWayView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }//from www  .  j  a  v  a 2s.  co m

    if (!mIsAttached || mAdapter == null) {
        return false;
    }

    boolean needsInvalidate = false;

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        if (mDataChanged) {
            break;
        }

        mVelocityTracker.clear();
        mScroller.abortAnimation();
        if (mPositionScroller != null) {
            mPositionScroller.stop();
        }

        final float x = ev.getX();
        final float y = ev.getY();

        mLastTouchPos = (mIsVertical ? y : x);

        int motionPosition = pointToPosition((int) x, (int) y);

        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderPos = 0;

        if (mDataChanged) {
            break;
        }

        if (mTouchMode == TOUCH_MODE_FLINGING) {
            mTouchMode = TOUCH_MODE_DRAGGING;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            motionPosition = findMotionRowOrColumn((int) mLastTouchPos);
        } else if (mMotionPosition >= 0 && mAdapter.isEnabled(mMotionPosition)) {
            mTouchMode = TOUCH_MODE_DOWN;
            triggerCheckForTap();
        }

        mMotionPosition = motionPosition;

        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(LOGTAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did TwoWayView receive an inconsistent " + "event stream?");
            return false;
        }

        final float pos;
        if (mIsVertical) {
            pos = MotionEventCompat.getY(ev, index);
        } else {
            pos = MotionEventCompat.getX(ev, index);
        }

        if (mDataChanged) {
            // Re-sync everything if data has been changed
            // since the scroll operation can query the adapter.
            layoutChildren();
        }

        final float diff = pos - mLastTouchPos + mTouchRemainderPos;
        final int delta = (int) diff;
        mTouchRemainderPos = diff - delta;

        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            // Check if we have moved far enough that it looks more like a
            // scroll than a tap
            maybeStartScrolling(delta);
            break;

        case TOUCH_MODE_DRAGGING:
        case TOUCH_MODE_OVERSCROLL:
            mLastTouchPos = pos;
            maybeScroll(delta);
            break;
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
        cancelCheckForTap();
        mTouchMode = TOUCH_MODE_REST;
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);

        setPressed(false);
        View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
        if (motionView != null) {
            motionView.setPressed(false);
        }

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate = mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;

    case MotionEvent.ACTION_UP: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING: {
            final int motionPosition = mMotionPosition;
            final View child = getChildAt(motionPosition - mFirstPosition);

            final float x = ev.getX();
            final float y = ev.getY();

            final boolean inList;
            if (mIsVertical) {
                inList = x > getPaddingLeft() && x < getWidth() - getPaddingRight();
            } else {
                inList = y > getPaddingTop() && y < getHeight() - getPaddingBottom();
            }

            if (child != null && !child.hasFocusable() && inList) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformClick == null) {
                    mPerformClick = new PerformClick();
                }

                final PerformClick performClick = mPerformClick;
                performClick.mClickMotionPosition = motionPosition;
                performClick.rememberWindowAttachCount();

                mResurrectToPosition = motionPosition;

                if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                    if (mTouchMode == TOUCH_MODE_DOWN) {
                        cancelCheckForTap();
                    } else {
                        cancelCheckForLongPress();
                    }

                    mLayoutMode = LAYOUT_NORMAL;

                    if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                        mTouchMode = TOUCH_MODE_TAP;

                        setPressed(true);
                        positionSelector(mMotionPosition, child);
                        child.setPressed(true);

                        if (mSelector != null) {
                            Drawable d = mSelector.getCurrent();
                            if (d != null && d instanceof TransitionDrawable) {
                                ((TransitionDrawable) d).resetTransition();
                            }
                        }

                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }

                        mTouchModeReset = new Runnable() {
                            @Override
                            public void run() {
                                mTouchMode = TOUCH_MODE_REST;

                                setPressed(false);
                                child.setPressed(false);

                                if (!mDataChanged) {
                                    performClick.run();
                                }

                                mTouchModeReset = null;
                            }
                        };

                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        updateSelectorState();
                    }
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

            mTouchMode = TOUCH_MODE_REST;

            finishSmoothScrolling();
            updateSelectorState();

            break;
        }

        case TOUCH_MODE_DRAGGING:
            if (contentFits()) {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                break;
            }

            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

            final float velocity;
            if (mIsVertical) {
                velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            } else {
                velocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId);
            }

            if (Math.abs(velocity) >= mFlingVelocity) {
                mTouchMode = TOUCH_MODE_FLINGING;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);

                mScroller.fling(0, 0, (int) (mIsVertical ? 0 : velocity), (int) (mIsVertical ? velocity : 0),
                        (mIsVertical ? 0 : Integer.MIN_VALUE), (mIsVertical ? 0 : Integer.MAX_VALUE),
                        (mIsVertical ? Integer.MIN_VALUE : 0), (mIsVertical ? Integer.MAX_VALUE : 0));

                mLastTouchPos = 0;
                needsInvalidate = true;
            } else {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            }

            break;

        case TOUCH_MODE_OVERSCROLL:
            mTouchMode = TOUCH_MODE_REST;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            break;
        }

        cancelCheckForTap();
        cancelCheckForLongPress();
        setPressed(false);

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate |= mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;
    }
    }

    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }

    return true;
}

From source file:org.digitalcampus.oppia.utils.TwoWayView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }/*from   w w w . ja  va 2  s  . co  m*/

    if (!mIsAttached || mAdapter == null) {
        return false;
    }

    boolean needsInvalidate = false;

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        if (mDataChanged) {
            break;
        }

        mVelocityTracker.clear();
        mScroller.abortAnimation();
        if (mPositionScroller != null) {
            mPositionScroller.stop();
        }

        final float x = ev.getX();
        final float y = ev.getY();

        mLastTouchPos = (mIsVertical ? y : x);

        int motionPosition = pointToPosition((int) x, (int) y);

        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderPos = 0;

        if (mDataChanged) {
            break;
        }

        if (mTouchMode == TOUCH_MODE_FLINGING) {
            mTouchMode = TOUCH_MODE_DRAGGING;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            motionPosition = findMotionRowOrColumn((int) mLastTouchPos);
        } else if (mMotionPosition >= 0 && mAdapter.isEnabled(mMotionPosition)) {
            mTouchMode = TOUCH_MODE_DOWN;
            triggerCheckForTap();
        }

        mMotionPosition = motionPosition;

        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(LOGTAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did TwoWayView receive an inconsistent " + "event stream?");
            return false;
        }

        final float pos;
        if (mIsVertical) {
            pos = MotionEventCompat.getY(ev, index);
        } else {
            pos = MotionEventCompat.getX(ev, index);
        }

        if (mDataChanged) {
            // Re-sync everything if data has been changed
            // since the scroll operation can query the adapter.
            layoutChildren();
        }

        final float diff = pos - mLastTouchPos + mTouchRemainderPos;
        final int delta = (int) diff;
        mTouchRemainderPos = diff - delta;

        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            // Check if we have moved far enough that it looks more like a
            // scroll than a tap
            maybeStartScrolling(delta);
            break;

        case TOUCH_MODE_DRAGGING:
        case TOUCH_MODE_OVERSCROLL:
            mLastTouchPos = pos;
            maybeScroll(delta);
            break;
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
        cancelCheckForTap();
        mTouchMode = TOUCH_MODE_REST;
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);

        setPressed(false);
        View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
        if (motionView != null) {
            motionView.setPressed(false);
        }

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate = mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;

    case MotionEvent.ACTION_UP: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING: {
            final int motionPosition = mMotionPosition;
            final View child = getChildAt(motionPosition - mFirstPosition);

            final float x = ev.getX();
            final float y = ev.getY();

            final boolean inList;
            if (mIsVertical) {
                inList = x > getPaddingLeft() && x < getWidth() - getPaddingRight();
            } else {
                inList = y > getPaddingTop() && y < getHeight() - getPaddingBottom();
            }

            if (child != null && !child.hasFocusable() && inList) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformClick == null) {
                    mPerformClick = new PerformClick();
                }

                final PerformClick performClick = mPerformClick;
                performClick.mClickMotionPosition = motionPosition;
                performClick.rememberWindowAttachCount();

                mResurrectToPosition = motionPosition;

                if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                    if (mTouchMode == TOUCH_MODE_DOWN) {
                        cancelCheckForTap();
                    } else {
                        cancelCheckForLongPress();
                    }

                    mLayoutMode = LAYOUT_NORMAL;

                    if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                        mTouchMode = TOUCH_MODE_TAP;

                        setPressed(true);
                        positionSelector(mMotionPosition, child);
                        child.setPressed(true);

                        if (mSelector != null) {
                            Drawable d = mSelector.getCurrent();
                            if (d != null && d instanceof TransitionDrawable) {
                                ((TransitionDrawable) d).resetTransition();
                            }
                        }

                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }

                        mTouchModeReset = new Runnable() {
                            public void run() {
                                mTouchMode = TOUCH_MODE_REST;

                                setPressed(false);
                                child.setPressed(false);

                                if (!mDataChanged) {
                                    performClick.run();
                                }

                                mTouchModeReset = null;
                            }
                        };

                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        updateSelectorState();
                    }
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

            mTouchMode = TOUCH_MODE_REST;

            finishSmoothScrolling();
            updateSelectorState();

            break;
        }

        case TOUCH_MODE_DRAGGING:
            if (contentFits()) {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                break;
            }

            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

            final float velocity;
            if (mIsVertical) {
                velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
            } else {
                velocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId);
            }

            if (Math.abs(velocity) >= mFlingVelocity) {
                mTouchMode = TOUCH_MODE_FLINGING;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);

                mScroller.fling(0, 0, (int) (mIsVertical ? 0 : velocity), (int) (mIsVertical ? velocity : 0),
                        (mIsVertical ? 0 : Integer.MIN_VALUE), (mIsVertical ? 0 : Integer.MAX_VALUE),
                        (mIsVertical ? Integer.MIN_VALUE : 0), (mIsVertical ? Integer.MAX_VALUE : 0));

                mLastTouchPos = 0;
                needsInvalidate = true;
            } else {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            }

            break;

        case TOUCH_MODE_OVERSCROLL:
            mTouchMode = TOUCH_MODE_REST;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            break;
        }

        cancelCheckForTap();
        cancelCheckForLongPress();
        setPressed(false);

        if (mStartEdge != null && mEndEdge != null) {
            needsInvalidate |= mStartEdge.onRelease() | mEndEdge.onRelease();
        }

        recycleVelocityTracker();

        break;
    }
    }

    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }

    return true;
}

From source file:javalibrary.android.ui.VerticalViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*//from ww w  . j ava  2s .c  o m
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onMotionEvent will be called and we do the actual
     * scrolling there.
     */

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    // Always take care of the touch gesture being complete.
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        // Release the drag.
        if (DEBUG)
            Log.v(TAG, "Intercept done!");
        mIsBeingDragged = false;
        mIsUnableToDrag = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        return false;
    }

    // Nothing more to do here if we have decided whether or not we
    // are dragging.
    if (action != MotionEvent.ACTION_DOWN) {
        if (mIsBeingDragged) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning true!");
            return true;
        }
        if (mIsUnableToDrag) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning false!");
            return false;
        }
    }

    switch (action) {
    case MotionEvent.ACTION_MOVE: {
        /*
         * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
         * whether the user has moved far enough from his original down touch.
         */

        /*
        * Locally do absolute value. mLastMotionY is set to the y value
        * of the down event.
        */
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float dx = x - mLastMotionX;
        final float xDiff = Math.abs(dx);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float dy = y - mLastMotionY;
        final float yDiff = Math.abs(y - mLastMotionY);
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        //                if (canScroll(this, false, (int) dx, (int) x, (int) y)) {
        if (canScrolls(this, false, (int) dy, (int) x, (int) y)) {
            // Nested view has scrollable area under this point. Let it be handled there.
            mInitialMotionX = mLastMotionX = x;
            mInitialMotionY = mLastMotionY = y;
            return false;
        }
        /*if (xDiff > mTouchSlop && xDiff > yDiff) {
            if (DEBUG) Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionX = x;
            setScrollingCacheEnabled(true);
        } else {
            if (yDiff > mTouchSlop) {
                // The finger has moved enough in the vertical
                // direction to be counted as a drag...  abort
                // any attempt to drag horizontally, to work correctly
                // with children that have scrolling containers.
                if (DEBUG) Log.v(TAG, "Starting unable to drag!");
                mIsUnableToDrag = true;
            }
        }*/

        if (yDiff > mTouchSlop && yDiff > xDiff) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionX = x;
            mLastMotionY = y;
            setScrollingCacheEnabled(true);
        } else {
            if (xDiff > mTouchSlop) {
                // The finger has moved enough in the vertical
                // direction to be counted as a drag...  abort
                // any attempt to drag horizontally, to work correctly
                // with children that have scrolling containers.
                if (DEBUG)
                    Log.v(TAG, "Starting unable to drag!");
                mIsUnableToDrag = true;
            }
        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        /*
         * Remember location of down touch.
         * ACTION_DOWN always refers to pointer index 0.
         */
        mLastMotionX = mInitialMotionX = ev.getX();
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);

        if (mScrollState == SCROLL_STATE_SETTLING) {
            // Let the user 'catch' the pager as it animates.
            mIsBeingDragged = true;
            mIsUnableToDrag = false;
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll();
            mIsBeingDragged = false;
            mIsUnableToDrag = false;
        }

        if (DEBUG)
            Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                    + "mIsUnableToDrag=" + mIsUnableToDrag);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

    if (!mIsBeingDragged) {
        // Track the velocity as long as we aren't dragging.
        // Once we start a real drag we will track in onTouchEvent.
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(ev);
    }

    /*
     * The only time we want to intercept motion events is if we are in the
     * drag mode.
     */
    return mIsBeingDragged;
}

From source file:com.baidu.zhuanche.view.LazyViewPager.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mFakeDragging) {
        // A fake drag is in progress already, ignore this real one
        // but still eat the touch events.
        // (It is likely that the user is multi-touching the screen.)
        return true;
    }//from w w  w . ja  v a2  s .c om

    if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // Don't handle edge touches immediately -- they may actually belong to one of our
        // descendants.
        return false;
    }

    if (mAdapter == null || mAdapter.getCount() == 0) {
        // Nothing to present or scroll; nothing to touch.
        return false;
    }

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();
    boolean needsInvalidate = false;

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        completeScroll();

        // Remember where the motion event started
        mLastMotionX = mInitialMotionX = ev.getX();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float xDiff = Math.abs(x - mLastMotionX);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float yDiff = Math.abs(y - mLastMotionY);
            if (DEBUG)
                Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
            if (xDiff > mTouchSlop && xDiff > yDiff) {
                if (DEBUG)
                    Log.v(TAG, "Starting drag!");
                mIsBeingDragged = true;
                mLastMotionX = x;
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);
            }
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float deltaX = mLastMotionX - x;
            mLastMotionX = x;
            float oldScrollX = getScrollX();
            float scrollX = oldScrollX + deltaX;
            final int width = getWidth();
            final int widthWithMargin = width + mPageMargin;

            final int lastItemIndex = mAdapter.getCount() - 1;
            final float leftBound = Math.max(0, (mCurItem - 1) * widthWithMargin);
            final float rightBound = Math.min(mCurItem + 1, lastItemIndex) * widthWithMargin;
            if (scrollX < leftBound) {
                if (leftBound == 0) {
                    float over = -scrollX;
                    needsInvalidate = mLeftEdge.onPull(over / width);
                }
                scrollX = leftBound;
            } else if (scrollX > rightBound) {
                if (rightBound == lastItemIndex * widthWithMargin) {
                    float over = scrollX - rightBound;
                    needsInvalidate = mRightEdge.onPull(over / width);
                }
                scrollX = rightBound;
            }
            // Don't lose the rounded component
            mLastMotionX += scrollX - (int) scrollX;
            scrollTo((int) scrollX, getScrollY());
            pageScrolled((int) scrollX);
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
            mPopulatePending = true;
            final int widthWithMargin = getWidth() + mPageMargin;
            final int scrollX = getScrollX();
            final int currentPage = scrollX / widthWithMargin;
            final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final int totalDelta = (int) (x - mInitialMotionX);
            int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);

            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            setCurrentItemInternal(mCurItem, true, true);
            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        final float x = MotionEventCompat.getX(ev, index);
        mLastMotionX = x;
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    if (needsInvalidate) {
        invalidate();
    }
    return true;
}

From source file:com.mcxiaoke.minicat.ui.widget.StaggeredGridView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);/*from  w  w  w.j a  v  a2  s  .  c  o  m*/
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    int motionPosition = pointToPosition((int) ev.getX(), (int) ev.getY());

    switch (action) {
    case MotionEvent.ACTION_DOWN:

        mVelocityTracker.clear();
        mScroller.abortAnimation();
        mLastTouchY = ev.getY();
        mLastTouchX = ev.getX();
        motionPosition = pointToPosition((int) mLastTouchX, (int) mLastTouchY);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderY = 0;

        if (mTouchMode != TOUCH_MODE_FLINGING && !mDataChanged && motionPosition >= 0
                && getAdapter().isEnabled(motionPosition)) {
            mTouchMode = TOUCH_MODE_DOWN;

            if (mPendingCheckForTap == null) {
                mPendingCheckForTap = new CheckForTap();
            }

            postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
        }

        mMotionPosition = motionPosition;
        invalidate();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(TAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did StaggeredGridView receive an inconsistent " + "event stream?");
            return false;
        }
        final float y = MotionEventCompat.getY(ev, index);
        final float dy = y - mLastTouchY + mTouchRemainderY;
        final int deltaY = (int) dy;
        mTouchRemainderY = dy - deltaY;

        if (Math.abs(dy) > mTouchSlop) {
            mTouchMode = TOUCH_MODE_DRAGGING;
        }

        if (mTouchMode == TOUCH_MODE_DRAGGING) {
            mLastTouchY = y;

            if (!trackMotionScroll(deltaY, true)) {
                // Break fling velocity if we impacted an edge.
                mVelocityTracker.clear();
            }
        }
    }
        break;

    case MotionEvent.ACTION_CANCEL:
        mTouchMode = TOUCH_MODE_IDLE;
        updateSelectorState();
        setPressed(false);
        View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
        if (motionView != null) {
            motionView.setPressed(false);
        }
        final Handler handler = getHandler();
        if (handler != null) {
            handler.removeCallbacks(mPendingCheckForLongPress);
        }

        if (mTopEdge != null) {
            mTopEdge.onRelease();
            mBottomEdge.onRelease();
        }
        break;

    case MotionEvent.ACTION_UP: {
        mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        final float velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
        final int prevTouchMode = mTouchMode;

        if (Math.abs(velocity) > mFlingVelocity) { // TODO
            mTouchMode = TOUCH_MODE_FLINGING;
            mScroller.fling(0, 0, 0, (int) velocity, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
            mLastTouchY = 0;
            invalidate();
        } else {
            mTouchMode = TOUCH_MODE_IDLE;
        }

        if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
            // TODO : handle
            mTouchMode = TOUCH_MODE_TAP;
        } else {
            mTouchMode = TOUCH_MODE_REST;
            updateSelectorState();
        }

        switch (prevTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            final View child = getChildAt(motionPosition - mFirstPosition);
            final float x = ev.getX();
            final boolean inList = x > getPaddingLeft() && x < getWidth() - getPaddingRight();
            if (child != null && !child.hasFocusable() && inList) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformClick == null) {
                    invalidate();
                    mPerformClick = new PerformClick();
                }

                final PerformClick performClick = mPerformClick;
                performClick.mClickMotionPosition = motionPosition;
                performClick.rememberWindowAttachCount();

                if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                    final Handler handlerTouch = getHandler();
                    if (handlerTouch != null) {
                        handlerTouch.removeCallbacks(mTouchMode == TOUCH_MODE_DOWN ? mPendingCheckForTap
                                : mPendingCheckForLongPress);
                    }

                    if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                        mTouchMode = TOUCH_MODE_TAP;

                        layoutChildren(mDataChanged);
                        child.setPressed(true);
                        positionSelector(mMotionPosition, child);
                        setPressed(true);
                        if (mSelector != null) {
                            Drawable d = mSelector.getCurrent();
                            if (d != null && d instanceof TransitionDrawable) {
                                ((TransitionDrawable) d).resetTransition();
                            }
                        }
                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }
                        mTouchModeReset = new Runnable() {
                            @Override
                            public void run() {
                                mTouchMode = TOUCH_MODE_REST;
                                child.setPressed(false);
                                setPressed(false);
                                if (!mDataChanged) {
                                    performClick.run();
                                }
                            }
                        };
                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());

                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        updateSelectorState();
                    }
                    return true;
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

            mTouchMode = TOUCH_MODE_REST;
            updateSelectorState();
        }

        updateSelectorState();
    }
        break;
    }
    return true;
}

From source file:com.chenglong.muscle.ui.LazyViewPager.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mFakeDragging) {
        // A fake drag is in progress already, ignore this real one
        // but still eat the touch events.
        // (It is likely that the user is multi-touching the screen.)
        return true;
    }// w  w w . j a  va 2  s.  c om
    if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // Don't handle edge touches immediately -- they may actually belong
        // to one of our
        // descendants.
        return false;
    }
    if (mAdapter == null || mAdapter.getCount() == 0) {
        // Nothing to present or scroll; nothing to touch.
        return false;
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);
    final int action = ev.getAction();
    boolean needsInvalidate = false;
    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        completeScroll();
        // Remember where the motion event started
        mLastMotionX = mInitialMotionX = ev.getX();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float xDiff = Math.abs(x - mLastMotionX);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float yDiff = Math.abs(y - mLastMotionY);
            if (DEBUG)
                Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
            if (xDiff > mTouchSlop && xDiff > yDiff) {
                if (DEBUG)
                    Log.v(TAG, "Starting drag!");
                mIsBeingDragged = true;
                mLastMotionX = x;
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);
            }
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float deltaX = mLastMotionX - x;
            mLastMotionX = x;
            float oldScrollX = getScrollX();
            float scrollX = oldScrollX + deltaX;
            final int width = getWidth();
            final int widthWithMargin = width + mPageMargin;
            final int lastItemIndex = mAdapter.getCount() - 1;
            final float leftBound = Math.max(0, (mCurItem - 1) * widthWithMargin);
            final float rightBound = Math.min(mCurItem + 1, lastItemIndex) * widthWithMargin;
            if (scrollX < leftBound) {
                if (leftBound == 0) {
                    float over = -scrollX;
                    needsInvalidate = mLeftEdge.onPull(over / width);
                }
                scrollX = leftBound;
            } else if (scrollX > rightBound) {
                if (rightBound == lastItemIndex * widthWithMargin) {
                    float over = scrollX - rightBound;
                    needsInvalidate = mRightEdge.onPull(over / width);
                }
                scrollX = rightBound;
            }
            // Don't lose the rounded component
            mLastMotionX += scrollX - (int) scrollX;
            scrollTo((int) scrollX, getScrollY());
            pageScrolled((int) scrollX);
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
            mPopulatePending = true;
            final int widthWithMargin = getWidth() + mPageMargin;
            final int scrollX = getScrollX();
            final int currentPage = scrollX / widthWithMargin;
            final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final int totalDelta = (int) (x - mInitialMotionX);
            int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);
            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            setCurrentItemInternal(mCurItem, true, true);
            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        final float x = MotionEventCompat.getX(ev, index);
        mLastMotionX = x;
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    if (needsInvalidate) {
        invalidate();
    }
    return true;
}

From source file:com.tylz.jiaoyanglogistics.view.LazyViewPager.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mFakeDragging) {
        // A fake drag is in progress already, ignore this real one
        // but still eat the touch events.
        // (It is likely that the user is multi-touching the screen.)
        return true;
    }/*  w  ww .ja v  a  2s .  c  o m*/

    if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // Don't handle edge touches immediately -- they may actually belong to one of our
        // descendants.
        return false;
    }

    if (mAdapter == null || mAdapter.getCount() == 0) {
        // Nothing to present or scroll; nothing to touch.
        return false;
    }

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();
    boolean needsInvalidate = false;

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        completeScroll();

        // Remember where the motion event started
        mLastMotionX = mInitialMotionX = ev.getX();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float xDiff = Math.abs(x - mLastMotionX);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float yDiff = Math.abs(y - mLastMotionY);
            if (DEBUG) {
                Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
            }
            if (xDiff > mTouchSlop && xDiff > yDiff) {
                if (DEBUG) {
                    Log.v(TAG, "Starting drag!");
                }
                mIsBeingDragged = true;
                mLastMotionX = x;
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);
            }
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float deltaX = mLastMotionX - x;
            mLastMotionX = x;
            float oldScrollX = getScrollX();
            float scrollX = oldScrollX + deltaX;
            final int width = getWidth();
            final int widthWithMargin = width + mPageMargin;

            final int lastItemIndex = mAdapter.getCount() - 1;
            final float leftBound = Math.max(0, (mCurItem - 1) * widthWithMargin);
            final float rightBound = Math.min(mCurItem + 1, lastItemIndex) * widthWithMargin;
            if (scrollX < leftBound) {
                if (leftBound == 0) {
                    float over = -scrollX;
                    needsInvalidate = mLeftEdge.onPull(over / width);
                }
                scrollX = leftBound;
            } else if (scrollX > rightBound) {
                if (rightBound == lastItemIndex * widthWithMargin) {
                    float over = scrollX - rightBound;
                    needsInvalidate = mRightEdge.onPull(over / width);
                }
                scrollX = rightBound;
            }
            // Don't lose the rounded component
            mLastMotionX += scrollX - (int) scrollX;
            scrollTo((int) scrollX, getScrollY());
            pageScrolled((int) scrollX);
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
            mPopulatePending = true;
            final int widthWithMargin = getWidth() + mPageMargin;
            final int scrollX = getScrollX();
            final int currentPage = scrollX / widthWithMargin;
            final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final int totalDelta = (int) (x - mInitialMotionX);
            int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);

            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            setCurrentItemInternal(mCurItem, true, true);
            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        final float x = MotionEventCompat.getX(ev, index);
        mLastMotionX = x;
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    if (needsInvalidate) {
        invalidate();
    }
    return true;
}

From source file:administrator.example.com.myscrollview.VerticalViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*//w  w  w.  java2  s  .  co  m
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onMotionEvent will be called and we do the actual
     * scrolling there.
     */

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    // Always take care of the touch gesture being complete.
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        // Release the drag.
        if (DEBUG)
            Log.v(TAG, "Intercept done!");
        mIsBeingDragged = false;
        mIsUnableToDrag = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        } /* end of if */
        return false;
    } /* end of if */

    // Nothing more to do here if we have decided whether or not we
    // are dragging.
    if (action != MotionEvent.ACTION_DOWN) {
        if (mIsBeingDragged) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning true!");
            return true;
        } /* end of if */
        if (mIsUnableToDrag) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning false!");
            return false;
        } /* end of if */
    } /* end of if */

    switch (action) {
    case MotionEvent.ACTION_MOVE: {
        /*
         * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
         * whether the user has moved far enough from his original down touch.
         */

        /*
        * Locally do absolute value. mLastMotionX is set to the x value
        * of the down event.
        */
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        } /* end of if */

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float xDiff = Math.abs(x - mLastMotionX);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float dy = y - mLastMotionY;
        final float yDiff = Math.abs(dy);

        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        if (canScroll(this, false, (int) dy, (int) x, (int) y)) {
            // Nested view has scrollable area under this point. Let it be handled there.
            mInitialMotionY = mLastMotionY = y;
            mLastMotionX = x;
            return false;
        } /* end of if */
        if (yDiff > mTouchSlop && yDiff > xDiff) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionY = y;
            setScrollingCacheEnabled(true);
        } else {
            if (xDiff > mTouchSlop) {
                // The finger has moved enough in the horizontal
                // direction to be counted as a drag...  abort
                // any attempt to drag vertically, to work correctly
                // with children that have scrolling containers.
                if (DEBUG)
                    Log.v(TAG, "Starting unable to drag!");
                mIsUnableToDrag = true;
            } /* end of if */
        } /* end of if */
        break;
    } /* end of case */

    case MotionEvent.ACTION_DOWN: {
        /*
         * Remember location of down touch.
         * ACTION_DOWN always refers to pointer index 0.
         */
        mLastMotionX = ev.getX();
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);

        if (mScrollState == SCROLL_STATE_SETTLING) {
            // Let the user 'catch' the pager as it animates.
            mIsBeingDragged = true;
            mIsUnableToDrag = false;
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll();
            mIsBeingDragged = false;
            mIsUnableToDrag = false;
        } /* end of if */

        if (DEBUG)
            Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                    + "mIsUnableToDrag=" + mIsUnableToDrag);
        break;
    } /* end of case */

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    } /* end of switch */

    if (!mIsBeingDragged) {
        // Track the velocity as long as we aren't dragging.
        // Once we start a real drag we will track in onTouchEvent.
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        } /* end of if */
        mVelocityTracker.addMovement(ev);
    } /* end of if */

    /*
     * The only time we want to intercept motion events is if we are in the
     * drag mode.
     */
    return mIsBeingDragged;
}

From source file:refresh.renyi.io.supperrefreshview.customview.VerticalViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*//www  . j a  va2  s. c  o  m
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onMotionEvent will be called and we do the actual
     * scrolling there.
     */
    if (DEBUG)
        System.out.println("ViewPager Intercept");

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    // Always take care of the touch gesture being complete.
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        // Release the drag.
        if (DEBUG)
            Log.v(TAG, "Intercept done!");
        mIsBeingDragged = false;
        mIsUnableToDrag = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        return false;
    }

    // Nothing more to do here if we have decided whether or not we
    // are dragging.
    if (action != MotionEvent.ACTION_DOWN) {
        if (mIsBeingDragged) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning true!");
            return true;
        }
        if (mIsUnableToDrag) {
            if (DEBUG)
                Log.v(TAG, "Intercept returning false!");
            return false;
        }
    }

    switch (action) {
    case MotionEvent.ACTION_MOVE: {
        /*
         * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
         * whether the user has moved far enough from his original down touch.
         */

        /*
        * Locally do absolute value. mLastMotionY is set to the y value
        * of the down event.
        */
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float dx = x - mLastMotionX;
        final float xDiff = Math.abs(dx);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float dy = y - mLastMotionY;
        final float yDiff = Math.abs(y - mLastMotionY);
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        //                if (canScroll(this, false, (int) dx, (int) x, (int) y)) {
        if (canScrolls(this, false, (int) dy, (int) x, (int) y)) {
            // Nested view has scrollable area under this point. Let it be handled there.
            mInitialMotionX = mLastMotionX = x;
            mInitialMotionY = mLastMotionY = y;
            return false;
        }
        /*if (xDiff > mTouchSlop && xDiff > yDiff) {
            if (DEBUG) Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionX = x;
            setScrollingCacheEnabled(true);
        } else {
            if (yDiff > mTouchSlop) {
                // The finger has moved enough in the vertical
                // direction to be counted as a drag...  abort
                // any attempt to drag horizontally, to work correctly
                // with children that have scrolling containers.
                if (DEBUG) Log.v(TAG, "Starting unable to drag!");
                mIsUnableToDrag = true;
            }
        }*/

        if (yDiff > mTouchSlop && yDiff > xDiff) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionX = x;
            mLastMotionY = y;
            setScrollingCacheEnabled(true);
        } else {
            if (xDiff > mTouchSlop) {
                // The finger has moved enough in the vertical
                // direction to be counted as a drag...  abort
                // any attempt to drag horizontally, to work correctly
                // with children that have scrolling containers.
                if (DEBUG)
                    Log.v(TAG, "Starting unable to drag!");
                mIsUnableToDrag = true;
            }
        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        /*
         * Remember location of down touch.
         * ACTION_DOWN always refers to pointer index 0.
         */
        mLastMotionX = mInitialMotionX = ev.getX();
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);

        if (mScrollState == SCROLL_STATE_SETTLING) {
            // Let the user 'catch' the pager as it animates.
            mIsBeingDragged = true;
            mIsUnableToDrag = false;
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll();
            mIsBeingDragged = false;
            mIsUnableToDrag = false;
        }

        if (DEBUG)
            Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                    + "mIsUnableToDrag=" + mIsUnableToDrag);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

    if (!mIsBeingDragged) {
        // Track the velocity as long as we aren't dragging.
        // Once we start a real drag we will track in onTouchEvent.
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(ev);
    }

    /*
     * The only time we want to intercept motion events is if we are in the
     * drag mode.
     */
    //        super.onInterceptTouchEvent(ev);
    return mIsBeingDragged;
}