Example usage for android.view MotionEvent getRawY

List of usage examples for android.view MotionEvent getRawY

Introduction

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

Prototype

public final float getRawY() 

Source Link

Document

Returns the original raw Y coordinate of this event.

Usage

From source file:com.android.contacts.quickcontact.QuickContactActivity.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
    }//from w w w . j  a  v  a  2s  .  co m
    return super.dispatchTouchEvent(ev);
}

From source file:cn.trinea.android.common.view.CycleViewPager.java

private boolean pointInRect(MotionEvent event, Rect rect) {
    final float x = event.getRawX();
    final float y = event.getRawY();
    Log.d(TAG, "pointInRect x = " + x + ", y = " + y + ", rect = " + rect);
    if (rect != null && mCurItem == mSpecTab) {
        return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom;
    }/*from   w  w  w .j a  va  2  s .  c  o  m*/
    return false;
}

From source file:io.bunnyblue.noticedog.app.lockscreen.ui.LockScreenWidget.java

void setupMoveHandlers() {
    this.rootView.setOnInterceptTouchListener(new OnTouchListener() {
        GestureDetector gestureDetector = new GestureDetector(LockScreenWidget.this.context,
                new OnGestureListener() {
                    public boolean onDown(MotionEvent motionEvent) {
                        return false;
                    }/*from  w  w w  .j  ava2s. c  om*/

                    public void onShowPress(MotionEvent motionEvent) {
                    }

                    public boolean onSingleTapUp(MotionEvent motionEvent) {
                        return false;
                    }

                    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float v,
                            float v2) {
                        return false;
                    }

                    public void onLongPress(MotionEvent motionEvent) {
                    }

                    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v,
                            float v2) {
                        if (v2 >= -1000.0f || LockScreenWidget.this.isPagerBeingDragged) {
                            return false;
                        }
                        LockScreenWidget.this.performAction(WidgetAction.actionClear);
                        return true;
                    }
                });

        public boolean onTouch(View view, MotionEvent motionEvent) {
            this.gestureDetector.onTouchEvent(motionEvent);
            boolean moved;
            switch (motionEvent.getAction()) {
            case 1:
            case 3:
                moved = false;
                if (LockScreenWidget.this.hasMoveStarted) {
                    moved = true;
                    LockScreenWidget.this.performAction(WidgetAction.actionStopMove);
                }
                LockScreenWidget.this.isTouchDown = false;
                LockScreenWidget.this.hasMoveStarted = false;
                if (moved) {
                    return true;
                }
                return false;
            case 2:
                moved = false;
                if (!LockScreenWidget.this.isTouchDown || LockScreenWidget.this.isPagerBeingDragged) {
                    return false;
                }
                if (LockScreenWidget.this.hasMoveStarted && LockScreenWidget.this.screenshotView != null) {
                    float newY = LockScreenWidget.this.screenshotView.getY()
                            + (motionEvent.getRawY() - LockScreenWidget.this.lastY);
                    int maxY = LockScreenWidget.this.fullscreenView.getHeight()
                            - LockScreenWidget.this.screenshotView.getHeight();
                    if (newY < 0.0f) {
                        Log.d(LockScreenWidget.TAG,
                                "Hit top edge with newY = " + newY + "  |   setting newY to 0");
                        newY = 0.0f;
                    } else if (newY > ((float) maxY)) {
                        Log.d(LockScreenWidget.TAG,
                                "Hit bottom edge with newY = " + newY + "  | setting newY " + maxY);
                        newY = (float) maxY;
                    }
                    LockScreenWidget.this.screenshotView.setY(newY);
                    moved = true;
                }
                LockScreenWidget.this.lastY = motionEvent.getRawY();
                if (moved) {
                    return true;
                }
                return false;
            default:
                return false;
            }
        }
    });
}

From source file:com.bolaa.medical.view.pulltorefreshgrid.StaggeredGridView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);/*from   w  w w  . ja v a2  s .  co m*/
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        LogUtil.d("--------------onInterceptTouchEvent ACTION_DOWN---------");
        mFirstDownY = ev.getRawY();

        mVelocityTracker.clear();
        mScroller.abortAnimation();
        mLastTouchY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderY = 0;
        if (mTouchMode == TOUCH_MODE_FLINGING) {
            // Catch!
            mTouchMode = TOUCH_MODE_DRAGGING;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL, getFirstVisiblePosition(),
                    getLastPosition());
            return true;
        }
        break;

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

        if (Math.abs(dy) > mTouchSlop) {
            mTouchMode = TOUCH_MODE_DRAGGING;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL, getFirstVisiblePosition(),
                    getLastPosition());
            return true;
        }
    }
    }

    return false;
}

From source file:cz.metaverse.android.bilingualreader.helper.BookPanelOnTouchListener.java

/**
 * The entry method for any touch-related event.
 * @param view The WebView where the swipe took place
 * @param event The MotionEvent of the swipe
 *//*from   www .  ja va2  s  . c  o  m*/
@Override
public boolean onTouch(View view, MotionEvent event) {
    // Provide data to the GestureDetector.
    gestureDetector.onTouchEvent(event);

    /*
     * Handle Double Tap Swipe in real time.
     *  This handling isn't in onDoubleTapEvent() because that method has a limit how many times
     *  it gets called after which it isn't called at all until the user lifts the finger in which
     *  case it gets called one last time. That is not workable for on-the-fly resizing of panels.
     */
    if (isDoubleTapSwipe) {
        float absDiffX = Math.abs(doubleTapOriginX - event.getX());
        float absDiffY = Math.abs(doubleTapOriginY - event.getY());

        // If the swipe was over predefined threshold value high and it was higher than wider,
        // or if the swipe was over predefined threshold value wide and it was wider than higher.
        if (doubleTapSwipeEscapedBounds ||
        // Bounds for PORTRAIT orientation.
                (absDiffY > threshold_swipe_up_or_down_change_panel_size_px && absDiffY > absDiffX
                        && doubleTapSwipe_orientation == Configuration.ORIENTATION_PORTRAIT)
                ||
                // Bounds for LANDSCAPE orientation.
                (absDiffX > threshold_swipe_left_right_change_panel_size_px && absDiffX > absDiffY
                        && doubleTapSwipe_orientation != Configuration.ORIENTATION_PORTRAIT)) {

            if (!doubleTapSwipeEscapedBounds) {
                // This is the first time doubleTapSwipe escaped it's bounds
                // - we're officially in the set-panels-size mode.
                doubleTapSwipeEscapedBounds = true;

                // Find out and save the relevant dimensions of our view/display
                Window window = activity.getWindow();

                if (doubleTapSwipe_orientation == Configuration.ORIENTATION_PORTRAIT) {
                    doubleTapSwipe_contentStartsAtHeight = window.findViewById(Window.ID_ANDROID_CONTENT)
                            .getTop();
                    doubleTapSwipe_viewHeight = window.getDecorView().getHeight();
                } else {
                    doubleTapSwipe_contentStartsAtHeight = window.findViewById(Window.ID_ANDROID_CONTENT)
                            .getLeft();
                    doubleTapSwipe_viewHeight = window.getDecorView().getWidth();
                }
            }

            // Compute the panels weight
            float useCoordinate = (doubleTapSwipe_orientation == Configuration.ORIENTATION_PORTRAIT)
                    ? event.getRawY()
                    : event.getRawX();

            newPanelsWeight = (useCoordinate - doubleTapSwipe_contentStartsAtHeight)
                    / (doubleTapSwipe_viewHeight - doubleTapSwipe_contentStartsAtHeight);

            // If the weight is close to 0.5, let it stick to it.
            if (Math.abs(0.5 - newPanelsWeight) < PANEL_RESIZE_SNAP_WEIGHT_AROUND_HALF) {
                newPanelsWeight = 0.5f;
            }

            // Assure that the weight is between the allowed minimum and maximum.
            newPanelsWeight = Func.minMaxRange(Func.PANEL_WEIGHT_MIN, newPanelsWeight, Func.PANEL_WEIGHT_MAX);

            // Change relative panel size on the fly.
            governor.changePanelsWeight(newPanelsWeight);

            //Log.v(LOG, "[" + panelPosition + "] doubleTapSwipe " + newPanelsWeight);
            //+ " = (" + event.getRawY() + " - " + contentViewTop + ") / " + (height - contentViewTop));
        }
    }

    /*
     * Handle ACTION_UP event for scrolling, because onScroll (almost?) never gets
     * called with the last MotionEvent.ACTION_UP.
     * Don't forget to mirror any changes made here in the onScroll() method as well, to be safe.
     */
    if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_UP) {
        Log.d(LOG, "[" + panelPosition + "] OnTouchListener --> onTouch ACTION_UP - scrollY: "
                + webView.getScrollY());

        // Only if it's not DoubleTapSwipe, that is handled separately.
        if (!isDoubleTapSwipe) {
            // Evaluate if the scroll that has just ended constitutes some gesture.
            handleScrollEnd(event);
        }
    }

    // Modify the event so that it is passed on in a state where the X coordinate hasn't changed at all.
    // This prevents the "over scroll glow effect" on pages that have some (hidden) horizontal scroll.
    //  -- Disabled because it causes the wrong scroll position to be loaded after opening a page
    //     sometimes (but not every time), for some unknown reason.
    //event.setLocation(touchOriginX, event.getY());

    view.performClick(); // Android system mandates we pass the baton to the onClick listener now.

    return view.onTouchEvent(event);
}

From source file:co.vn.e_alarm.customwiget.SlidingLayer.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    if (!mEnabled) {
        return false;
    }//  w w  w. java2s. c  o m

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

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        /*
         * Return event touch to children view.
         */
        mIsDragging = false;
        mIsUnableToDrag = false;

        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        return false;
    }

    if (action != MotionEvent.ACTION_DOWN) {
        if (mIsDragging) {
            return true;
        } else if (mIsUnableToDrag) {
            return false;
        }
    }

    switch (action) {
    case MotionEvent.ACTION_MOVE:
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            break;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        if (pointerIndex == -1) {
            mActivePointerId = INVALID_POINTER;
            break;
        }

        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float dx = x - mLastX;
        final float xDiff = Math.abs(dx);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float dy = y - mLastY;
        final float yDiff = Math.abs(y - mLastY);

        if (xDiff > mTouchSlop && xDiff > yDiff && allowDragingX(dx, mInitialX)) {
            // mIsDragging = true;
            mLastX = x;
            setDrawingCacheEnabled(true);
        } else if (yDiff > mTouchSlop && yDiff > xDiff && allowDragingY(dy, mInitialY)) {
            // mIsDragging = true;
            mLastY = y;
            setDrawingCacheEnabled(true);
        }
        float matY = (float) Math.abs(corY - ev.getRawY());
        float matX = (float) Math.abs(corX - ev.getRawX());
        if (matY - 100 > 0) {
            mIsDragging = true;
            setDrawingCacheEnabled(true);
            // Dang di chuyen len
        } else if (matX - 100 > 0) {
            mIsDragging = false;
        }
        if (matY == 0) {
            setDrawingCacheEnabled(false);
        }

        /*   }else{
        mIsDragging = false;
        setDrawingCacheEnabled(true);
           }*/
        /* }else if(corX +150 > ev.getRawX()){
        mIsDragging = false;
        setDrawingCacheEnabled(true);
        }else if(corX +150 < ev.getRawX()){
        mIsDragging = false;
        setDrawingCacheEnabled(true);
        }*/
        // double corY = ev.getY();

        break;

    case MotionEvent.ACTION_DOWN:
        corX = ev.getRawX();
        corY = ev.getRawY();
        mActivePointerId = ev.getAction() & (Build.VERSION.SDK_INT >= 8 ? MotionEvent.ACTION_POINTER_INDEX_MASK
                : MotionEventCompat.ACTION_POINTER_INDEX_MASK);
        mLastX = mInitialX = MotionEventCompat.getX(ev, mActivePointerId);
        mLastY = mInitialY = MotionEventCompat.getY(ev, mActivePointerId);
        if (allowSlidingFromHereX(ev, mInitialX)) {
            mIsDragging = false;
            mIsUnableToDrag = false;
            // If nobody else got the focus we use it to close the layer
            return super.onInterceptTouchEvent(ev);
        } else if (allowSlidingFromHereY(ev, mInitialY)) {
            mIsDragging = false;
            mIsUnableToDrag = false;
            // If nobody else got the focus we use it to close the layer
            return super.onInterceptTouchEvent(ev);
        } else {
            mIsUnableToDrag = true;
        }
        break;
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

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

    return mIsDragging;
}

From source file:com.sun.toy.ux.widget.CustomViewPager.java

License:asdf

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

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    Log.i("asdfasdf", "sdlfjsd : " + ev.getY());
    if (mIsUnableToDrag) {
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float y = ev.getRawY();
        final float yDiff = y - mLastMotionRawY;
        Log.i("viewpager.drag.point", ev.getPointerCount() + " || delta : " + yDiff);
        Log.e("viewpager.drag.point", "mInitialMotionY : " + mInitialMotionY + " | " + y);
        deliver.onDragY(yDiff);
    }
    // Always take care of the touch gesture being complete.
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        // Release the drag.
        if (DEBUG)
            Log.v(TAG, "Intercept done!");
        mIsBeingDragged = false;
        mIsUnableToDrag = false;
        mActivePointerId = INVALID_POINTER;
        mVelocityTracker.computeCurrentVelocity(1000);
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        deliver.onDragEnd();
        return false;
    }

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

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

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

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float dx = x - mLastMotionX;
        final float xDiff = Math.abs(dx);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float yDiff = Math.abs(y - mInitialMotionY);
        mLastMotionRawY = ev.getRawY();
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
        //
        //            if (dx != 0 && !isGutterDrag(mLastMotionX, dx) &&
        //                  canScroll(this, false, (int) dx, (int) x, (int) y)) {
        //               // Nested view has scrollable area under this point. Let it be handled there.
        //               mLastMotionX = x;
        //               mLastMotionY = y;
        //               mIsUnableToDrag = true;
        //               return false;
        //            }
        if (xDiff > mTouchSlop /*&& xDiff * 0.5f > yDiff*/) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            requestParentDisallowInterceptTouchEvent(true);
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionX = dx > 0 ? mInitialMotionX + mTouchSlop : mInitialMotionX - mTouchSlop;
            mLastMotionY = y;
            setScrollingCacheEnabled(true);
        } else if (yDiff > mTouchSlop) {
            // The finger has moved enough in the vertical
            // direction to be counted as a drag...  abort
            // any attempt to drag horizontally, to work correctly
            // with children that have scrolling containers.
            if (DEBUG)
                Log.v(TAG, "Starting unable to drag!");
            Log.d("viewpager", "drag y");
            //                    deliver.onDragY(yDiff);
            mIsUnableToDrag = true;
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            if (performDrag(x)) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
        }
        //                if (mIsUnableToDrag) {
        //                    deliver.onDragY(yDiff);
        //                }
        break;
    }

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

        mScroller.computeScrollOffset();
        if (mScrollState == SCROLL_STATE_SETTLING
                && Math.abs(mScroller.getFinalX() - mScroller.getCurrX()) > mCloseEnough) {
            // Let the user 'catch' the pager as it animates.
            mScroller.abortAnimation();
            mPopulatePending = false;
            populate();
            mIsBeingDragged = true;
            requestParentDisallowInterceptTouchEvent(true);
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll(false);
            mIsBeingDragged = false;
        }

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

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

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

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

From source file:se.oort.clockify.widget.sgv.StaggeredGridView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);//  ww  w .j  av  a 2 s .  c  om
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mOffsetToAbsoluteX = (int) (ev.getRawX() - ev.getX());
        mOffsetToAbsoluteY = (int) (ev.getRawY() - ev.getY());

        // Per bug 7377413, event.getX() and getY() returns rawX and rawY when accessed in
        // dispatchDragEvent, so since an action down is required before a drag can be
        // initiated, initialize mTouchDownForDragStartX/Y here for the most accurate value.
        mTouchDownForDragStartX = (int) ev.getX();
        mTouchDownForDragStartY = (int) ev.getY();

        mVelocityTracker.clear();
        mScroller.abortAnimation();
        mLastTouchY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderY = 0;
        if (mTouchMode == TOUCH_MODE_FLINGING) {
            // Catch!
            mTouchMode = TOUCH_MODE_DRAGGING;
            return true;
        }
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(LOG_TAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did StaggeredGridView receive an inconsistent " + "event stream?");
            return false;
        }
        final float y = MotionEventCompat.getY(ev, index);
        final float dy = y - mLastTouchY + mTouchRemainderY;
        final int deltaY = (int) dy;
        mTouchRemainderY = dy - deltaY;

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

    return false;
}

From source file:app.umitems.greenclock.widget.sgv.StaggeredGridView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);//from  ww  w.  ja  v a2 s .c om
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mOffsetToAbsoluteX = (int) (ev.getRawX() - ev.getX());
        mOffsetToAbsoluteY = (int) (ev.getRawY() - ev.getY());

        // Per bug 7377413, event.getX() and getY() returns rawX and rawY when accessed in
        // dispatchDragEvent, so since an action down is required before a drag can be
        // initiated, initialize mTouchDownForDragStartX/Y here for the most accurate value.
        mTouchDownForDragStartX = (int) ev.getX();
        mTouchDownForDragStartY = (int) ev.getY();

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

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

    return false;
}

From source file:com.bolaa.medical.view.pulltorefreshgrid.StaggeredGridView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);//from  ww w .  ja v  a2 s  .com
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

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

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        LogUtil.d("----------onTouch ACTION_DOWN-------------");
        isOnTouch = true;

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

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

            mBeginClick = true;

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

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

        mMotionPosition = motionPosition;
        invalidate();
        break;

    case MotionEvent.ACTION_MOVE:
        //LogUtil.d("----------onTouch ACTION_MOVE-------------");
        float currentY = ev.getRawY();
        if (mMoveTouchListener != null) {
            if (currentY - mFirstDownY >= 30) {
                mMoveTouchListener.onMoveDown();
            } else if (mFirstDownY - currentY >= 30) {
                mMoveTouchListener.onMoveUp();
            }
        }

        mFirstDownY = currentY;

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

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

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

            if (!trackMotionScroll(deltaY, true)) {
                // Break fling velocity if we impacted an edge.
                mVelocityTracker.clear();
            }
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL, getFirstVisiblePosition(),
                    getLastPosition());
        }

        updateSelectorState();

        if (mFlingListener != null) {
            mFlingListener.onTouchFling();
        }
        break;

    case MotionEvent.ACTION_CANCEL:
        // LogUtil.d("----------onTouch ACTION_CANCEL-------------");
        isOnTouch = false;

        mTouchMode = TOUCH_MODE_IDLE;
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE, getFirstVisiblePosition(),
                getLastPosition());
        updateSelectorState();
        setPressed(false);
        View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
        if (motionView != null) {
            motionView.setPressed(false);
        }
        final Handler handler = getHandler();
        if (handler != null) {
            handler.removeCallbacks(mPendingCheckForLongPress);
        }

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

        mTouchMode = TOUCH_MODE_IDLE;
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE, getFirstVisiblePosition(),
                getLastPosition());

        break;

    case MotionEvent.ACTION_UP: {

        //LogUtil.d("----------onTouch ACTION_UP-------------");
        // LogUtil.d("TAG---------------onTouch ACTION_UP finish-----------" + mScroller.isFinished());
        isOnTouch = false;
        mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        final float velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
        final int prevTouchMode = mTouchMode;

        if (Math.abs(velocity) > mFlingVelocity) { // TODO
            mTouchMode = TOUCH_MODE_FLINGING;
            mIsFling = true;
            mScroller.fling(0, 0, 0, (int) velocity, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
            mLastTouchY = 0;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING, getFirstVisiblePosition(),
                    getLastPosition());
            invalidate();
        } else {
            mIsFling = true;
            mTouchMode = TOUCH_MODE_IDLE;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE, getFirstVisiblePosition(),
                    getLastPosition());
        }

        if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
            // TODO : handle
            mTouchMode = TOUCH_MODE_TAP;
        } else {
            mTouchMode = TOUCH_MODE_REST;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE, getFirstVisiblePosition(),
                    getLastPosition());
        }

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

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

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

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

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

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

                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE, getFirstVisiblePosition(),
                                getLastPosition());
                    }
                    return true;
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

            mTouchMode = TOUCH_MODE_REST;
            reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE, getFirstVisiblePosition(),
                    getLastPosition());
        }

        mBeginClick = false;

        updateSelectorState();
    }
        break;
    }

    return true;
}