Example usage for android.view ViewConfiguration getTapTimeout

List of usage examples for android.view ViewConfiguration getTapTimeout

Introduction

In this page you can find the example usage for android.view ViewConfiguration getTapTimeout.

Prototype

public static int getTapTimeout() 

Source Link

Usage

From source file:com.bw.luzz.monkeyapplication.View.DateTimePicker.time.RadialPickerLayout.java

public RadialPickerLayout(Context context, AttributeSet attrs) {
    super(context, attrs);

    setOnTouchListener(this);
    ViewConfiguration vc = ViewConfiguration.get(context);
    TOUCH_SLOP = vc.getScaledTouchSlop();
    TAP_TIMEOUT = ViewConfiguration.getTapTimeout();
    mDoingMove = false;//w  w  w.jav  a  2 s . co  m

    mCircleView = new com.bw.luzz.monkeyapplication.View.DateTimePicker.time.CircleView(context);
    addView(mCircleView);

    mAmPmCirclesView = new AmPmCirclesView(context);
    addView(mAmPmCirclesView);

    mHourRadialSelectorView = new RadialSelectorView(context);
    addView(mHourRadialSelectorView);
    mMinuteRadialSelectorView = new RadialSelectorView(context);
    addView(mMinuteRadialSelectorView);
    mSecondRadialSelectorView = new RadialSelectorView(context);
    addView(mSecondRadialSelectorView);

    mHourRadialTextsView = new com.bw.luzz.monkeyapplication.View.DateTimePicker.time.RadialTextsView(context);
    addView(mHourRadialTextsView);
    mMinuteRadialTextsView = new com.bw.luzz.monkeyapplication.View.DateTimePicker.time.RadialTextsView(
            context);
    addView(mMinuteRadialTextsView);
    mSecondRadialTextsView = new com.bw.luzz.monkeyapplication.View.DateTimePicker.time.RadialTextsView(
            context);
    addView(mSecondRadialTextsView);

    // Prepare mapping to snap touchable degrees to selectable degrees.
    preparePrefer30sMap();

    mLastValueSelected = null;

    mInputEnabled = true;

    mGrayBox = new View(context);
    mGrayBox.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    mGrayBox.setBackgroundColor(ContextCompat.getColor(context, R.color.mdtp_transparent_black));
    mGrayBox.setVisibility(View.INVISIBLE);
    addView(mGrayBox);

    mAccessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);

    mTimeInitialized = false;
}

From source file:nz.ac.otago.psyanlab.common.designer.program.stage.StageView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled()) {
        // Ignore touch events if not enabled.
        return false;
    }/* w ww.j  av  a 2 s  .  c o m*/

    final int action = event.getAction();
    final int pointerCount = event.getPointerCount();
    final Handler handler = getHandler();

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_POINTER_DOWN: {
        // Throw away event if we have already seen at least this many
        // fingers before.
        if (mMaxFingersDown > pointerCount) {
            return true;
        }

        if (handler != null) {
            handler.removeCallbacks(mPendingCheckForTap);
            handler.removeCallbacks(mPendingCheckForLongPress);
        }

        mPendingCheckForTap = new CheckForTap();
        postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());

        mMaxFingersDown = pointerCount;

        mMotionPosition = INVALID_POSITION;
        updateMotionCoords(event, pointerCount);

        mTouchMode = TOUCH_MODE_DOWN;

        return true;
    }

    case MotionEvent.ACTION_DOWN: {
        mMaxFingersDown = pointerCount;

        if (mPendingCheckForTap == null) {
            mPendingCheckForTap = new CheckForTap();
        }
        postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());

        updateMotionCoords(event, pointerCount);
        mMotionPosition = pointToPosition(mMotionX.get(0).intValue(), mMotionY.get(0).intValue());

        mTouchMode = TOUCH_MODE_DOWN;

        return true;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mMaxFingersDown == 1 && mMotionPosition != NO_MATCHED_CHILD
                && mMotionPosition == pointToPosition((int) event.getX(), (int) event.getY())) {
            // Ignore movement in single touch mode until the user has
            // moved out of the prop hit area.
            return true;
        }

        boolean moveIsOverSlop = false;
        int touchSlop = mMaxFingersDown > 1 ? mTouchSlop * 6 : mTouchSlop;
        for (int pointerIndex = 0; pointerIndex < pointerCount; pointerIndex++) {
            int pointerId = event.getPointerId(pointerIndex);
            moveIsOverSlop = moveIsOverSlop
                    || Math.abs(event.getY(pointerIndex) - mMotionY.get(pointerId)) > touchSlop
                    || Math.abs(event.getX(pointerIndex) - mMotionX.get(pointerId)) > touchSlop;
        }

        if (mTouchMode != TOUCH_MODE_AT_REST && moveIsOverSlop) {
            // Too much movement to be a tap event.
            mTouchMode = TOUCH_MODE_AT_REST;
            final View child = getChildAt(mMotionPosition);
            if (child != null) {
                child.setPressed(false);
            }
            setPressed(false);
            if (handler != null) {
                handler.removeCallbacks(mPendingCheckForLongPress);
            }
            mMotionPosition = NO_MATCHED_CHILD;
            updateSelectorState();
            invalidate();
        }
        return true;
    }

    case MotionEvent.ACTION_UP: {
        if (mTouchMode == TOUCH_MODE_FINISHED_LONG_PRESS) {
            return true;
        }

        if (mTouchMode == TOUCH_MODE_AT_REST) {
            break;
        }

        // Handle stage multi-touch.

        if (mMotionPosition == NO_MATCHED_CHILD) {
            if (mPerformPropClick == null) {
                mPerformPropClick = new PerformClick();
            }

            final PerformClick performPropClick = mPerformPropClick;
            performPropClick.mClickMotionPosition = mMotionPosition;
            performPropClick.rememberWindowAttachCount();

            if (mTouchMode != TOUCH_MODE_DOWN || mTouchMode != TOUCH_MODE_TAP) {
                if (handler != null) {
                    handler.removeCallbacks(
                            mTouchMode == TOUCH_MODE_DOWN ? mPendingCheckForTap : mPendingCheckForLongPress);
                }

                if (!mDataChanged) {
                    // Got here so must be a tap. The long press would
                    // have triggered inside the delayed runnable.
                    mTouchMode = TOUCH_MODE_TAP;
                    positionSelector(this);
                    setPressed(true);
                    updateSelectorState();
                    invalidate();

                    resetSelectorTransition(getVirtualFingers());

                    if (mTouchModeReset != null) {
                        removeCallbacks(mTouchModeReset);
                    }
                    mTouchModeReset = new Runnable() {
                        @Override
                        public void run() {
                            mTouchMode = TOUCH_MODE_AT_REST;
                            setPressed(false);
                            if (!mDataChanged) {
                                performPropClick.run();
                            }
                            updateSelectorState();
                            invalidate();
                        }
                    };
                    postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                } else {
                    mTouchMode = TOUCH_MODE_AT_REST;
                }
            } else if (!mDataChanged) {
                performPropClick.run();
            }
        } else {
            // Handle touch on child.
            final View child = getChildAt(mMotionPosition);
            if (child != null && !child.hasFocusable()) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformPropClick == null) {
                    mPerformPropClick = new PerformClick();
                }

                final PerformClick performPropClick = mPerformPropClick;
                performPropClick.mClickMotionPosition = mMotionPosition;
                performPropClick.rememberWindowAttachCount();

                if (mTouchMode != TOUCH_MODE_DOWN || mTouchMode != TOUCH_MODE_TAP) {
                    if (handler != null) {
                        handler.removeCallbacks(mTouchMode == TOUCH_MODE_DOWN ? mPendingCheckForTap
                                : mPendingCheckForLongPress);
                    }

                    if (!mDataChanged) {
                        // Got here so must be a tap. The long press
                        // would
                        // have triggered inside the delayed runnable.
                        mTouchMode = TOUCH_MODE_TAP;
                        child.setPressed(true);
                        positionSelector(child);
                        setPressed(true);
                        updateSelectorState();
                        invalidate();

                        resetSelectorTransition(getVirtualFingers());

                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }
                        mTouchModeReset = new Runnable() {
                            @Override
                            public void run() {
                                mTouchMode = TOUCH_MODE_AT_REST;
                                child.setPressed(false);
                                setPressed(false);
                                updateSelectorState();
                                invalidate();
                                if (!mDataChanged) {
                                    performPropClick.run();
                                }
                            }
                        };
                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                    } else {
                        mTouchMode = TOUCH_MODE_AT_REST;
                        updateSelectorState();
                        invalidate();
                    }
                } else if (!mDataChanged) {
                    performPropClick.run();
                }
            }
        }
        return true;
    }
    }
    return true;
}

From source file:ac.robinson.mediaphone.MediaPhoneActivity.java

protected boolean verifyButtonClick(View currentButton) {
    // handle a problem on some devices where touch events get passed twice if the finger moves slightly
    final int buttonId = currentButton.getId();
    if (mRecentlyClickedButton == buttonId) {
        mRecentlyClickedButton = -1; // just in case - don't want to get stuck in the unclickable state
        if (MediaPhone.DEBUG) {
            Log.d(DebugUtilities.getLogTag(this), "Discarding button click too soon after previous");
        }/*from   w w w  .j  ava 2  s .  c om*/
        return false;
    }

    // allow button clicks after a tap-length timeout
    mRecentlyClickedButton = buttonId;
    currentButton.postDelayed(new Runnable() {
        @Override
        public void run() {
            mRecentlyClickedButton = -1;
        }
    }, ViewConfiguration.getTapTimeout());

    return true;
}

From source file:com.appeaser.sublimepickerlibrary.datepicker.SimpleMonthView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int x = (int) (event.getX() + 0.5f);
    final int y = (int) (event.getY() + 0.5f);

    final int action = event.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mDownX = x;/*from  w  w  w. j  av  a  2s  .  c o  m*/
        mDownY = y;

        mInitialTarget = getDayAtLocation(mDownX, mDownY);

        if (mInitialTarget < 0) {
            return false;
        }

        if (mPendingCheckForTap == null) {
            mPendingCheckForTap = new CheckForTap();
        }
        postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
        break;
    case MotionEvent.ACTION_MOVE:
        if (!isStillAClick(x, y)) {
            if (mPendingCheckForTap != null) {
                removeCallbacks(mPendingCheckForTap);
            }

            mInitialTarget = -1;

            if (mTouchedItem >= 0) {
                mTouchedItem = -1;
                invalidate();
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        onDayClicked(mInitialTarget);
        // Fall through.
    case MotionEvent.ACTION_CANCEL:
        if (mPendingCheckForTap != null) {
            removeCallbacks(mPendingCheckForTap);
        }
        // Reset touched day on stream end.
        mTouchedItem = -1;
        mInitialTarget = -1;
        invalidate();
        break;
    }
    return true;
}

From source file:org.telegram.ui.Components.NumberPicker.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled()) {
        return false;
    }/* www.  j a v a 2s.  c  o  m*/
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    int action = event.getActionMasked();
    switch (action) {
    case MotionEvent.ACTION_MOVE: {
        if (mIngonreMoveEvents) {
            break;
        }
        float currentMoveY = event.getY();
        if (mScrollState != OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
            int deltaDownY = (int) Math.abs(currentMoveY - mLastDownEventY);
            if (deltaDownY > mTouchSlop) {
                removeAllCallbacks();
                onScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
            }
        } else {
            int deltaMoveY = (int) ((currentMoveY - mLastDownOrMoveEventY));
            scrollBy(0, deltaMoveY);
            invalidate();
        }
        mLastDownOrMoveEventY = currentMoveY;
    }
        break;
    case MotionEvent.ACTION_UP: {
        removeChangeCurrentByOneFromLongPress();
        mPressedStateHelper.cancel();
        VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
        int initialVelocity = (int) velocityTracker.getYVelocity();
        if (Math.abs(initialVelocity) > mMinimumFlingVelocity) {
            fling(initialVelocity);
            onScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
        } else {
            int eventY = (int) event.getY();
            int deltaMoveY = (int) Math.abs(eventY - mLastDownEventY);
            long deltaTime = event.getEventTime() - mLastDownEventTime;
            if (deltaMoveY <= mTouchSlop && deltaTime < ViewConfiguration.getTapTimeout()) {
                int selectorIndexOffset = (eventY / mSelectorElementHeight) - SELECTOR_MIDDLE_ITEM_INDEX;
                if (selectorIndexOffset > 0) {
                    changeValueByOne(true);
                    mPressedStateHelper.buttonTapped(PressedStateHelper.BUTTON_INCREMENT);
                } else if (selectorIndexOffset < 0) {
                    changeValueByOne(false);
                    mPressedStateHelper.buttonTapped(PressedStateHelper.BUTTON_DECREMENT);
                }
            } else {
                ensureScrollWheelAdjusted();
            }
            onScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
    }
        break;
    }
    return true;
}

From source file:com.hippo.widget.Slider.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    int action = event.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_MOVE:
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:

        if (mListener != null) {
            if (action == MotionEvent.ACTION_DOWN) {
                mListener.onFingerDown();
            } else if (action == MotionEvent.ACTION_UP) {
                mListener.onFingerUp();//from  w w  w . j a va2 s .  c  om
            }
        }

        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        float radius = mRadius;
        float x = event.getX();
        int progress = Math.round(MathUtils.lerp((float) mStart, (float) mEnd,
                MathUtils
                        .clamp((mReverse ? (getWidth() - paddingLeft - radius - x) : (x - radius - paddingLeft))
                                / (getWidth() - radius - radius - paddingLeft - paddingRight), 0.0f, 1.0f)));
        float percent = MathUtils.delerp(mStart, mEnd, progress);

        // ACTION_CANCEL not changed
        if (action == MotionEvent.ACTION_CANCEL) {
            progress = mProgress;
            percent = mPercent;
        }

        if (mTargetProgress != progress) {
            mTargetProgress = progress;
            startProgressAnimation(percent);

            if (mListener != null) {
                mListener.onSetProgress(this, mProgress, progress, true, false);
            }
        }

        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
            SimpleHandler.getInstance().removeCallbacks(mCheckForShowBubble);
            setShowBubble(false);
        } else if (action == MotionEvent.ACTION_DOWN) {
            if (mCheckForShowBubble == null) {
                mCheckForShowBubble = new CheckForShowBubble();
            }
            SimpleHandler.getInstance().postDelayed(mCheckForShowBubble, ViewConfiguration.getTapTimeout());
        }

        if (action == MotionEvent.ACTION_UP) {
            int oldProgress = mProgress;
            if (mProgress != progress) {
                mProgress = progress;
                mPercent = mDrawPercent;
            }
            if (mListener != null) {
                mListener.onSetProgress(this, progress, oldProgress, true, true);
            }
        }
        break;
    }

    return true;
}

From source file:nf.framework.expand.widgets.staggerView.StaggeredGridView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    acquireVelocityTracker(ev);/*  ww  w . j  a va2 s .c o m*/
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

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

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        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) {
            mTouchMode = TOUCH_MODE_DRAGGING;
        }

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

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

        updateSelectorState();
    }
        break;

    case MotionEvent.ACTION_CANCEL:
        mTouchMode = TOUCH_MODE_IDLE;
        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;
        releaseVelocityTracker();
        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
            mTouchMode = TOUCH_MODE_FLINGING;
            mScroller.fling(0, 0, 0, (int) velocity, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
            mLastTouchY = 0;
            invalidate();
        } else {
            mTouchMode = 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();
        releaseVelocityTracker();
    }
        break;
    }
    return true;
}

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

@Override
public boolean onTouchEvent(MotionEvent ev) {
    velocityTracker.addMovement(ev);// w  w  w . jav a2  s  .  c  om
    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.rajul.staggeredgridview.StaggeredGridView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);//from  w  w w  .  ja va 2  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:
        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;

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

            postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
        }
        mMotionPosition = motionPosition;
        ViewCompat.postInvalidateOnAnimation(this);
        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();
            }
        }
    }
        break;

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

        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
            mTouchMode = TOUCH_MODE_FLINGING;
            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 {
            mTouchMode = TOUCH_MODE_IDLE;
            setTouchMode(TOUCH_MODE_IDLE);
        }

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

        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) {
                    // TODO
                    ViewCompat.postInvalidateOnAnimation(this);
                    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;
                        updateSelectorState();
                    }
                    return true;
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

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

From source file:cn.iterlog.myapplication.widget.overscroll.StaggeredGridView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);//  w  ww . jav a2 s .  c o  m
    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 && mAdapter != null
                && mAdapter.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) {
            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();
            }
        }

        updateSelectorState();
    }
        break;

    case MotionEvent.ACTION_CANCEL:
        mTouchMode = TOUCH_MODE_IDLE;
        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;
        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
            mTouchMode = TOUCH_MODE_FLINGING;
            mScroller.fling(0, 0, 0, (int) velocity, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
            mLastTouchY = 0;
            invalidate();
        } else {
            mTouchMode = TOUCH_MODE_IDLE;
        }

        if (!mDataChanged && mAdapter != null && 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 != null && 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 != null && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }

            mTouchMode = TOUCH_MODE_REST;
        }

        mBeginClick = false;

        updateSelectorState();
    }
        break;
    }
    return true;
}