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.oguzbabaoglu.cardpager.CardPager.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent ev) {

    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;
    }/*from w  w w. j  a  v  a  2 s  . c o  m*/

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

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

    final int action = ev.getAction();

    switch (action & MotionEventCompat.ACTION_MASK) {

    case MotionEvent.ACTION_DOWN:

        // Do not interfere with the settling action.
        if (scrollState != SCROLL_STATE_SETTLING) {
            scroller.abortAnimation();
            populatePending = false;
            populate();
        }

        // Remember where the motion event started
        lastMotionX = initialMotionX = ev.getX();
        lastMotionY = initialMotionY = ev.getY();
        activePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;

    case MotionEvent.ACTION_MOVE:
        if (!dragInProgress) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float xDiff = Math.abs(x - lastMotionX);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float yDiff = Math.abs(y - lastMotionY);

            if (xDiff > touchSlop && xDiff > yDiff) {

                dragInProgress = true;
                lastMotionX = x - initialMotionX > 0 ? initialMotionX + touchSlop : initialMotionX - touchSlop;
                lastMotionY = y;
                setScrollState(SCROLL_STATE_DRAGGING);

                // Disallow Parent Intercept, just in case
                requestParentDisallowInterceptTouchEvent(true);
            }
        }
        // Not else! Note that dragInProgress can be set above.
        if (dragInProgress) {
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            performDrag(x);
        }
        break;

    case MotionEvent.ACTION_UP:

        if (!dragInProgress) {
            break;
        }

        final VelocityTracker velocityTracker = this.velocityTracker;
        velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
        final int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, activePointerId);

        populatePending = true;

        final int width = getClientWidth();
        final int scrollX = getScrollX();
        final ItemInfo ii = infoForCurrentScrollPosition();
        final int currentPage = ii.position;
        final float pageOffset = ((float) scrollX / width) - ii.offset;
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final int totalDelta = (int) (x - initialMotionX);
        final int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);

        setCurrentItemInternal(nextPage, true, true, initialVelocity);
        activePointerId = INVALID_POINTER;
        endDrag();
        break;

    case MotionEvent.ACTION_CANCEL:

        if (!dragInProgress) {
            break;
        }

        scrollToItem(currentItem, true, 0);
        activePointerId = INVALID_POINTER;
        endDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN:
        final int index = MotionEventCompat.getActionIndex(ev);
        lastMotionX = MotionEventCompat.getX(ev, index);
        activePointerId = MotionEventCompat.getPointerId(ev, index);
        break;

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        lastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, activePointerId));
        break;
    }

    return true;
}

From source file:android.support.v7.widget.RecyclerView.java

@Override
public boolean onTouchEvent(MotionEvent e) {
    if (dispatchOnItemTouch(e)) {
        cancelTouch();//from  w  ww . jav  a  2  s .co m
        return true;
    }

    final boolean canScrollHorizontally = mLayout.canScrollHorizontally();
    final boolean canScrollVertically = mLayout.canScrollVertically();

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

    final int action = MotionEventCompat.getActionMasked(e);
    final int actionIndex = MotionEventCompat.getActionIndex(e);

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mScrollPointerId = MotionEventCompat.getPointerId(e, 0);
        mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f);
        mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f);
    }
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex);
        mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
        mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
    }
        break;

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId);
        if (index < 0) {
            Log.e(TAG, "Error processing scroll; pointer index for id " + mScrollPointerId
                    + " not found. Did any MotionEvents get skipped?");
            return false;
        }

        final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f);
        final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f);
        if (mScrollState != SCROLL_STATE_DRAGGING) {
            final int dx = x - mInitialTouchX;
            final int dy = y - mInitialTouchY;
            boolean startScroll = false;
            if (canScrollHorizontally && Math.abs(dx) > mTouchSlop) {
                mLastTouchX = mInitialTouchX + mTouchSlop * (dx < 0 ? -1 : 1);
                startScroll = true;
            }
            if (canScrollVertically && Math.abs(dy) > mTouchSlop) {
                mLastTouchY = mInitialTouchY + mTouchSlop * (dy < 0 ? -1 : 1);
                startScroll = true;
            }
            if (startScroll) {
                getParent().requestDisallowInterceptTouchEvent(true);
                setScrollState(SCROLL_STATE_DRAGGING);
            }
        }
        if (mScrollState == SCROLL_STATE_DRAGGING) {
            final int dx = x - mLastTouchX;
            final int dy = y - mLastTouchY;
            scrollByInternal(canScrollHorizontally ? -dx : 0, canScrollVertically ? -dy : 0);
        }
        mLastTouchX = x;
        mLastTouchY = y;
    }
        break;

    case MotionEventCompat.ACTION_POINTER_UP: {
        onPointerUp(e);
    }
        break;

    case MotionEvent.ACTION_UP: {
        mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
        final float xvel = canScrollHorizontally
                ? -VelocityTrackerCompat.getXVelocity(mVelocityTracker, mScrollPointerId)
                : 0;
        final float yvel = canScrollVertically
                ? -VelocityTrackerCompat.getYVelocity(mVelocityTracker, mScrollPointerId)
                : 0;
        if (!((xvel != 0 || yvel != 0) && fling((int) xvel, (int) yvel))) {
            setScrollState(SCROLL_STATE_IDLE);
        }
        mVelocityTracker.clear();
        releaseGlows();
    }
        break;

    case MotionEvent.ACTION_CANCEL: {
        cancelTouch();
    }
        break;
    }

    return true;
}

From source file:com.hippo.refreshlayout.RefreshLayout.java

private boolean headerTouchEvent(MotionEvent ev) {
    int pointerIndex;
    final int action = MotionEventCompat.getActionMasked(ev);
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = ev.getPointerId(0);
        mIsHeaderBeingDragged = false;//from   w w w  . j  a va2 s  .  c o  m
        break;

    case MotionEvent.ACTION_MOVE: {
        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        final float y = ev.getY(pointerIndex);
        startHeaderDragging(y);

        if (mIsHeaderBeingDragged) {
            final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
            if (overscrollTop > 0) {
                moveSpinner(overscrollTop);
            } else {
                return false;
            }
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        pointerIndex = MotionEventCompat.getActionIndex(ev);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_POINTER_DOWN event but have an invalid action index.");
            return false;
        }
        mActivePointerId = ev.getPointerId(pointerIndex);
        break;
    }

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

    case MotionEvent.ACTION_UP: {
        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
            return false;
        }

        if (mIsHeaderBeingDragged) {
            final float y = ev.getY(pointerIndex);
            final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
            mIsHeaderBeingDragged = false;
            finishSpinner(overscrollTop);
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }
    case MotionEvent.ACTION_CANCEL:
        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
            return false;
        }

        if (mIsHeaderBeingDragged) {
            mIsHeaderBeingDragged = false;
            finishSpinner(0);
        }
        mActivePointerId = INVALID_POINTER;
        return false;
    }

    return true;
}

From source file:com.usabusi.swiperefreshlayoutupdown.view.SwipeRefreshLayoutUpDown.java

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

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;/*from   w w  w.j  av a  2  s .  c o m*/
        Log.e(LOG_TAG, "mReturningToStart = false in onTouchEvent");
    }

    if (!isEnabled() || mReturningToStart || mRefreshing || mLoading) {
        boolean bisEnabled;
        bisEnabled = !isEnabled();
        String strLogout = String.format(
                "Got return false event in onTouchEvent.!isEnabled()= %b,mReturningToStart= %b,mRefreshing =%b,mRefreshing=%b",
                bisEnabled, mReturningToStart, mRefreshing, mLoading);
        Log.e(LOG_TAG, strLogout);
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }
    if ((mMode.permitsPullFromStartOnly() && canChildScrollUp())
            || (mMode.permitsPullFromEndOnly() && canChildScrollDown())) {
        boolean bcanChildScrollDown, bcanChildScrollUp;
        bcanChildScrollDown = canChildScrollDown();
        bcanChildScrollUp = canChildScrollUp();
        boolean bpermitsPullFromStartOnly = mMode.permitsPullFromStartOnly();
        String strLogout2 = String.format(
                "Got return false permitsPullFromStart/End Only event in onTouchEvent.canChildScrollDown()= %b,canChildScrollUp() =%b,bpermitsPullFromStartOnly=%b",
                bcanChildScrollDown, bcanChildScrollUp, bpermitsPullFromStartOnly);
        Log.e(LOG_TAG, strLogout2);
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }
    /*
    if (mMode.permitsPullFromBoth() &&
        (canChildScrollUp() &&  canChildScrollDown() ) ) {
    boolean bcanChildScrollDown,bcanChildScrollUp;
    bcanChildScrollDown=canChildScrollDown();
    bcanChildScrollUp=canChildScrollUp();
    String strLogout3= String.format( "Got return false permitsPullFromBoth event in onTouchEvent.canChildScrollDown()= %b,canChildScrollUp() =%b",bcanChildScrollDown,bcanChildScrollUp);
    Log.e(LOG_TAG, strLogout3);
    // Fail fast if we're not in a state where a swipe is possible
    return false;
    }*/
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        Log.e(LOG_TAG, "Got ACTION_DOWN event in onTouchEvent.");
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        //v21 updown csdn version
        mLastMotionY = mInitialMotionY = ev.getY();

        mStartPoint = mInitialMotionY;

        up = canChildScrollUp();
        down = canChildScrollDown();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id in in onTouchEvent.");
            return false;
        } else
            Log.e(LOG_TAG, "Got ACTION_MOVE event in onTouchEvent.");
        if (mIsBeingDragged) {
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            //v21 updown
            //float yDiff = y - mInitialMotionY;
            float yDiff = y - mStartPoint;
            float overscrollTopValue = yDiff * DRAG_RATE;//< mTotalDragDistance
            // User velocity passed min velocity; trigger a refresh
            if (overscrollTopValue > mTotalDragDistance) {
                // User movement passed distance; trigger a refresh
                Log.e(LOG_TAG,
                        "Got overscrollTopValue > mTotalDragDistance when ACTION_MOVE event in onTouchEvent.");
                if (mLastDirection == PullMode.PULL_FROM_END) {
                    return true;
                }
                if (mMode.permitsPullFromStart()) {
                    mLastDirection = PullMode.PULL_FROM_START;
                    mCurrentMode = PullMode.PULL_FROM_START;
                    //startRefresh();
                }
            } else if (-overscrollTopValue > mTotalDragDistance) {
                Log.e(LOG_TAG,
                        "Got -overscrollTopValue > mTotalDragDistance when ACTION_MOVE event in onTouchEvent.");
                if ((!up && !down && !loadNoFull) || mLastDirection == PullMode.PULL_FROM_START) {
                    return true;
                }
                if (mMode.permitsPullFromEnd()) {
                    mLastDirection = PullMode.PULL_FROM_END;
                    mCurrentMode = PullMode.PULL_FROM_END;
                    yDiff = -yDiff;
                    //startLoad();
                }
            } else {
                Log.e(LOG_TAG,
                        "Got overscrollTopValue between -mTotalDragDistance and mTotalDragDistancewhen ACTION_MOVE event in onTouchEvent.");
                if (!up && !down && yDiff < 0 && !loadNoFull) {
                    return true;
                }
                // Just track the user's movement
                //????
                //                        setTriggerPercentage(
                //                                mAccelerateInterpolator.getInterpolation(
                //                                        Math.abs(yDiff) / mDistanceToTriggerSync));
                //                        updateContentOffsetTop((int) yDiff);
                //                        if (mTarget.getTop() == getPaddingTop()) {
                //                            // If the user puts the view back at the top, we
                //                            // don't need to. This shouldn't be considered
                //                            // cancelling the gesture as the user can restart from the top.
                //                            removeCallbacks(mCancel);
                //                            mLastDirection = PullMode.DISABLED;
                //                        } else {
                //                            mDirection = (yDiff > 0 ? 1 : -1);
                //                            updatePositionTimeout();
                //                        }
            }

            final float overscrollTop = yDiff * DRAG_RATE;
            mProgress.showArrow(true);
            float originalDragPercent = overscrollTop / mTotalDragDistance;
            if (originalDragPercent < 0) {
                Log.e(LOG_TAG, "Got originalDragPercent <  0 when ACTION_MOVE event in onTouchEvent.");
                return false;
            }
            float dragPercent = Math.min(1f, Math.abs(originalDragPercent));
            float adjustedPercent = (float) Math.max(dragPercent - .4, 0) * 5 / 3;
            float extraOS = Math.abs(overscrollTop) - mTotalDragDistance;
            float slingshotDist = mUsingCustomStart ? mSpinnerFinalOffset - mOriginalOffsetTop
                    : mSpinnerFinalOffset;
            float tensionSlingshotPercent = Math.max(0, Math.min(extraOS, slingshotDist * 2) / slingshotDist);
            float tensionPercent = (float) ((tensionSlingshotPercent / 4)
                    - Math.pow((tensionSlingshotPercent / 4), 2)) * 2f;
            float extraMove = (slingshotDist) * tensionPercent * 2;

            int targetY = mOriginalOffsetTop + (int) ((slingshotDist * dragPercent) + extraMove);
            // where 1.0f is a full circle
            if (mCircleView.getVisibility() != View.VISIBLE) {
                mCircleView.setVisibility(View.VISIBLE);
            }
            if (!mScale) {
                ViewCompat.setScaleX(mCircleView, 1f);
                ViewCompat.setScaleY(mCircleView, 1f);
            }
            if (overscrollTop < mTotalDragDistance) {
                if (mScale) {
                    setAnimationProgress(overscrollTop / mTotalDragDistance);
                }
                if (mProgress.getAlpha() > STARTING_PROGRESS_ALPHA
                        && !isAnimationRunning(mAlphaStartAnimation)) {
                    // Animate the alpha
                    startProgressAlphaStartAnimation();
                }
                float strokeStart = (float) (adjustedPercent * .8f);
                mProgress.setStartEndTrim(0f, Math.min(MAX_PROGRESS_ANGLE, strokeStart));
                mProgress.setArrowScale(Math.min(1f, adjustedPercent));
            } else {
                if (mProgress.getAlpha() < MAX_ALPHA && !isAnimationRunning(mAlphaMaxAnimation)) {
                    //                        //V21 updown csdn version
                    //                            if (mLastDirection ==PullMode.PULL_FROM_END) {
                    //                                int progressBarHeight = getHeight();
                    //                                int progressBarBottom = getBottom();
                    //                                int progressBarTop = getTop();
                    //                                int top = 40;
                    //                                //http://stackoverflow.com/questions/26484907/setrefreshingtrue-does-not-show-indicator
                    //                                //?
                    //                                //https://code.google.com/p/android/issues/detail?id=77712
                    //                                //?
                    //                                //https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/BaseActivity.java
                    ////http://stackoverflow.com/questions/26484907/setrefreshingtrue-does-not-show-indicator
                    //                            import android.view.Display;
                    //                            import android.graphics.Point;
                    ////                                Display display = getDefaultDisplay();
                    ////                                Point size = new Point();
                    ////                                display.getSize(size);
                    ////                                int height = size.y;
                    //                                setProgressViewOffset(false, 200, 500);
                    //
                    //                                //??
                    //                                //setProgressViewOffset(false, 300, 900);
                    //                                //top - progressBarBottom+500 , top+300);
                    //                                Log.e(LOG_TAG, "setProgressViewOffset top - progressBarBottom when ACTION_MOVE event in onTouchEvent.");
                    //                                invalidate();
                    ////                            http://stackoverflow.com/questions/26493213/android-swiperefreshlayout-no-animation-on-fragment-creation/26640352#26640352
                    ////                            It depends on which API level you're building under - if you're using up to API 20 then you can just turn on setRefreshing(true), this will run the animation in the ActionBar, but in API 21 (Material Design) they changed the progress to be a spinner than is "pulled into view" before it spins
                    ////
                    ////                            You have 2 ways of getting around this in API 21: 1) shift the spinner down with setProgressViewOffset(), but remember to shift it back up afterwords (note that this works in px, while setDistanceToTriggerSync() uses dp) 2) make a duplicate spinner that is displayed when you're loading the data
                    ////
                    ////                            The more code-efficient solution is to use the existing spinner, but you have to be careful that you do reset its position
                    ////
                    ////                            If you need to calculate the pixel density, you can grab it from:
                    ////
                    ////                            DisplayMetrics metrics = new DisplayMetrics();
                    ////                            activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
                    ////                            float scale = metrics.density;
                    //                            }
                    // Animate the alpha
                    startProgressAlphaMaxAnimation();
                }
            }
            float rotation = (-0.25f + .4f * adjustedPercent + tensionPercent * 2) * .5f;
            mProgress.setProgressRotation(rotation);
            //mCurrentTargetOffsetTop-=800;
            setTargetOffsetTopAndBottom(targetY - mCurrentTargetOffsetTop, true /* requires update */);
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        //v21 updown
        mLastMotionY = MotionEventCompat.getY(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

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

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {

        if (mActivePointerId == INVALID_POINTER) {
            if (action == MotionEvent.ACTION_UP) {
                Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id in onTouchEvent.");
            }
            return false;
        }
        if (action == MotionEvent.ACTION_UP) {
            Log.e(LOG_TAG, "ACTION_UP in onTouchEvent");
        } else if (action == MotionEvent.ACTION_CANCEL) {
            Log.e(LOG_TAG, "ACTION_CANCEL in onTouchEvent");
        }
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        //float yDiff = y - mInitialMotionY;

        float yDiff = y - mStartPoint;
        //float overscrollTopValue = yDiff * DRAG_RATE;//< mTotalDragDistance
        float overscrollTop = yDiff * DRAG_RATE;
        mIsBeingDragged = false;
        // User velocity passed min velocity; trigger a refresh
        if (overscrollTop > mTotalDragDistance) {
            // User movement passed distance; trigger a refresh
            Log.e(LOG_TAG,
                    "Got overscrollTopValue > mTotalDragDistance when ACTION_MOVE event in onTouchEvent.");
            if (mLastDirection == PullMode.PULL_FROM_END) {
                String strLogout = String.format("Got return false  in overscrollTop= %f,mTotalDragDistance=%f",
                        overscrollTop, mTotalDragDistance);
                Log.e(LOG_TAG, strLogout);
                animateStop();//return true;
            }
            if (mMode.permitsPullFromStart()) {
                mLastDirection = PullMode.PULL_FROM_START;
                mCurrentMode = PullMode.PULL_FROM_START;
                //startRefresh();
                setRefreshing(true, true /* notify */);
                Log.e(LOG_TAG, "setRefreshing(true, true) ACTION_UP in onTouchEvent");
            }
        } else if (-overscrollTop > mTotalDragDistance) {
            Log.e(LOG_TAG,
                    "Got -overscrollTopValue > mTotalDragDistance when ACTION_MOVE event in onTouchEvent.");
            if ((!up && !down && !loadNoFull) || mLastDirection == PullMode.PULL_FROM_START) {
                String strLogout = String.format("Got return false  in overscrollTop= %f,mTotalDragDistance=%f",
                        overscrollTop, mTotalDragDistance);
                Log.e(LOG_TAG, strLogout);
                animateStop();//return true;
            }
            if (mMode.permitsPullFromEnd()) {
                mLastDirection = PullMode.PULL_FROM_END;
                mCurrentMode = PullMode.PULL_FROM_END;
                yDiff = -yDiff;
                setRefreshing(true, true /* notify */);
                Log.e(LOG_TAG, "setRefreshing(true, true) ACTION_UP in onTouchEvent");
                //startLoad();
            }
        } else {
            Log.e(LOG_TAG,
                    "Got overscrollTopValue between -mTotalDragDistance and mTotalDragDistancewhen ACTION_MOVE event in onTouchEvent.");
            if (!up && !down && yDiff < 0 && !loadNoFull) {
                String strLogout = String.format("Got return false  in overscrollTop= %f,mTotalDragDistance=%f",
                        overscrollTop, mTotalDragDistance);
                Log.e(LOG_TAG, strLogout);
                animateStop();//return true;
            }
            // Just track the user's movement
            //????
            //                        setTriggerPercentage(
            //                                mAccelerateInterpolator.getInterpolation(
            //                                        Math.abs(yDiff) / mDistanceToTriggerSync));
            //                        updateContentOffsetTop((int) yDiff);
            //                        if (mTarget.getTop() == getPaddingTop()) {
            //                            // If the user puts the view back at the top, we
            //                            // don't need to. This shouldn't be considered
            //                            // cancelling the gesture as the user can restart from the top.
            //                            removeCallbacks(mCancel);
            //                            mLastDirection = PullMode.DISABLED;
            //                        } else {
            //                            mDirection = (yDiff > 0 ? 1 : -1);
            //                            updatePositionTimeout();
            //                        }
        }

        mActivePointerId = INVALID_POINTER;
        //v21 updown csdn version
        mLastDirection = PullMode.DISABLED;
        return false;
    }
    }

    return true;
}

From source file:com.lixin.foodmarket.view.LazyViewPager.java

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going ic_up. Choose a new
        // active pointer and adjust accordingly.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastMotionX = MotionEventCompat.getX(ev, newPointerIndex);
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();//  w w w .  j  a v  a  2  s.  c  o  m
        }
    }
}

From source file:android.support.v7.widget.RecyclerView.java

private void onPointerUp(MotionEvent e) {
    final int actionIndex = MotionEventCompat.getActionIndex(e);
    if (MotionEventCompat.getPointerId(e, actionIndex) == mScrollPointerId) {
        // Pick a new pointer to pick up the slack.
        final int newIndex = actionIndex == 0 ? 1 : 0;
        mScrollPointerId = MotionEventCompat.getPointerId(e, newIndex);
        mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, newIndex) + 0.5f);
        mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, newIndex) + 0.5f);
    }/*w  w  w  . j a v a2 s .  co  m*/
}

From source file:com.wevalue.view.LazyViewPager.java

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a news
        // active pointer and adjust accordingly.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastMotionX = MotionEventCompat.getX(ev, newPointerIndex);
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();/*from  w w  w  .java2 s .c o  m*/
        }
    }
}

From source file:de.andacaydin.bidirectionalviewpagerlibrary.BiDirectionalViewPager.java

@Override
public boolean onTouchEvent(MotionEvent ev) {

    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;
    }//from  w  w w . j ava2  s.c  o  m

    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();

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        mScroller.abortAnimation();
        //             mPopulatePending = false;
        //                populate();

        //automatic H/V-Detection
        downX = ev.getX();
        downY = ev.getY();

        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        completeScroll();

        // Remember where the motion event started
        if (mOrientation == HORIZONTAL) {
            mLastMotionX = mInitialMotionX = ev.getX();
        } else {
            mLastMotionY = mInitialMotionY = ev.getY();
        }
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    }
    case MotionEvent.ACTION_MOVE:

        //auto H/V-detection
        float moveX = ev.getX();
        float moveY = ev.getY();

        float deltaX = Math.abs(downX - moveX);
        float deltaY = Math.abs(downY - moveY);

        if (!isOrientationModeLocked) {
            if (deltaY > deltaX && deltaY > MIN_HORIZONTAL_VERTICAL_LOCK_DISTANCE) {
                Log.v(TAG, "lock vertical");
                this.setOrientation(VERTICAL);
                isOrientationModeLocked = true;
            } else if (deltaX > deltaY && deltaX > MIN_HORIZONTAL_VERTICAL_LOCK_DISTANCE) {
                Log.v(TAG, "lock horizontal");
                this.setOrientation(HORIZONTAL);
                isOrientationModeLocked = true;
            } else {
                Log.v(TAG, "Not Locked: DeltaX=(" + deltaX + ") DeltaY=(" + deltaY + ")");
                break;
            }
        }

        if (!mIsBeingDragged) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float xDiff = Math.abs(x - mLastMotionX);
            final float yDiff = Math.abs(y - mLastMotionY);
            float primaryDiff;
            float secondaryDiff;

            if (mOrientation == HORIZONTAL) {
                primaryDiff = xDiff;
                secondaryDiff = yDiff;
            } else {
                primaryDiff = yDiff;
                secondaryDiff = xDiff;
            }

            if (DEBUG)
                Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
            if (primaryDiff > mTouchSlop && primaryDiff > secondaryDiff) {
                if (DEBUG)
                    Log.v(TAG, "Starting drag!");
                mIsBeingDragged = true;
                if (mOrientation == HORIZONTAL) {
                    mLastMotionX = x;
                } else {
                    mLastMotionY = y;
                }
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);
            }
        }
        if (mIsBeingDragged) {
            mPopulatePending = true;
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float y = MotionEventCompat.getY(ev, activePointerIndex);

            int size;
            float scroll;

            if (mOrientation == HORIZONTAL) {
                size = getWidth();
                scroll = getScrollX() + (mLastMotionX - x);
                mLastMotionX = x;
            } else {
                size = getHeight();
                scroll = getScrollY() + (mLastMotionY - y);
                mLastMotionY = y;
            }
            // Log.i(TAG, "mLastMotionY ="+y+" mLastMotionX="+x);
            if (DEBUG)
                Log.i(TAG, "current Item =" + mCurItem);
            final float lowerBound = computePrevPosition() == 0 ? size : 0; //Math.max(0, (mCurItem - 1) * size);
            final float upperBound = computeNextPosition() == 0 ? size : size * 2;
            // Math.min(mCurItem + 1, mAdapter.getCount() - 1) * size;
            if (scroll < lowerBound) {
                scroll = lowerBound;
            } else if (scroll > upperBound) {
                scroll = upperBound;
            }
            if (mOrientation == HORIZONTAL) {
                scrollTo((int) scroll, getScrollY());
            } else {
                scrollTo(getScrollX(), (int) scroll);
            }
            //mLastMotionY += scroll - (int) scroll;
            //mLastMotionX += scroll - (int) scroll;

            if (mOnPageChangeListener != null) {
                final int position = (int) scroll / size;
                final int positionOffsetPixels = (int) scroll % size;
                final float positionOffset = (float) positionOffsetPixels / size;
                mOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        isOrientationModeLocked = false;
        //TODO snap back to correct position
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity;
            float lastMotion;
            int sizeOverThree;
            float initialMotion;
            if (mOrientation == HORIZONTAL) {
                initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
                lastMotion = mLastMotionX;
                sizeOverThree = getWidth() / 3;
                initialMotion = mInitialMotionX;
            } else {
                initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
                lastMotion = mLastMotionY;
                sizeOverThree = getHeight() / 3;
                initialMotion = mInitialMotionY;
            }

            mPopulatePending = true;
            if ((Math.abs(initialVelocity) > mMinimumVelocity)
                    || Math.abs(initialMotion - lastMotion) >= sizeOverThree) {
                Log.i(TAG, "lastMotion=" + lastMotion + "mInitialMotionX=" + initialMotion);
                if (lastMotion > initialMotion) {
                    if (computePrevPosition() != null)
                        setCurrentItemInternal(computePrevPosition(), true, true);
                } else {
                    if (computeNextPosition() != null)
                        setCurrentItemInternal(computeNextPosition(), true, true);
                }
            } else {
                setCurrentItemInternal(0, true, true); //snap back to original position
            }

            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            setCurrentItemInternal(mCurItem, true, true);
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        isOrientationModeLocked = false;
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        if (mOrientation == HORIZONTAL) {
            mLastMotionX = MotionEventCompat.getX(ev, index);
        } else {
            mLastMotionY = MotionEventCompat.getY(ev, index);
        }
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mLastMotionY = MotionEventCompat.getY(ev, index);
        break;
    }
    return true;
}

From source file:com.cnpeng.cnpeng_mydemosfrom2016_12.a_12_GetLocalFiles_VP_FM.CustomNoPreLoadViewPager.java

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new  
        // active pointer and adjust accordingly.  
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastMotionX = MotionEventCompat.getX(ev, newPointerIndex);
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();/*w  ww  .ja  v a  2s.  c o  m*/
        }
    }
}

From source file:com.hippo.refreshlayout.RefreshLayout.java

private boolean footerTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    int pointerIndex;
    float y;/*from  ww  w .  j  a v  a2 s . co  m*/
    float yDiff;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = ev.getPointerId(0);
        mIsFooterBeingDragged = false;
        mFooterCurrPercentage = 0;
        break;

    case MotionEvent.ACTION_MOVE:
        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        y = ev.getY(pointerIndex);
        if (!mIsFooterBeingDragged) {
            yDiff = y - mInitialDownY;
            if (yDiff < -mTouchSlop) {
                mIsFooterBeingDragged = true;
                mInitialMotionY = mInitialDownY - mTouchSlop;
            }
        }

        if (mIsFooterBeingDragged) {
            yDiff = y - mInitialMotionY;
            setTriggerPercentage(mAccelerateInterpolator.getInterpolation(
                    MathUtils.clamp(-yDiff, 0, mFooterDistanceToTriggerSync) / mFooterDistanceToTriggerSync));
        }
        break;

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

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

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (mActivePointerId == INVALID_POINTER && pointerIndex < 0) {
            if (action == MotionEvent.ACTION_UP) {
                Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
            }
            return false;
        }

        try {
            y = ev.getY(pointerIndex);
        } catch (Throwable e) {
            y = 0;
        }

        yDiff = y - mInitialMotionY;

        if (action == MotionEvent.ACTION_UP && -yDiff > mFooterDistanceToTriggerSync) {
            // User movement passed distance; trigger a refresh
            startFooterRefresh();
        } else {
            mCancel.run();
        }

        mIsFooterBeingDragged = false;
        mFooterCurrPercentage = 0;
        mActivePointerId = INVALID_POINTER;
        return false;
    }

    return mIsFooterBeingDragged;
}