Example usage for android.view MotionEvent getY

List of usage examples for android.view MotionEvent getY

Introduction

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

Prototype

public final float getY() 

Source Link

Document

#getY(int) for the first pointer index (may be an arbitrary pointer identifier).

Usage

From source file:com.apptentive.android.sdk.view.ApptentiveNestedScrollView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    initVelocityTrackerIfNotExists();/*from w  ww.  j  a v a  2  s  .  c  om*/

    MotionEvent vtev = MotionEvent.obtain(ev);

    final int actionMasked = MotionEventCompat.getActionMasked(ev);

    if (actionMasked == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;
    }
    vtev.offsetLocation(0, mNestedYOffset);

    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN: {
        if (getChildCount() == 0) {
            return false;
        }
        if ((mIsBeingDragged = !mScroller.isFinished())) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
        }

        /*
         * 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
        mLastMotionY = (int) ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (activePointerIndex == -1) {
            Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
            break;
        }

        final int y = (int) MotionEventCompat.getY(ev, activePointerIndex);
        int deltaY = mLastMotionY - y;
        if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
            deltaY -= mScrollConsumed[1];
            vtev.offsetLocation(0, mScrollOffset[1]);
            mNestedYOffset += mScrollOffset[1];
        }
        if (!mIsBeingDragged && Math.abs(deltaY) > mTouchSlop) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
            mIsBeingDragged = true;
            if (deltaY > 0) {
                deltaY -= mTouchSlop;
            } else {
                deltaY += mTouchSlop;
            }
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            mLastMotionY = y - mScrollOffset[1];

            final int oldY = getScrollY();
            final int range = getScrollRange();
            final int overscrollMode = ViewCompat.getOverScrollMode(this);
            boolean canOverscroll = overscrollMode == ViewCompat.OVER_SCROLL_ALWAYS
                    || (overscrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && range > 0);

            // Calling overScrollByCompat will call onOverScrolled, which
            // calls onScrollChanged if applicable.
            if (overScrollByCompat(0, deltaY, 0, getScrollY(), 0, range, 0, 0, true)
                    && !hasNestedScrollingParent()) {
                // Break our velocity if we hit a scroll barrier.
                mVelocityTracker.clear();
            }

            final int scrolledDeltaY = getScrollY() - oldY;
            final int unconsumedY = deltaY - scrolledDeltaY;
            if (dispatchNestedScroll(0, scrolledDeltaY, 0, unconsumedY, mScrollOffset)) {
                mLastMotionY -= mScrollOffset[1];
                vtev.offsetLocation(0, mScrollOffset[1]);
                mNestedYOffset += mScrollOffset[1];
            } else if (canOverscroll) {
                ensureGlows();
                final int pulledToY = oldY + deltaY;
                if (pulledToY < 0) {
                    mEdgeGlowTop.onPull((float) deltaY / getHeight(),
                            MotionEventCompat.getX(ev, activePointerIndex) / getWidth());
                    if (!mEdgeGlowBottom.isFinished()) {
                        mEdgeGlowBottom.onRelease();
                    }
                } else if (pulledToY > range) {
                    mEdgeGlowBottom.onPull((float) deltaY / getHeight(),
                            1.f - MotionEventCompat.getX(ev, activePointerIndex) / getWidth());
                    if (!mEdgeGlowTop.isFinished()) {
                        mEdgeGlowTop.onRelease();
                    }
                }
                if (mEdgeGlowTop != null && (!mEdgeGlowTop.isFinished() || !mEdgeGlowBottom.isFinished())) {
                    ViewCompat.postInvalidateOnAnimation(this);
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);

            if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
                flingWithNestedDispatch(-initialVelocity);
            } else if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0, getScrollRange())) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
        }
        mActivePointerId = INVALID_POINTER;
        endDrag();
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged && getChildCount() > 0) {
            if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0, getScrollRange())) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
        }
        mActivePointerId = INVALID_POINTER;
        endDrag();
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionY = (int) MotionEventCompat.getY(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionY = (int) MotionEventCompat.getY(ev,
                MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(vtev);
    }
    vtev.recycle();
    return true;
}

From source file:com.android.ex.chips.RecipientEditTextView.java

/**
 * Monitor touch events in the RecipientEditTextView. If the view does not have focus, any tap on the view will just
 * focus the view. If the view has focus, determine if the touch target is a recipient chip. If it is and the chip
 * is not selected, select it and clear any other selected chips. If it isn't, then select that chip.
 *//*from ww  w  .  j  a va  2  s .  c  o m*/
@Override
public boolean onTouchEvent(final MotionEvent event) {
    if (!isFocused())
        // Ignore any chip taps until this view is focused.
        return super.onTouchEvent(event);
    android.util.Log.d("AppLog", "scroll:" + getScrollY());
    boolean handled = super.onTouchEvent(event);
    final int action = event.getAction();
    boolean chipWasSelected = false;
    if (mSelectedChip == null)
        mGestureDetector.onTouchEvent(event);
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mIsScrolling = false;
        mStartTouchY = (int) event.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if (Math.abs(event.getY() - mStartTouchY) > SCROLLING_BOUNDS_THRESHOLD_IN_PIXELS)
            mIsScrolling = true;
        break;
    case MotionEvent.ACTION_UP:
        if (mIsScrolling) {
            mIsScrolling = false;
            break;
        }
        if (mCopyAddress == null) {
            final float x = event.getX();
            final float y = event.getY();
            final int offset = putOffsetInRange(x, y);
            final DrawableRecipientChip currentChip = findChip(offset);
            if (currentChip != null) {
                if (action == MotionEvent.ACTION_UP)
                    if (mSelectedChip != null && mSelectedChip != currentChip) {
                        clearSelectedChip();
                        mSelectedChip = selectChip(currentChip);
                    } else if (mSelectedChip == null) {
                        setSelection(getText().length());
                        commitDefault();
                        mSelectedChip = selectChip(currentChip);
                    } else
                        onClick(mSelectedChip, offset, x, y);
                chipWasSelected = true;
                handled = true;
            } else if (mSelectedChip != null && shouldShowEditableText(mSelectedChip))
                chipWasSelected = true;
        }
        if (!chipWasSelected)
            clearSelectedChip();
        break;
    }
    return handled;
}

From source file:com.app.afteryou.ui.staggered.StaggeredGridView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }/*  w  w  w. ja v a2s .c  o m*/
    mVelocityTracker.addMovement(ev);
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

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

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mVelocityTracker.clear();
        mScroller.abortAnimation();
        mLastTouchY = ev.getY();
        mLastTouchX = ev.getX();
        motionPosition = pointToPosition((int) mLastTouchX, (int) mLastTouchY);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderY = 0;

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

            mBeginClick = true;

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

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

        mMotionPosition = motionPosition;
        invalidate();
        break;

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

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

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

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

        updateSelectorState();
    }
        break;

    case MotionEvent.ACTION_CANCEL:
        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();
        }

        setTouchMode(TOUCH_MODE_IDLE);
        break;

    case MotionEvent.ACTION_UP: {
        mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        final float velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
        final int prevTouchMode = mTouchMode;
        if (Math.abs(velocity) > mFlingVelocity) { // TODO
            setTouchMode(TOUCH_MODE_FLINGING);
            mScroller.fling(0, 0, 0, (int) velocity, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
            mLastTouchY = 0;
            ViewCompat.postInvalidateOnAnimation(this);
        } else {
            setTouchMode(TOUCH_MODE_IDLE);
        }

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

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

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

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

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

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

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

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

            mTouchMode = TOUCH_MODE_REST;
        }
        mBeginClick = false;
        updateSelectorState();
    }
        break;
    }
    return true;
}

From source file:com.android.backups.BackupStaggeredGridView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);//from   w  ww . ja  v  a 2 s  .c o m
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mVelocityTracker.clear();
        mScroller.abortAnimation();
        mLastTouchY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderY = 0;
        if (mTouchMode == TOUCH_MODE_FLINGING) {
            // Catch!
            mTouchMode = TOUCH_MODE_DRAGGING;
            setTouchMode(mTouchMode);
            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;
            setTouchMode(mTouchMode);
            return true;
        }
    }
    }

    return false;
}

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

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);//from   w ww  .  ja  va2 s  . c  om
    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;
}

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

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);/*from   www .  j a  va2s  . c  o  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:com.acbelter.scheduleview.ScheduleView.java

private void init(Context context) {
    if (!isInEditMode()) {
        mOverScroller = new OverScroller(context);
        mGestureListener = new GestureDetector.SimpleOnGestureListener() {
            @Override//w  ww.  j av  a  2s  .co m
            public boolean onDown(MotionEvent e) {
                if (DEBUG) {
                    Log.d(TAG, "onDown() y=" + mListY);
                }

                releaseEdgeEffects();
                mOverScroller.forceFinished(true);
                return true;
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if (DEBUG) {
                    Log.d(TAG, "onFling() y=" + mListY);
                }

                // Fling isn't needed
                if (mDeltaHeight < 0) {
                    return true;
                }

                mScrollDirection = velocityY > 0 ? 1 : -1;
                mOverScroller.fling(0, mListY, 0, (int) velocityY, 0, 0, -mDeltaHeight, 0);
                if (!awakenScrollBars()) {
                    ViewCompat.postInvalidateOnAnimation(ScheduleView.this);
                }
                return true;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                for (int i = 0; i < getChildCount(); i++) {
                    getChildAt(i).setPressed(false);
                }

                mListY -= (int) distanceY;
                recalculateOffset();

                positionItemViews();

                if (mListY == 0) {
                    mTopEdgeEffect.onPull(distanceY / (float) getHeight());
                    mTopEdgeEffectActive = true;
                }
                if (mListY == -mDeltaHeight) {
                    mBottomEdgeEffect.onPull(distanceY / (float) getHeight());
                    mBottomEdgeEffectActive = true;
                }

                if (!awakenScrollBars()) {
                    invalidate();
                }

                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                if (DEBUG) {
                    Log.d(TAG, "onLongPress() y=" + mListY);
                }

                View child;
                for (int i = 0; i < getChildCount(); i++) {
                    child = getChildAt(i);
                    child.getHitRect(mClickedViewBounds);
                    if (mClickedViewBounds.contains((int) e.getX(), (int) e.getY())) {
                        if (!mIsActionMode) {
                            mActionMode = startActionMode(mActionModeCallback);
                            mIsActionMode = true;
                        }

                        if (!child.isSelected()) {
                            mSelectedIds.add(mAdapter.getItemId(i));
                            child.setSelected(true);
                        } else {
                            mSelectedIds.remove(mAdapter.getItemId(i));
                            child.setSelected(false);
                        }

                        if (mSelectedIds.isEmpty()) {
                            finishActionMode();
                        }

                        invalidate();
                        return;
                    }
                }
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                if (DEBUG) {
                    Log.d(TAG, "onSingleTapConfirmed() y=" + mListY);
                }

                View child;
                for (int i = 0; i < getChildCount(); i++) {
                    child = getChildAt(i);
                    child.getHitRect(mClickedViewBounds);
                    if (mClickedViewBounds.contains((int) e.getX(), (int) e.getY())) {
                        if (!mIsActionMode) {
                            OnItemClickListener callback = getOnItemClickListener();

                            if (callback != null) {
                                callback.onItemClick(ScheduleView.this, child, i, mAdapter.getItemId(i));
                            }
                        } else {
                            if (!child.isSelected()) {
                                mSelectedIds.add(mAdapter.getItemId(i));
                                child.setSelected(true);
                            } else {
                                mSelectedIds.remove(mAdapter.getItemId(i));
                                child.setSelected(false);
                            }

                            if (mSelectedIds.isEmpty()) {
                                finishActionMode();
                            }

                            invalidate();
                        }
                        break;
                    }
                }
                return true;
            }
        };

        mGestureDetector = new GestureDetector(context, mGestureListener);
    }
}

From source file:com.bulletnoid.android.widget.StaggeredGridView.StaggeredGridView3.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    int action = ev.getAction();
    View v;//from   ww w . j  a va  2s  .  c o  m

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        int touchMode = mTouchMode;

        final int x = (int) ev.getX();
        final int y = (int) ev.getY();
        mActivePointerId = ev.getPointerId(0);

        int motionPosition = findMotionRow(y);
        if (touchMode != TOUCH_MODE_FLINGING && motionPosition >= 0) {
            // User clicked on an actual view (and was not stopping a fling).
            // Remember where the motion event started
            v = getChildAt(motionPosition - mFirstPosition);
            //mMotionViewOriginalTop=v.getTop();
            mLastTouchX = x;
            mLastTouchY = y;
            mMotionPosition = motionPosition;
            mTouchMode = TOUCH_MODE_DOWN;
            clearScrollingCache();
        }
        mTouchRemainderY = Integer.MIN_VALUE;
        if (touchMode == TOUCH_MODE_FLINGING) {
            return true;
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final int y = (int) ev.getY(pointerIndex);
            if (startScrollIfNeeded((int) (y - mLastTouchX))) {
                return true;
            }
            break;
        }
        break;
    }

    case MotionEvent.ACTION_UP: {
        mTouchMode = TOUCH_MODE_REST;
        mActivePointerId = INVALID_POINTER;
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
        break;
    }

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

    return false;
}

From source file:com.cssweb.android.view.KlineView.java

public void touchesMoved(MotionEvent event) {
    //Log.i("========currentPositionX==========", currentPositionX+">>>>>>>>>>");
    //Log.i("========currentPositionY==========", currentPositionY+">>>>>>>>>>");
    currentPositionX = event.getX();//  ww w  .  j a  v a  2 s  .  com
    currentPositionY = event.getY();
    float deltaX = startPositionX - currentPositionX;
    float deltaY = startPositionY - currentPositionY;
    //Log.i("========Math.abs(delta)==========", Math.abs(deltaX)+">>>>>>>>>>"+Math.abs(deltaY));
    if (Math.abs(deltaX) < 8 && Math.abs(deltaY) < 8) {
        isTouchMoved = false;
    } else {
        isTouchMoved = true;
    }
    if (quoteData == null)
        return;
    int mouseX = (int) event.getX();
    int mouseY = (int) event.getY();
    if (mouseX <= klineX || mouseY <= axisLabelHeight || mouseY >= height - axisLabelHeight - zbmenuHeight)
        return;

    if (isTrackStatus) {//??
        isSingleMoved = false;//???
        double sep = (shapeWidth + spaceWidth);
        visualPos = (int) ((mouseX - klineX) / sep + 1);
        if (visualPos >= this.actualDataLen - actualPos) {
            visualPos = this.actualDataLen - actualPos;
        } else if (visualPos < 1)
            visualPos = 1;
        int idx = actualPos + visualPos - 1;

        if (mouseX > klineX) {
            trackLineV = (int) (visualPos * (spaceWidth + shapeWidth) - shapeWidth / 2);
        }

        if (idx < actualDataLen) {
            isTrackNumber = idx;
        } else {
            isTrackNumber = this.actualDataLen - 1;
        }
        //         Log.i("@@@@@@@@@@isTrackNumber@@@@@@@@@@", isTrackNumber+">>>>>>>>>");
        //         Log.i("@@@@@@@@@@idx@@@@@@@@@@", idx+">>>>>>>>>");
        //         Log.i("@@@@@@@@@@actualPos@@@@@@@@@@", actualPos+">>>>>>>>>");
        //         Log.i("@@@@@@@@@@visualPos@@@@@@@@@@", visualPos+">>>>>>>>>");
        this.invalidate();
    } else {//K?
        isSingleMoved = true;
        if (deltaX < -50 && deltaY < 10) {
            moveRight(1);
        } else if (deltaX > 50 && deltaY < 10) {
            moveLeft(1);
        }
    }
}