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

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

Introduction

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

Prototype

public static int getActionIndex(MotionEvent event) 

Source Link

Document

Call MotionEvent#getAction , returning only the pointer index portion

Usage

From source file:com.aosijia.dragonbutler.ui.widget.UnderlinePageIndicator.java

@SuppressLint("ClickableViewAccessibility")
public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }//  ww w . jav  a2 s  .co  m
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.google.blockly.android.ui.VirtualWorkspaceView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    mScaleGestureDetector.onTouchEvent(event);
    if (mScaleGestureDetector.isInProgress()) {
        // If the scale gesture detector is handling a scale-and-pan gesture, then exit here
        // since otherwise we would also be generating dragging events below.
        return true;
    }// w w  w . ja va 2  s. com

    final int action = MotionEventCompat.getActionMasked(event);

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        clearFocus();
        mImeManager.hideSoftInputFromWindow(getWindowToken(), 0);

        if (mScrollable) {
            final int pointerIdx = MotionEventCompat.getActionIndex(event);
            mPanningPointerId = MotionEventCompat.getPointerId(event, pointerIdx);
            mPanningStart.set((int) MotionEventCompat.getX(event, pointerIdx),
                    (int) MotionEventCompat.getY(event, pointerIdx));
            mOriginalScrollX = getScrollX();
            mOriginalScrollY = getScrollY();
        }
        return true;
    }
    case MotionEvent.ACTION_MOVE: {
        if (mPanningPointerId != MotionEvent.INVALID_POINTER_ID) {
            final int pointerIdx = MotionEventCompat.findPointerIndex(event, mPanningPointerId);
            if (pointerIdx == -1) {
                // TODO: (#319) remove when we clean up multi-touch handling.
                Log.w(TAG, "Got an invalid pointer idx for the panning pointer.");
                return false;
            }
            scrollTo(mOriginalScrollX + mPanningStart.x - (int) MotionEventCompat.getX(event, pointerIdx),
                    mOriginalScrollY + mPanningStart.y - (int) MotionEventCompat.getY(event, pointerIdx));
            return true;
        } else {
            return false;
        }
    }
    case MotionEvent.ACTION_POINTER_UP: {
        // Some pointer went up - check whether it was the one used for panning.
        final int pointerIdx = MotionEventCompat.getActionIndex(event);
        final int pointerId = MotionEventCompat.getPointerId(event, pointerIdx);
        if (pointerId != mPanningPointerId) {
            return false;
        }
        // Pointer that went up was used for panning - treat like ACTION_UP.
        // FALLTHROUGH INTENDED.
    }
    case MotionEvent.ACTION_UP: {
        if (mPanningPointerId != MotionEvent.INVALID_POINTER_ID) {
            mPanningPointerId = MotionEvent.INVALID_POINTER_ID;
            return true;
        } else {
            return false;
        }
    }
    case MotionEvent.ACTION_CANCEL: {
        if (mPanningPointerId != MotionEvent.INVALID_POINTER_ID) {
            // When cancelled, reset to original scroll position.
            scrollTo(mOriginalScrollX, mOriginalScrollY);
            mPanningPointerId = MotionEvent.INVALID_POINTER_ID;
            return true;
        } else {
            return false;
        }
    }
    default: {
        break;
    }
    }

    return false;
}

From source file:com.runmit.sweedee.view.UnderlinePageIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }//from   w  ww .j av a 2  s  .  c  o m
    if ((mViewPager == null) || (((RecyclingPagerAdapter) mViewPager.getAdapter()).getRealCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = ((RecyclingPagerAdapter) mViewPager.getAdapter()).getRealCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;
            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.velocityviewpagerindicator.VelocityUnderlinePageIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*w  w w  . j a  va 2 s . c  o m*/
    if ((mVelocityViewPager == null) || (mVelocityViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mVelocityViewPager.isFakeDragging() || mVelocityViewPager.beginFakeDrag()) {
                mVelocityViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mVelocityViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mVelocityViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mVelocityViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityViewPager.isFakeDragging())
            mVelocityViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.halloon.android.util.UnderLinePageIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }//from ww  w . j  av  a2s .c  o m
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            final int pageWidth = (getWidth() - getPaddingLeft() - getPaddingRight()) / count;
            final int currentTouchPosition = (int) Math.floor(ev.getX() / pageWidth);

            mViewPager.setCurrentItem(currentTouchPosition);

            /*
             * if ((mCurrentPage > 0) && (ev.getX() < halfWidth -
             * sixthWidth)) { if (action != MotionEvent.ACTION_CANCEL) {
             * mViewPager.setCurrentItem(mCurrentPage - 1); } return true; }
             * else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth
             * + sixthWidth)) { if (action != MotionEvent.ACTION_CANCEL) {
             * mViewPager.setCurrentItem(mCurrentPage + 1); } return true; }
             */
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:cc.solart.turbo.TurboRecyclerView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    if (!mLoadEnabled || canScrollEnd() || mIsLoading || isEmpty()) {
        return super.onInterceptTouchEvent(e);
    }/*from  www .j  a v  a 2s  .c  o m*/

    if (dispatchOnItemTouchIntercept(e)) {
        return true;
    }

    final int action = MotionEventCompat.getActionMasked(e);
    final int actionIndex = MotionEventCompat.getActionIndex(e);
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mActivePointerId = MotionEventCompat.getPointerId(e, 0);
        mInitialMotionX = getMotionEventX(e, actionIndex);
        mInitialMotionY = getMotionEventY(e, actionIndex);
    }
        break;

    case MotionEvent.ACTION_POINTER_DOWN: {
        mActivePointerId = MotionEventCompat.getPointerId(e, actionIndex);
        mInitialMotionX = getMotionEventX(e, actionIndex);
        mInitialMotionY = getMotionEventY(e, actionIndex);
    }
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mActivePointerId = INVALID_POINTER;
        break;
    case MotionEventCompat.ACTION_POINTER_UP: {
        onPointerUp(e);
    }
        break;
    }
    return super.onInterceptTouchEvent(e);
}

From source file:com.viewpagerindicator.LoopingLinePageIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from   w  w  w.j a  va2s.co m*/
    if ((mViewPager == null) || (getRealCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = getRealCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.android.hiparker.lierda_light.view.viewpagerindicator.LinePageIndicator.java

public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from www . j a  va 2  s .c om*/
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }

        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.tsingning.view.TabPageIndicators.LinePageIndicator.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*ww  w.  j  a v  a 2  s .  co m*/
    if (mViewPager == null || mViewPager.getAdapter().getCount() == 0) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if (mCurrentPage > 0 && ev.getX() < halfWidth - sixthWidth) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if (mCurrentPage < count - 1 && ev.getX() > halfWidth + sixthWidth) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging()) {
            mViewPager.endFakeDrag();
        }
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.tasomaniac.openwith.resolver.ResolverDrawerLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    mVelocityTracker.addMovement(ev);//from w ww.j av  a2  s.co  m

    boolean handled = false;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX(0);
        final float y = ev.getY(0);
        mInitialTouchX = x;
        mInitialTouchY = mLastTouchY = y;
        mActivePointerId = ev.getPointerId(0);
        final boolean hitView = findChildUnder(mInitialTouchX, mInitialTouchY) != null;
        handled = (!hitView && mOnDismissedListener != null) || mCollapsibleHeight > 0;
        mIsDragging = hitView && handled;
        abortAnimation();
    }
        break;

    case MotionEvent.ACTION_MOVE: {
        int index = ev.findPointerIndex(mActivePointerId);
        if (index < 0) {
            Timber.e("Bad pointer id %d, resetting", mActivePointerId);
            index = 0;
            mActivePointerId = ev.getPointerId(0);
            mInitialTouchX = ev.getX(0);
            mInitialTouchY = mLastTouchY = ev.getY(0);
        }
        final float x = ev.getX(index);
        final float y = ev.getY(index);
        if (!mIsDragging) {
            final float dy = y - mInitialTouchY;
            if (Math.abs(dy) > mTouchSlop && findChildUnder(x, y) != null) {
                handled = mIsDragging = true;
                mLastTouchY = Math.max(mLastTouchY - mTouchSlop,
                        Math.min(mLastTouchY + dy, mLastTouchY + mTouchSlop));
            }
        }
        if (mIsDragging) {
            final float dy = y - mLastTouchY;
            performDrag(dy);
        }
        mLastTouchY = y;
    }
        break;

    case MotionEvent.ACTION_POINTER_DOWN: {
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);

        mActivePointerId = ev.getPointerId(pointerIndex);
        mInitialTouchX = ev.getX(pointerIndex);
        mInitialTouchY = mLastTouchY = ev.getY(pointerIndex);
    }
        break;

    case MotionEvent.ACTION_POINTER_UP: {
        onSecondaryPointerUp(ev);
    }
        break;

    case MotionEvent.ACTION_UP: {
        final boolean wasDragging = mIsDragging;
        mIsDragging = false;
        if (!wasDragging && findChildUnder(mInitialTouchX, mInitialTouchY) == null
                && findChildUnder(ev.getX(0), ev.getY(0)) == null) {
            if (mOnDismissedListener != null) {
                dispatchOnDismissed();
                resetTouch();
                return true;
            }
        }
        if (mOpenOnClick && Math.abs(ev.getX(0) - mInitialTouchX) < mTouchSlop
                && Math.abs(ev.getY(0) - mInitialTouchY) < mTouchSlop) {
            smoothScrollTo(0, 0);
            return true;
        }
        mVelocityTracker.computeCurrentVelocity(1000);
        final float yvel = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
        if (Math.abs(yvel) > mMinFlingVelocity) {
            if (mOnDismissedListener != null && yvel > 0 && mCollapseOffset > mCollapsibleHeight) {
                smoothScrollTo(mCollapsibleHeight + mUncollapsibleHeight, yvel);
                mDismissOnScrollerFinished = true;
            } else {
                smoothScrollTo(yvel < 0 ? 0 : mCollapsibleHeight, yvel);
            }
        } else {
            smoothScrollTo(mCollapseOffset < mCollapsibleHeight / 2 ? 0 : mCollapsibleHeight, 0);
        }
        resetTouch();
    }
        break;

    case MotionEvent.ACTION_CANCEL: {
        if (mIsDragging) {
            smoothScrollTo(mCollapseOffset < mCollapsibleHeight / 2 ? 0 : mCollapsibleHeight, 0);
        }
        resetTouch();
        return true;
    }
    }

    return handled;
}