Example usage for android.view MotionEvent findPointerIndex

List of usage examples for android.view MotionEvent findPointerIndex

Introduction

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

Prototype

public final int findPointerIndex(int pointerId) 

Source Link

Document

Given a pointer identifier, find the index of its data in the event.

Usage

From source file:com.android.launcher3.ItemTouchHelper.java

private android.support.v7.widget.RecyclerView.ViewHolder findSwipedView(MotionEvent motionEvent) {
    final android.support.v7.widget.RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager();
    if (mActivePointerId == ACTIVE_POINTER_ID_NONE) {
        return null;
    }/*  w w  w .j  a  v  a2  s.  c o m*/
    final int pointerIndex = motionEvent.findPointerIndex(mActivePointerId);
    final float dx = motionEvent.getX(pointerIndex) - mInitialTouchX;
    final float dy = motionEvent.getY(pointerIndex) - mInitialTouchY;
    final float absDx = Math.abs(dx);
    final float absDy = Math.abs(dy);

    if (absDx < mSlop && absDy < mSlop) {
        return null;
    }
    if (absDx > absDy && lm.canScrollHorizontally()) {
        return null;
    } else if (absDy > absDx && lm.canScrollVertically()) {
        return null;
    }
    View child = findChildView(motionEvent);
    if (child == null) {
        return null;
    }
    return mRecyclerView.getChildViewHolder(child);
}

From source file:com.android.systemui.statusbar.phone.NotificationPanelView.java

private void onQsTouch(MotionEvent event) {
    int pointerIndex = event.findPointerIndex(mTrackingPointer);
    if (pointerIndex < 0) {
        pointerIndex = 0;//  w w w .  j a  va 2  s .co  m
        mTrackingPointer = event.getPointerId(pointerIndex);
    }
    final float y = event.getY(pointerIndex);
    final float x = event.getX(pointerIndex);
    final float h = y - mInitialTouchY;
    logf("onQsTouch() touch event = " + event.getActionMasked());
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        logf("onQsTouch() touch event = MotionEvent.ACTION_DOWN ");
        mQsTracking = true;
        mInitialTouchY = y;
        mInitialTouchX = x;
        onQsExpansionStarted();
        mInitialHeightOnTouch = mQsExpansionHeight;
        initVelocityTracker();
        trackMovement(event);
        break;

    case MotionEvent.ACTION_POINTER_UP:
        logf("onQsTouch() touch event = MotionEvent.ACTION_POINTER_UP ");
        final int upPointer = event.getPointerId(event.getActionIndex());
        if (mTrackingPointer == upPointer) {
            // gesture is ongoing, find a new pointer to track
            final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
            final float newY = event.getY(newIndex);
            final float newX = event.getX(newIndex);
            mTrackingPointer = event.getPointerId(newIndex);
            mInitialHeightOnTouch = mQsExpansionHeight;
            mInitialTouchY = newY;
            mInitialTouchX = newX;
        }
        break;

    case MotionEvent.ACTION_MOVE:
        logf("onQsTouch() touch event = MotionEvent.ACTION_MOVE ");
        setQsExpansion(h + mInitialHeightOnTouch);
        if (h >= getFalsingThreshold()) {
            mQsTouchAboveFalsingThreshold = true;
        }
        trackMovement(event);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        logf("onQsTouch() touch event = MotionEvent.ACTION_UP/ACTION_CANCEL");
        mQsTracking = false;
        mTrackingPointer = -1;
        trackMovement(event);
        float fraction = getQsExpansionFraction();
        if ((fraction != 0f || y >= mInitialTouchY) && (fraction != 1f || y <= mInitialTouchY)) {
            flingQsWithCurrentVelocity(y, event.getActionMasked() == MotionEvent.ACTION_CANCEL);
        } else {
            logQsSwipeDown(y);
            mScrollYOverride = -1;
        }
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        break;
    }
}

From source file:com.android.systemui.statusbar.phone.NotificationPanelView.java

private boolean onQsIntercept(MotionEvent event) {
    int pointerIndex = event.findPointerIndex(mTrackingPointer);
    if (pointerIndex < 0) {
        pointerIndex = 0;/*from  www.java  2s. co  m*/
        mTrackingPointer = event.getPointerId(pointerIndex);
    }
    final float x = event.getX(pointerIndex);
    final float y = event.getY(pointerIndex);

    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        mIntercepting = true;
        mInitialTouchY = y;
        mInitialTouchX = x;
        initVelocityTracker();
        trackMovement(event);
        if (shouldQuickSettingsIntercept(mInitialTouchX, mInitialTouchY, 0)) {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        if (mQsExpansionAnimator != null) {
            onQsExpansionStarted();
            mInitialHeightOnTouch = mQsExpansionHeight;
            mQsTracking = true;
            mIntercepting = false;
            mNotificationStackScroller.removeLongPressCallback();
        }
        break;
    case MotionEvent.ACTION_POINTER_UP:
        final int upPointer = event.getPointerId(event.getActionIndex());
        if (mTrackingPointer == upPointer) {
            // gesture is ongoing, find a new pointer to track
            final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1;
            mTrackingPointer = event.getPointerId(newIndex);
            mInitialTouchX = event.getX(newIndex);
            mInitialTouchY = event.getY(newIndex);
        }
        break;

    case MotionEvent.ACTION_MOVE:
        final float h = y - mInitialTouchY;
        trackMovement(event);
        if (mQsTracking) {

            // Already tracking because onOverscrolled was called. We need to update here
            // so we don't stop for a frame until the next touch event gets handled in
            // onTouchEvent.

            setQsExpansion(h + mInitialHeightOnTouch);
            trackMovement(event);
            mIntercepting = false;
            return true;
        }
        if (Math.abs(h) > mTouchSlop && Math.abs(h) > Math.abs(x - mInitialTouchX)
                && shouldQuickSettingsIntercept(mInitialTouchX, mInitialTouchY, h)) {
            mQsTracking = true;
            onQsExpansionStarted();
            notifyExpandingFinished();
            mInitialHeightOnTouch = mQsExpansionHeight;
            mInitialTouchY = y;
            mInitialTouchX = x;
            mIntercepting = false;
            mNotificationStackScroller.removeLongPressCallback();
            return true;
        }
        break;

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        trackMovement(event);
        if (mQsTracking) {
            flingQsWithCurrentVelocity(y, event.getActionMasked() == MotionEvent.ACTION_CANCEL);
            mQsTracking = false;
        }
        mIntercepting = false;
        break;
    }
    return false;
}

From source file:com.aviary.android.feather.sdk.widget.AviaryWorkspace.java

@Override
public boolean onTouchEvent(MotionEvent ev) {

    final int action = ev.getAction();

    if (!isEnabled()) {
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();//ww  w .j  a va2 s  .c  o m
        }
        snapToScreen(mCurrentScreen);
        return false; // We don't want the events. Let them fall through to the all
                      // apps view.
    }

    acquireVelocityTrackerAndAddMovement(ev);

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

        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mLastMotionX = ev.getX();
        mLastMotionX2 = ev.getX();
        mActivePointerId = ev.getPointerId(0);
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1);
        }
        break;
    case MotionEvent.ACTION_MOVE:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            // Scroll to follow the motion event
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float deltaX = mLastMotionX - x;
            final float deltaX2 = mLastMotionX2 - x;
            final int mode = mOverScrollMode;

            mLastMotionX = x;

            if (deltaX < 0) {
                mTouchX += deltaX;
                mSmoothingTime = System.nanoTime() / NANOTIME_DIV;

                if (mTouchX < 0 && mode != OVER_SCROLL_NEVER) {
                    mTouchX = mLastMotionX = 0;
                    // mLastMotionX = x;

                    if (mEdgeGlowLeft != null && deltaX2 < 0) {
                        mEdgeGlowLeft.onPull((float) deltaX / getWidth());
                        if (!mEdgeGlowRight.isFinished()) {
                            mEdgeGlowRight.onRelease();
                        }
                    }
                }

                invalidate();

            } else if (deltaX > 0) {
                final int totalWidth = getScreenScrollPositionX(mItemCount - 1);
                final float availableToScroll = getScreenScrollPositionX(mItemCount) - mTouchX;
                mSmoothingTime = System.nanoTime() / NANOTIME_DIV;

                mTouchX += Math.min(availableToScroll, deltaX);

                if (availableToScroll <= getWidth() && mode != OVER_SCROLL_NEVER) {
                    mTouchX = mLastMotionX = totalWidth;
                    // mLastMotionX = x;

                    if (mEdgeGlowLeft != null && deltaX2 > 0) {
                        mEdgeGlowRight.onPull((float) deltaX / getWidth());
                        if (!mEdgeGlowLeft.isFinished()) {
                            mEdgeGlowLeft.onRelease();
                        }
                    }
                }
                invalidate();

            } else {
                awakenScrollBars();
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            final int velocityX = (int) velocityTracker.getXVelocity(mActivePointerId);

            final int screenWidth = getWidth();
            final int whichScreen = (getScrollX() + (screenWidth / 2)) / screenWidth;
            final float scrolledPos = (float) getScrollX() / screenWidth;

            if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
                // Fling hard enough to move left.
                // Don't fling across more than one screen at a time.
                final int bound = scrolledPos < whichScreen ? mCurrentScreen - 1 : mCurrentScreen;
                snapToScreen(Math.min(whichScreen, bound), velocityX, true);
            } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < mItemCount - 1) {
                // Fling hard enough to move right
                // Don't fling across more than one screen at a time.
                final int bound = scrolledPos > whichScreen ? mCurrentScreen + 1 : mCurrentScreen;
                snapToScreen(Math.max(whichScreen, bound), velocityX, true);
            } else {
                snapToScreen(whichScreen, 0, true);
            }

            if (mEdgeGlowLeft != null) {
                mEdgeGlowLeft.onRelease();
                mEdgeGlowRight.onRelease();
            }
        }
        mTouchState = TOUCH_STATE_REST;
        mActivePointerId = INVALID_POINTER;
        releaseVelocityTracker();
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final int screenWidth = getWidth();
            final int whichScreen = (getScrollX() + (screenWidth / 2)) / screenWidth;
            snapToScreen(whichScreen, 0, true);
        }
        mTouchState = TOUCH_STATE_REST;
        mActivePointerId = INVALID_POINTER;
        releaseVelocityTracker();

        if (mEdgeGlowLeft != null) {
            mEdgeGlowLeft.onRelease();
            mEdgeGlowRight.onRelease();
        }

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

    return true;
}

From source file:cc.flydev.launcher.Page.java

protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
    // Disallow scrolling if we don't have a valid pointer index
    final int pointerIndex = ev.findPointerIndex(mActivePointerId);
    if (pointerIndex == -1)
        return;/*from w ww. j  a v a  2 s.co m*/

    // Disallow scrolling if we started the gesture from outside the viewport
    final float x = ev.getX(pointerIndex);
    final float y = ev.getY(pointerIndex);
    if (!isTouchPointInViewportWithBuffer((int) x, (int) y))
        return;

    final int xDiff = (int) Math.abs(x - mLastMotionX);
    final int yDiff = (int) Math.abs(y - mLastMotionY);

    final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
    boolean xPaged = xDiff > mPagingTouchSlop;
    boolean xMoved = xDiff > touchSlop;
    boolean yMoved = yDiff > touchSlop;

    if (xMoved || xPaged || yMoved) {
        if (mUsePagingTouchSlop ? xPaged : xMoved) {
            // Scroll if the user moved far enough along the X axis
            mTouchState = TOUCH_STATE_SCROLLING;
            mTotalMotionX += Math.abs(mLastMotionX - x);
            mLastMotionX = x;
            mLastMotionXRemainder = 0;
            mTouchX = getViewportOffsetX() + getScrollX();
            mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
            pageBeginMoving();
        }
    }
}

From source file:com.n2hsu.launcher.Page.java

protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
    // Disallow scrolling if we don't have a valid pointer index
    final int pointerIndex = ev.findPointerIndex(mActivePointerId);
    if (pointerIndex == -1)
        return;//from   w  w w . j a va  2 s.  c o m

    // Disallow scrolling if we started the gesture from outside the
    // viewport
    final float x = ev.getX(pointerIndex);
    final float y = ev.getY(pointerIndex);
    if (!isTouchPointInViewportWithBuffer((int) x, (int) y))
        return;

    final int xDiff = (int) Math.abs(x - mLastMotionX);
    final int yDiff = (int) Math.abs(y - mLastMotionY);

    final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
    boolean xPaged = xDiff > mPagingTouchSlop;
    boolean xMoved = xDiff > touchSlop;
    boolean yMoved = yDiff > touchSlop;

    if (xMoved || xPaged || yMoved) {
        if (mUsePagingTouchSlop ? xPaged : xMoved) {
            // Scroll if the user moved far enough along the X axis
            mTouchState = TOUCH_STATE_SCROLLING;
            mTotalMotionX += Math.abs(mLastMotionX - x);
            mLastMotionX = x;
            mLastMotionXRemainder = 0;
            mTouchX = getViewportOffsetX() + getScrollX();
            mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
            pageBeginMoving();
        }
    }
}

From source file:com.android.launcher2.PagedView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // Skip touch handling if there are no pages to swipe
    if (getChildCount() <= 0)
        return super.onTouchEvent(ev);

    acquireVelocityTrackerAndAddMovement(ev);

    final int action = ev.getAction();

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        /*/*  w  ww  .j a v a2s .c o m*/
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mDownMotionX = mLastMotionX = ev.getX();
        mLastMotionXRemainder = 0;
        mTotalMotionX = 0;
        mActivePointerId = ev.getPointerId(0);
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            pageBeginMoving();
        }
        break;

    case MotionEvent.ACTION_MOVE:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            // Scroll to follow the motion event
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float deltaX = mLastMotionX + mLastMotionXRemainder - x;

            mTotalMotionX += Math.abs(deltaX);

            // Only scroll and update mLastMotionX if we have moved some discrete amount.  We
            // keep the remainder because we are actually testing if we've moved from the last
            // scrolled position (which is discrete).
            if (Math.abs(deltaX) >= 1.0f) {
                mTouchX += deltaX;
                mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
                if (!mDeferScrollUpdate) {
                    scrollBy((int) deltaX, 0);
                    if (DEBUG)
                        Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
                } else {
                    invalidate();
                }
                mLastMotionX = x;
                mLastMotionXRemainder = deltaX - (int) deltaX;
            } else {
                awakenScrollBars();
            }
        } else {
            determineScrollingStart(ev);
        }
        break;

    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final int activePointerId = mActivePointerId;
            final int pointerIndex = ev.findPointerIndex(activePointerId);
            final float x = ev.getX(pointerIndex);
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
            final int deltaX = (int) (x - mDownMotionX);
            final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
            boolean isSignificantMove = Math.abs(deltaX) > pageWidth * SIGNIFICANT_MOVE_THRESHOLD;

            mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);

            boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING
                    && Math.abs(velocityX) > mFlingThresholdVelocity;

            // In the case that the page is moved far to one direction and then is flung
            // in the opposite direction, we use a threshold to determine whether we should
            // just return to the starting page, or if we should skip one further.
            boolean returnToOriginalPage = false;
            if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD
                    && Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
                returnToOriginalPage = true;
            }

            int finalPage;
            // We give flings precedence over large moves, which is why we short-circuit our
            // test for a large move if a fling has been registered. That is, a large
            // move to the left and fling to the right will register as a fling to the right.
            if (((isSignificantMove && deltaX > 0 && !isFling) || (isFling && velocityX > 0))
                    && mCurrentPage > 0) {
                finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
                snapToPageWithVelocity(finalPage, velocityX);
            } else if (((isSignificantMove && deltaX < 0 && !isFling) || (isFling && velocityX < 0))
                    && mCurrentPage < getChildCount() - 1) {
                finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
                snapToPageWithVelocity(finalPage, velocityX);
            } else {
                snapToDestination();
            }
        } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
            // at this point we have not moved beyond the touch slop
            // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
            // we can just page
            int nextPage = Math.max(0, mCurrentPage - 1);
            if (nextPage != mCurrentPage) {
                snapToPage(nextPage);
            } else {
                snapToDestination();
            }
        } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
            // at this point we have not moved beyond the touch slop
            // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
            // we can just page
            int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
            if (nextPage != mCurrentPage) {
                snapToPage(nextPage);
            } else {
                snapToDestination();
            }
        } else {
            onUnhandledTap(ev);
        }
        mTouchState = TOUCH_STATE_REST;
        mActivePointerId = INVALID_POINTER;
        releaseVelocityTracker();
        break;

    case MotionEvent.ACTION_CANCEL:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            snapToDestination();
        }
        mTouchState = TOUCH_STATE_REST;
        mActivePointerId = INVALID_POINTER;
        releaseVelocityTracker();
        break;

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

    return true;
}

From source file:net.osmand.plus.views.controls.DynamicListView.java

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

    if (singleTapDetector.onTouchEvent(event)) {
        if (tag != null) {
            tag.onClick();//from  ww w  . j av a2s.  c  om
        }
        touchEventsCancelled();
        return super.onTouchEvent(event);
    }

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        if (!mCellIsMobile && mHoverCell == null) {
            // Find the view that the user pressed their finger down on.
            View v = findViewAtPositionWithDragIconTag(getRootView(), (int) event.getRawX(),
                    (int) event.getRawY());

            // If the view contains a tag set to "DragIcon" class, it means that the user wants to
            // reorder the list item.
            if ((v != null) && (v.getTag() != null) && (v.getTag() instanceof DragIcon)) {
                mDownX = (int) event.getX();
                mDownY = (int) event.getY();
                mActivePointerId = event.getPointerId(0);
                mTotalOffset = 0;
                tag = (DragIcon) v.getTag();

                int position = pointToPosition(mDownX, mDownY);
                if (position != INVALID_POSITION) {
                    Object item = getAdapter().getItem(position);
                    if (mActiveItemsList == null || mActiveItemsList.contains(item)) {

                        int itemNum = position - getFirstVisiblePosition();
                        itemsSwapped = false;

                        View selectedView = getChildAt(itemNum);
                        mMobileItemId = getAdapter().getItemId(position);
                        mHoverCell = getAndAddHoverView(selectedView);
                        selectedView.setVisibility(INVISIBLE);

                        mCellIsMobile = true;
                        updateNeighborViewsForID(mMobileItemId);

                        if (dCallbacks != null) {
                            dCallbacks.onItemSwapping(position);
                        }
                    }
                }
            }
        }
        break;

    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER_ID) {
            break;
        }

        int pointerIndex = event.findPointerIndex(mActivePointerId);

        mLastEventY = (int) event.getY(pointerIndex);
        int deltaY = mLastEventY - mDownY;

        if (mCellIsMobile && mHoverCell != null) {
            mHoverCellCurrentBounds.offsetTo(mHoverCellOriginalBounds.left,
                    mHoverCellOriginalBounds.top + deltaY + mTotalOffset);
            mHoverCell.setBounds(mHoverCellCurrentBounds);
            invalidate();

            handleCellSwitch();

            mIsMobileScrolling = false;
            handleMobileCellScroll();

            return false;
        }
        break;

    case MotionEvent.ACTION_UP:
        touchEventsEnded();
        break;

    case MotionEvent.ACTION_CANCEL:
        touchEventsCancelled();
        break;

    case MotionEvent.ACTION_POINTER_UP:
        /* If a multitouch event took place and the original touch dictating
         * the movement of the hover cell has ended, then the dragging event
         * ends and the hover cell is animated to its corresponding position
         * in the listview. */
        pointerIndex = (event.getAction()
                & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        final int pointerId = event.getPointerId(pointerIndex);
        if (pointerId == mActivePointerId) {
            touchEventsEnded();
        }
        break;

    default:
        break;
    }

    return super.onTouchEvent(event);
}

From source file:net.simonvt.staggeredgridview.StaggeredGridView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    velocityTracker.addMovement(ev);/*  w  ww.  j a v  a  2  s  .co  m*/
    final int action = ev.getAction() & MotionEvent.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        if (tapReset != null) {
            removeCallbacks(tapReset);
            tapReset = null;
        }
        if (pendingTapCheck != null) {
            removeCallbacks(pendingTapCheck);
            pendingTapCheck = null;
        }

        velocityTracker.clear();
        scroller.abortAnimation();
        lastTouchY = ev.getY();
        lastTouchX = ev.getX();
        final int x = (int) ev.getX();
        activePointerId = ev.getPointerId(0);
        touchRemainderY = 0;
        motionPosition = getPositionAt(x, (int) lastTouchY);
        if (motionPosition != INVALID_POSITION && adapter != null && adapter.isEnabled(motionPosition)) {
            pendingTapCheck = new TapCheck();
            postDelayed(pendingTapCheck, ViewConfiguration.getTapTimeout());
            if (hasStableIds) {
                motionId = ((LayoutParams) getChildAt(motionPosition - firstPosition).getLayoutParams()).id;
            }
        }
        break;
    }

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

        if (Math.abs(dy) > touchSlop) {
            touchMode = TOUCH_MODE_DRAGGING;
        }

        if (touchMode == TOUCH_MODE_DRAGGING) {
            if (pendingTapCheck != null) {
                removeCallbacks(pendingTapCheck);
            }
            if (!selectorRect.isEmpty()) {
                selectorRect.setEmpty();
            }
            if (motionPosition != INVALID_POSITION) {
                final View child = getChildAt(motionPosition - firstPosition);
                if (child != null) {
                    child.setPressed(false);
                }
                setPressed(false);
                selector.setState(StateSet.NOTHING);
                motionPosition = INVALID_POSITION;
                motionId = -1L;
            }

            lastTouchY = y;
            lastTouchX = x;

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

    case MotionEvent.ACTION_CANCEL:
        touchMode = TOUCH_MODE_IDLE;

        if (motionPosition != INVALID_POSITION) {
            View child = getChildAt(motionPosition - firstPosition);
            child.setPressed(false);

            setPressed(false);
        }

        motionPosition = INVALID_POSITION;
        motionId = -1L;
        selectorRect.setEmpty();

        if (pendingTapCheck != null) {
            removeCallbacks(pendingTapCheck);
            pendingTapCheck = null;
        }
        if (tapReset != null) {
            removeCallbacks(tapReset);
            tapReset = null;
        }
        break;

    case MotionEvent.ACTION_UP: {
        velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
        final float velocity = velocityTracker.getYVelocity(activePointerId);

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

        if (Math.abs(velocity) > flingVelocity) { // TODO
            touchMode = TOUCH_MODE_FLINGING;
            scroller.fling(0, 0, 0, (int) velocity, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
            lastTouchY = 0;
            postInvalidateOnAnimation();

            if (motionPosition != INVALID_POSITION) {
                View child = getChildAt(motionPosition - firstPosition);
                if (child != null) {
                    child.setPressed(false);
                }

                setPressed(false);

                motionPosition = INVALID_POSITION;
                motionId = -1L;
                selectorRect.setEmpty();

                if (pendingTapCheck != null) {
                    removeCallbacks(pendingTapCheck);
                    pendingTapCheck = null;
                }
            }
        } else {
            if (touchMode != TOUCH_MODE_DRAGGING && motionPosition != INVALID_POSITION) {
                if (adapter != null && adapter.isEnabled(motionPosition)) {
                    new TapCheck().run();
                    tapReset = new TapReset();
                    postDelayed(tapReset, ViewConfiguration.getPressedStateDuration());
                } else {
                    motionPosition = INVALID_POSITION;
                    motionId = -1L;
                }
            }
            touchMode = TOUCH_MODE_IDLE;
        }
    }
        break;
    }
    return true;
}

From source file:com.doubleTwist.drawerlib.ADrawerLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // Here we actually handle the touch event (e.g. if the action is ACTION_MOVE,
    // scroll this container).
    // This method will only be called if the touch event was intercepted in
    // onInterceptTouchEvent

    if (DEBUG_TOUCH)
        Log.d(TAG, "onTouchEvent");

    if (!mDrawerState.mDraggingEnabled)
        return false;

    if (mVelocityTracker != null)
        mVelocityTracker.addMovement(ev);

    final int action = MotionEventCompat.getActionMasked(ev);

    if (!mIsScrolling) {
        if (DEBUG_TOUCH)
            Log.d(TAG, "NOT SCROLLING IS NOW SCROLLING");
        int drawer = preProcessScrollMotionEvent(ev);
        postProcessScrollMotionEvent(drawer, ev);
        if (action == MotionEvent.ACTION_UP) {
            if (mDrawerState.mScrollState == IDLE)
                processIdleUp(ev);/* w ww .  j  a v a 2s.  c  o  m*/
        }
        return true; // needs to be true so we keep getting these values
    }

    if (DEBUG_TOUCH)
        Log.d(TAG, "DOING THE TOUCH STUFF");

    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        mIsScrolling = false;
        mStartedTracking = false;
        snap();
        mVelocityTracker.clear();
    }

    if (action == MotionEvent.ACTION_MOVE) {
        int ptrIdx = ev.findPointerIndex(mDownEvent.mPointerId);
        if (DEBUG_TOUCH)
            Log.d(TAG, "ptrIdx: " + ptrIdx);
        if (ptrIdx >= ev.getPointerCount() || ptrIdx < 0)
            return true;

        if (mDrawerState.mActiveDrawer == LEFT_DRAWER || mDrawerState.mActiveDrawer == RIGHT_DRAWER) {
            mCurrentScrollX += mStartedTracking ? ev.getX(ptrIdx) - mLastMoveEvent.mCoords.x
                    : ev.getX(ptrIdx) - mDownEvent.mCoords.x;
            mCurrentScrollX = Math.max(mMinScrollX + mPeekSize.right, mCurrentScrollX);
            mCurrentScrollX = Math.min(mMaxScrollX - mPeekSize.left, mCurrentScrollX);
        } else if (mDrawerState.mActiveDrawer == TOP_DRAWER || mDrawerState.mActiveDrawer == BOTTOM_DRAWER) {
            mCurrentScrollY += mStartedTracking ? ev.getY(ptrIdx) - mLastMoveEvent.mCoords.y
                    : ev.getY(ptrIdx) - mDownEvent.mCoords.y;
            mCurrentScrollY = Math.max(mMinScrollY + mPeekSize.bottom, mCurrentScrollY);
            mCurrentScrollY = Math.min(mMaxScrollY - mPeekSize.top, mCurrentScrollY);
        }

        if (!mStartedTracking)
            mStartedTracking = true;
        mLastMoveEvent.mCoords.x = ev.getX(ptrIdx);
        mLastMoveEvent.mCoords.y = ev.getY(ptrIdx);

        // Log.d(TAG, "=======================");
        // Log.d(TAG, "mCurrentScrollX: "+mCurrentScrollX);
        // Log.d(TAG, "mCurrentScrollY: "+mCurrentScrollY);
        // Log.d(TAG, "mMinScrollY: "+mMinScrollY);
        // Log.d(TAG, "mMaxScrollY: "+mMaxScrollY);
        // Log.d(TAG, "mPeekSize.top: "+mPeekSize.top);
        // Log.d(TAG, "mPeekSize.bottom: "+mPeekSize.bottom);

        adjustScrollStates(mCurrentScrollX, mCurrentScrollY);
        notifyOffset();
    }

    return true;
}