Java Code Examples for androidx.core.view.MotionEventCompat#getActionMasked()

The following examples show how to use androidx.core.view.MotionEventCompat#getActionMasked() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SwipeableViewPager.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(final MotionEvent event) {
    int action = MotionEventCompat.getActionMasked(event);

    switch (action) {
        case (MotionEvent.ACTION_DOWN):
            return super.onInterceptTouchEvent(event);
        case (MotionEvent.ACTION_MOVE):
            if (!swipingAllowed) {
                return false;
            }
            return super.onInterceptTouchEvent(event);
        case (MotionEvent.ACTION_UP):
            if (!swipingAllowed) {
                return false;
            }
            return super.onInterceptTouchEvent(event);
        default:
            return super.onInterceptTouchEvent(event);
    }
}
 
Example 2
Source File: SuperRecyclerView.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    if (mRefreshEnabled) {
        final int action = MotionEventCompat.getActionMasked(e);
        final int actionIndex = MotionEventCompat.getActionIndex(e);
        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                mActivePointerId = MotionEventCompat.getPointerId(e, 0);
                mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
                mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
            }
            break;
            case MotionEvent.ACTION_POINTER_DOWN: {
                mActivePointerId = MotionEventCompat.getPointerId(e, actionIndex);
                mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
                mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
            }
            break;
            case MotionEventCompat.ACTION_POINTER_UP: {
                onPointerUp(e);
            }
            break;
        }
    }
    return super.onInterceptTouchEvent(e);
}
 
Example 3
Source File: PullDismissLayout.java    From StoryView with MIT License 5 votes vote down vote up
public boolean onInterceptTouchEvent(MotionEvent event) {
    int action = MotionEventCompat.getActionMasked(event);

    boolean pullingDown = false;

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            verticalTouchSlop = event.getY();
        case MotionEvent.ACTION_MOVE:
            final float dy = event.getY() - verticalTouchSlop;
            if (dy > dragHelper.getTouchSlop()) {
                pullingDown = true;
                mTouchCallbacks.touchPull();
            }else{
                mTouchCallbacks.touchDown(event.getX(), event.getY());
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            verticalTouchSlop = 0.0f;
            mTouchCallbacks.touchUp();
            break;
    }

    if (!dragHelper.shouldInterceptTouchEvent(event) && pullingDown) {
        if (dragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE &&
                dragHelper.checkTouchSlop(ViewDragHelper.DIRECTION_VERTICAL)) {

            View child = getChildAt(0);
            if (child != null && !listener.onShouldInterceptTouchEvent()) {
                dragHelper.captureChildView(child, event.getPointerId(0));
                return dragHelper.getViewDragState() == ViewDragHelper.STATE_DRAGGING;
            }
        }
    }
    return false;
}
 
Example 4
Source File: SwipeListView.java    From UltimateRecyclerView with Apache License 2.0 5 votes vote down vote up
/**
 * @see ListView#onInterceptTouchEvent(MotionEvent)
 */
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    int action = MotionEventCompat.getActionMasked(ev);
    final float x = ev.getX();
    final float y = ev.getY();

    if (isEnabled() && touchListener.isSwipeEnabled()) {

        if (touchState == TOUCH_STATE_SCROLLING_X) {
            return touchListener.onTouch(this, ev);
        }

        switch (action) {
            case MotionEvent.ACTION_MOVE:
                checkInMoving(x, y);
                return touchState == TOUCH_STATE_SCROLLING_Y;
            case MotionEvent.ACTION_DOWN:
                super.onInterceptTouchEvent(ev);
                touchListener.onTouch(this, ev);
                touchState = TOUCH_STATE_REST;
                lastMotionX = x;
                lastMotionY = y;
                return false;
            case MotionEvent.ACTION_CANCEL:
                touchState = TOUCH_STATE_REST;
                break;
            case MotionEvent.ACTION_UP:
                touchListener.onTouch(this, ev);
                return touchState == TOUCH_STATE_SCROLLING_Y;
            default:
                break;
        }
    }

    return super.onInterceptTouchEvent(ev);
}
 
Example 5
Source File: DuoDrawerLayout.java    From duo-navigation-drawer with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    switch (action) {
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP: {
            mViewDragHelper.cancel();
            return false;
        }
    }

    return mViewDragHelper.shouldInterceptTouchEvent(ev);
}
 
Example 6
Source File: NoteEditActivity.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
@Override
public void itemSelectMove(MotionEvent event, RecyclerView.ViewHolder holder) {
    final int action = MotionEventCompat.getActionMasked(event);
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            showDragDelete();
            break;
        case MotionEvent.ACTION_MOVE:
            Rect moveRect = new Rect();
            mDragDelete.getGlobalVisibleRect(moveRect);
            Rect itemMoveRect = new Rect();
            ((BaseViewHolder) holder).getView(R.id.item_note_edit_image).getGlobalVisibleRect(itemMoveRect);
            if (Math.max(itemMoveRect.top, moveRect.top) < Math.min(itemMoveRect.bottom, moveRect.bottom)) {
                showCanDragDelete();
            } else {
                hideCanDragDelete();
            }
            break;
        case MotionEvent.ACTION_UP:
            Rect upRect = new Rect();
            mDragDelete.getGlobalVisibleRect(upRect);
            Rect itemUpRect = new Rect();
            ((BaseViewHolder) holder).getView(R.id.item_note_edit_image).getGlobalVisibleRect(itemUpRect);
            hideDragDelete();
            if (Math.max(itemUpRect.top, upRect.top) < Math.min(itemUpRect.bottom, upRect.bottom)) {
                removeImage(holder.getLayoutPosition());
                mAdapter.notifyDataSetChanged();
            }
            break;
        default:
            break;
    }
}
 
Example 7
Source File: ItemTouchHelperExtension.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
private void handleTouch(MotionEvent ev) {
    if (mCallback != null && mSelected != null) {
        mCallback.itemSelectMove(ev, mSelected);
    }
    if (DEBUG && mSelected != null) {
        float x = ev.getX();
        float y = ev.getY();
        final int action = MotionEventCompat.getActionMasked(ev);
        Log.d("updateDxDy", "x:" + x + " y:" + y + " action:" + action + " position:" + mSelected.getLayoutPosition());
    }
}
 
Example 8
Source File: FloatingNavigationView.java    From Floating-Navigation-View with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouch(@NonNull View v, @NonNull MotionEvent event) {
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    if ((MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN)
            && ((x < 0) || (x >= mNavigationView.getWidth())
            || (y < 0) || (y >= mNavigationView.getHeight()))) {
        close();
        return true;
    } else if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_OUTSIDE) {
        close();
        return true;
    }
    return false;
}
 
Example 9
Source File: BaseItemDraggableAdapter.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
/**
 * Set the drag event should be trigger on long press.
 * Work when the toggleViewId has been set.
 *
 * @param longPress by default is true.
 */
public void setToggleDragOnLongPress(boolean longPress) {
    mDragOnLongPress = longPress;
    if (mDragOnLongPress) {
        mOnToggleViewTouchListener = null;
        mOnToggleViewLongClickListener = new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mItemTouchHelper != null && itemDragEnabled) {
                    mItemTouchHelper.startDrag((RecyclerView.ViewHolder) v.getTag(R.id.BaseQuickAdapter_viewholder_support));
                }
                return true;
            }
        };
    } else {
        mOnToggleViewTouchListener = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN
                        && !mDragOnLongPress) {
                    if (mItemTouchHelper != null && itemDragEnabled) {
                        mItemTouchHelper.startDrag((RecyclerView.ViewHolder) v.getTag(R.id.BaseQuickAdapter_viewholder_support));
                    }
                    return true;
                } else {
                    return false;
                }
            }
        };
        mOnToggleViewLongClickListener = null;
    }
}
 
Example 10
Source File: CustomSwipeRefreshLayout.java    From NewFastFrame with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();

    final int action = MotionEventCompat.getActionMasked(ev);
    int pointerIndex;

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }

    if (!isEnabled() || mReturningToStart || canChildScrollUp()
            || mRefreshing || mNestedScrollInProgress) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    if (!canChildScrollUp() && mGestureDetector.onTouchEvent(ev)) {
        return false;
    }


    switch (action) {
        case MotionEvent.ACTION_DOWN:
            setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true);
            mActivePointerId = ev.getPointerId(0);
            mIsBeingDragged = false;

            pointerIndex = ev.findPointerIndex(mActivePointerId);
            if (pointerIndex < 0) {
                return false;
            }
            mInitialDownY = ev.getY(pointerIndex);
            break;

        case MotionEvent.ACTION_MOVE:
            if (mActivePointerId == INVALID_POINTER) {
                Log.e(LOG_TAG, "Got ACTION_MOVE event but don't have an active pointer id.");
                return false;
            }

            pointerIndex = ev.findPointerIndex(mActivePointerId);
            if (pointerIndex < 0) {
                return false;
            }
            final float y = ev.getY(pointerIndex);
            startDragging(y);
            break;

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

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mIsBeingDragged = false;
            mActivePointerId = INVALID_POINTER;
            break;
    }

    return mIsBeingDragged;
}
 
Example 11
Source File: VRefreshLayout.java    From BaseProject with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureContent();
    int action = MotionEventCompat.getActionMasked(ev);
    if (!isEnabled() || canChildScrollUp() || mIsRefreshing) {
        return false;
    }
    int pointerIndex;
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            if(isLogMeasure)
            Log.i(TAG, "-->onInterceptTouchEvent: ACTION_DOWN");
            mIsBeingDragged = false;
            notifyStatus(STATUS_INIT);
            mActivePointerId = ev.getPointerId(0);
            pointerIndex = ev.findPointerIndex(mActivePointerId);
            if (pointerIndex < 0) {
                return false;
            }
            mInitDownY = ev.getY(pointerIndex);
            break;

        case MotionEvent.ACTION_MOVE:
            if(isLogMeasure)
            Log.i(TAG, "-->onInterceptTouchEvent: ACTION_MOVE");
            pointerIndex = ev.findPointerIndex(mActivePointerId);
            if (pointerIndex < 0) {
                return false;
            }
            float evY = ev.getY(pointerIndex);
            checkDragging(evY);
            break;

        case MotionEventCompat.ACTION_POINTER_UP:
            if(isLogMeasure)
            Log.i(TAG, "-->onInterceptTouchEvent: ACTION_POINTER_UP");
            checkOtherPointerUp(ev);
            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            if(isLogMeasure)
            Log.i(TAG, "-->onInterceptTouchEvent: ACTION_OVER");
            mIsBeingDragged = false;
            mActivePointerId = -1;
            break;
    }


    return mIsBeingDragged;
}
 
Example 12
Source File: SlidingPanelLayout.java    From NewFastFrame with Apache License 2.0 4 votes vote down vote up
@Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        final int action = MotionEventCompat.getActionMasked(ev);

        if (!isEnabled() || !isTouchEnabled() || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
//            mDragHelper.abort();
            return super.dispatchTouchEvent(ev);
        }

        final float y = ev.getY();

        if (action == MotionEvent.ACTION_DOWN) {
            mIsScrollableViewHandlingTouch = false;
            mPrevMotionY = y;
        } else if (action == MotionEvent.ACTION_MOVE) {
            float dy = y - mPrevMotionY;
            mPrevMotionY = y;

            // If the scroll view isn't under the touch, pass the
            // event along to the dragView.
            if (!isViewUnder(mScrollableView, (int) mInitialMotionX, (int) mInitialMotionY)) {
                return super.dispatchTouchEvent(ev);
            }

            // Which direction (up or down) is the drag moving?
            if (dy * (mIsSlidingUp ? 1 : -1) > 0) { // Collapsing
                // Is the child less than fully scrolled?
                // Then let the child handle it.
                if (mScrollableViewHelper.getScrollableViewScrollPosition(mScrollableView, mIsSlidingUp) > 0) {
                    mIsScrollableViewHandlingTouch = true;
                    return super.dispatchTouchEvent(ev);
                }

                // Was the child handling the touch previously?
                // Then we need to rejigger things so that the
                // drag panel gets a proper down event.
                if (mIsScrollableViewHandlingTouch) {
                    // Send an 'UP' event to the child.
                    MotionEvent up = MotionEvent.obtain(ev);
                    up.setAction(MotionEvent.ACTION_CANCEL);
                    super.dispatchTouchEvent(up);
                    up.recycle();

                    // Send a 'DOWN' event to the panel. (We'll cheat
                    // and hijack this one)
                    ev.setAction(MotionEvent.ACTION_DOWN);
                }

                mIsScrollableViewHandlingTouch = false;
                return this.onTouchEvent(ev);
            } else if (dy * (mIsSlidingUp ? 1 : -1) < 0) { // Expanding
                // Is the panel less than fully expanded?
                // Then we'll handle the drag here.
                if (mSlideOffset < 1.0f) {
                    mIsScrollableViewHandlingTouch = false;
                    return this.onTouchEvent(ev);
                }

                // Was the panel handling the touch previously?
                // Then we need to rejigger things so that the
                // child gets a proper down event.
                if (!mIsScrollableViewHandlingTouch && mDragHelper.isDragging()) {
                    mDragHelper.cancel();
                    ev.setAction(MotionEvent.ACTION_DOWN);
                }

                mIsScrollableViewHandlingTouch = true;
                return super.dispatchTouchEvent(ev);
            }
        } else if (action == MotionEvent.ACTION_UP) {
            // If the scrollable view was handling the touch and we receive an up
            // we want to clear any previous dragging state so we don't intercept a touch stream accidentally
            if (mIsScrollableViewHandlingTouch) {
                mDragHelper.setDragState(ViewDragHelper.STATE_IDLE);
            }
        }

        // In all other cases, just let the default behavior take over.
        return super.dispatchTouchEvent(ev);
    }
 
Example 13
Source File: SlidingUpPanelLayout.java    From react-native-photo-editor with Apache License 2.0 4 votes vote down vote up
@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    if (!isEnabled() || !isTouchEnabled() || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
        mDragHelper.cancel();
        return super.dispatchTouchEvent(ev);
    }

    final float y = ev.getY();

    if (action == MotionEvent.ACTION_DOWN) {
        mIsScrollableViewHandlingTouch = false;
        mPrevMotionY = y;
    } else if (action == MotionEvent.ACTION_MOVE) {
        float dy = y - mPrevMotionY;
        mPrevMotionY = y;

        // If the scroll view isn't under the touch, pass the
        // event along to the dragView.
        if (!isViewUnder(mScrollableView, (int) mInitialMotionX, (int) mInitialMotionY)) {
            return super.dispatchTouchEvent(ev);
        }

        // Which direction (up or down) is the drag moving?
        if (dy * (mIsSlidingUp ? 1 : -1) > 0) { // Collapsing
            // Is the child less than fully scrolled?
            // Then let the child handle it.
            if (getScrollableViewScrollPosition() > 0) {
                mIsScrollableViewHandlingTouch = true;
                return super.dispatchTouchEvent(ev);
            }

            // Was the child handling the touch previously?
            // Then we need to rejigger things so that the
            // drag panel gets a proper down event.
            if (mIsScrollableViewHandlingTouch) {
                // Send an 'UP' event to the child.
                MotionEvent up = MotionEvent.obtain(ev);
                up.setAction(MotionEvent.ACTION_CANCEL);
                super.dispatchTouchEvent(up);
                up.recycle();

                // Send a 'DOWN' event to the panel. (We'll cheat
                // and hijack this one)
                ev.setAction(MotionEvent.ACTION_DOWN);
            }

            mIsScrollableViewHandlingTouch = false;
            return this.onTouchEvent(ev);
        } else if (dy * (mIsSlidingUp ? 1 : -1) < 0) { // Expanding
            // Is the panel less than fully expanded?
            // Then we'll handle the drag here.
            if (mSlideOffset < 1.0f) {
                mIsScrollableViewHandlingTouch = false;
                return this.onTouchEvent(ev);
            }

            // Was the panel handling the touch previously?
            // Then we need to rejigger things so that the
            // child gets a proper down event.
            if (!mIsScrollableViewHandlingTouch && mDragHelper.isDragging()) {
                mDragHelper.cancel();
                ev.setAction(MotionEvent.ACTION_DOWN);
            }

            mIsScrollableViewHandlingTouch = true;
            return super.dispatchTouchEvent(ev);
        }
    } else if (action == MotionEvent.ACTION_UP && mIsScrollableViewHandlingTouch) {
        // If the scrollable view was handling the touch and we receive an up
        // we want to clear any previous dragging state so we don't intercept a touch stream accidentally
        mDragHelper.setDragState(ViewDragHelper.STATE_IDLE);
    }

    // In all other cases, just let the default behavior take over.
    return super.dispatchTouchEvent(ev);
}
 
Example 14
Source File: DragLayout.java    From UIWidget with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(@NonNull MotionEvent event) {
    final int action = MotionEventCompat.getActionMasked(event);

    if (!isEnabled()) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mActivePointerId = INVALID_POINTER;
        mIsBeingDragged = false;
        if (mCollapsible && -yDiff > mDragHelper.getTouchSlop()) {
            expand(mDragHelper.getCapturedView(), 0);
        }
        mDragHelper.cancel();
        return false;
    }

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mHeight = getChildAt(0).getHeight();
            mTop = getChildAt(0).getTop();
            mActivePointerId = MotionEventCompat.getPointerId(event, 0);
            mIsBeingDragged = false;
            final float initialMotionY = getMotionEventY(event, mActivePointerId);
            if (initialMotionY == -1) {
                return false;
            }
            mInitialMotionY = initialMotionY;
            yDiff = 0;
            break;
        case MotionEvent.ACTION_MOVE:
            if (mActivePointerId == INVALID_POINTER) {
                return false;
            }
            final float y = getMotionEventY(event, mActivePointerId);
            if (y == -1) {
                return false;
            }
            yDiff = y - mInitialMotionY;
            if (mDragEnable && yDiff > mDragHelper.getTouchSlop() && !mIsBeingDragged) {
                mIsBeingDragged = true;
                mDragHelper.captureChildView(getChildAt(0), 0);
            }
            break;
    }
    mDragHelper.shouldInterceptTouchEvent(event);
    return mIsBeingDragged;
}
 
Example 15
Source File: SlidingUpPanelLayout.java    From react-native-photo-editor with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    // If the scrollable view is handling touch, never intercept
    if (mIsScrollableViewHandlingTouch) {
        mDragHelper.cancel();
        return false;
    }

    final int action = MotionEventCompat.getActionMasked(ev);
    final float x = ev.getX();
    final float y = ev.getY();

    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            mIsUnableToDrag = false;
            mInitialMotionX = x;
            mInitialMotionY = y;
            break;
        }

        case MotionEvent.ACTION_MOVE: {
            final float adx = Math.abs(x - mInitialMotionX);
            final float ady = Math.abs(y - mInitialMotionY);
            final int dragSlop = mDragHelper.getTouchSlop();

            if ((ady > dragSlop && adx > ady) || !isViewUnder(mDragView, (int) mInitialMotionX, (int) mInitialMotionY)) {
                mDragHelper.cancel();
                mIsUnableToDrag = true;
                return false;
            }
            break;
        }

        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            // If the dragView is still dragging when we get here, we need to call processTouchEvent
            // so that the view is settled
            // Added to make scrollable views work (tokudu)
            if (mDragHelper.isDragging()) {
                mDragHelper.processTouchEvent(ev);
                return true;
            }
            break;
    }
    return mDragHelper.shouldInterceptTouchEvent(ev);
}
 
Example 16
Source File: NestedFrameLayout.java    From UIWidget with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean result = false;

    MotionEvent trackedEvent = MotionEvent.obtain(event);

    final int action = MotionEventCompat.getActionMasked(event);

    if (action == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;
    }

    int y = (int) event.getY();

    event.offsetLocation(0, mNestedYOffset);

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mLastMotionY = y;
            startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
            result = super.onTouchEvent(event);
            break;
        case MotionEvent.ACTION_MOVE:
            int deltaY = mLastMotionY - y;

            if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
                deltaY -= mScrollConsumed[1];
                trackedEvent.offsetLocation(0, mScrollOffset[1]);
                mNestedYOffset += mScrollOffset[1];
            }

            mLastMotionY = y - mScrollOffset[1];

            int oldY = getScrollY();
            int newScrollY = Math.max(0, oldY + deltaY);
            int dyConsumed = newScrollY - oldY;
            int dyUnconsumed = deltaY - dyConsumed;

            if (dispatchNestedScroll(0, dyConsumed, 0, dyUnconsumed, mScrollOffset)) {
                mLastMotionY -= mScrollOffset[1];
                trackedEvent.offsetLocation(0, mScrollOffset[1]);
                mNestedYOffset += mScrollOffset[1];
            }

            result = super.onTouchEvent(trackedEvent);
            trackedEvent.recycle();
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            stopNestedScroll();
            result = super.onTouchEvent(event);
            break;
        default:
            break;
    }
    return result;
}
 
Example 17
Source File: ListPopupWindow.java    From material with Apache License 2.0 4 votes vote down vote up
/**
 * Observes motion events and determines when to start forwarding.
 *
 * @param srcEvent motion event in source view coordinates
 * @return true to start forwarding motion events, false otherwise
 */
private boolean onTouchObserved(MotionEvent srcEvent) {
    final View src = mSrc;
    if (!src.isEnabled()) {
        return false;
    }

    final int actionMasked = MotionEventCompat.getActionMasked(srcEvent);
    switch (actionMasked) {
        case MotionEvent.ACTION_DOWN:
            mActivePointerId = srcEvent.getPointerId(0);
            mWasLongPress = false;

            if (mDisallowIntercept == null) {
                mDisallowIntercept = new DisallowIntercept();
            }
            src.postDelayed(mDisallowIntercept, mTapTimeout);
            if (mTriggerLongPress == null) {
                mTriggerLongPress = new TriggerLongPress();
            }
            src.postDelayed(mTriggerLongPress, mLongPressTimeout);
            break;
        case MotionEvent.ACTION_MOVE:
            final int activePointerIndex = srcEvent.findPointerIndex(mActivePointerId);
            if (activePointerIndex >= 0) {
                final float x = srcEvent.getX(activePointerIndex);
                final float y = srcEvent.getY(activePointerIndex);
                if (!pointInView(src, x, y, mScaledTouchSlop)) {
                    clearCallbacks();

                    // Don't let the parent intercept our events.
                    src.getParent().requestDisallowInterceptTouchEvent(true);
                    return true;
                }
            }
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            clearCallbacks();
            break;
    }

    return false;
}
 
Example 18
Source File: CustomScrollViewTest.java    From WidgetCase with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (mTracker == null) {
        mTracker = VelocityTracker.obtain();
    }
    int action = MotionEventCompat.getActionMasked(event);
    int pointerIndex = MotionEventCompat.getActionIndex(event);
    MotionEvent vEvent = MotionEvent.obtain(event);
    boolean isAddEvent = false;

    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            mScrollState = SCROLL_STATE_IDLE;
            mScrollPointerId = event.getPointerId(0);
            mLastTouchY = (int) (event.getY() + 0.5f);
            break;
        }
        case MotionEvent.ACTION_POINTER_DOWN: {
            mScrollPointerId = event.getPointerId(pointerIndex);
            mLastTouchY = (int) (event.getY(pointerIndex) + 0.5f);
            break;
        }
        case MotionEvent.ACTION_MOVE: {
            final int index = event.findPointerIndex(mScrollPointerId);
            if (index < 0) {
                return false;
            }
            int currY = (int) (event.getY(index) + 0.5f);
            int dy = mLastTouchY - currY;
            if (mScrollState != SCROLL_STATE_DRAGING) {
                if (Math.abs(dy) > mTouchSlop) {
                    if (dy > 0) {
                        dy -= mTouchSlop;
                    } else {
                        dy += mTouchSlop;
                    }
                    mScrollState = SCROLL_STATE_DRAGING;
                }
            }
            if (mScrollState == SCROLL_STATE_DRAGING) {
                mLastTouchY = currY;
                scrollBy(0, dy);
            }
            break;
        }
        case MotionEvent.ACTION_POINTER_UP: {
            if (event.getPointerId(pointerIndex) == mScrollPointerId) {
                int newIndex = pointerIndex == 0 ? 1 : 0;
                mScrollPointerId = event.getPointerId(newIndex);
                mLastTouchY = (int) (event.getY(newIndex) + 0.5f);
            }
            break;
        }
        case MotionEvent.ACTION_UP: {
            mTracker.addMovement(vEvent);
            isAddEvent = true;
            mTracker.computeCurrentVelocity(1000, mMaxVelocity);
            float velocityY = -VelocityTrackerCompat.getYVelocity(mTracker, mScrollPointerId);
            if (Math.abs(velocityY) < mMinVelocity) {
                velocityY = 0F;
            } else {
                velocityY = Math.max(-mMaxVelocity, Math.min(velocityY, mMaxVelocity));
            }
            if (velocityY != 0) {
                LogUtil.logD("velocityY", "velocityY = " + velocityY);
                // 将速度反应到滚动器上去
                mFlinger.onFling((int) velocityY);
            } else {
                mScrollState = SCROLL_STATE_IDLE;
            }
            resetTouch();
            break;
        }
        case MotionEvent.ACTION_CANCEL:{
            resetTouch();
        }
    }
    if (!isAddEvent) {
        mTracker.addMovement(vEvent);
    }
    vEvent.recycle();
    return true;
}
 
Example 19
Source File: SlidingUpPanelLayout.java    From toktok-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    if (!isEnabled() || !isTouchEnabled() || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
        mDragHelper.abort();
        return super.dispatchTouchEvent(ev);
    }

    final float x = ev.getX();
    final float y = ev.getY();

    if (action == MotionEvent.ACTION_DOWN) {
        mIsScrollableViewHandlingTouch = false;
        mPrevMotionX = x;
        mPrevMotionY = y;
    } else if (action == MotionEvent.ACTION_MOVE) {
        float dx = x - mPrevMotionX;
        float dy = y - mPrevMotionY;
        mPrevMotionX = x;
        mPrevMotionY = y;

        if (Math.abs(dx) > Math.abs(dy)) {
            // Scrolling horizontally, so ignore
            return super.dispatchTouchEvent(ev);
        }

        // If the scroll view isn't under the touch, pass the
        // event along to the dragView.
        if (!isViewUnder(mScrollableView, (int) mInitialMotionX, (int) mInitialMotionY)) {
            return super.dispatchTouchEvent(ev);
        }

        // Which direction (up or down) is the drag moving?
        if (dy * (mIsSlidingUp ? 1 : -1) > 0) { // Collapsing
            // Is the child less than fully scrolled?
            // Then let the child handle it.
            if (mScrollableViewHelper.getScrollableViewScrollPosition(mScrollableView, mIsSlidingUp) > 0) {
                mIsScrollableViewHandlingTouch = true;
                return super.dispatchTouchEvent(ev);
            }

            // Was the child handling the touch previously?
            // Then we need to rejigger things so that the
            // drag panel gets a proper down event.
            if (mIsScrollableViewHandlingTouch) {
                // Send an 'UP' event to the child.
                MotionEvent up = MotionEvent.obtain(ev);
                up.setAction(MotionEvent.ACTION_CANCEL);
                super.dispatchTouchEvent(up);
                up.recycle();

                // Send a 'DOWN' event to the panel. (We'll cheat
                // and hijack this one)
                ev.setAction(MotionEvent.ACTION_DOWN);
            }

            mIsScrollableViewHandlingTouch = false;
            return this.onTouchEvent(ev);
        } else if (dy * (mIsSlidingUp ? 1 : -1) < 0) { // Expanding
            // Is the panel less than fully expanded?
            // Then we'll handle the drag here.
            if (mSlideOffset < 1.0f) {
                mIsScrollableViewHandlingTouch = false;
                return this.onTouchEvent(ev);
            }

            // Was the panel handling the touch previously?
            // Then we need to rejigger things so that the
            // child gets a proper down event.
            if (!mIsScrollableViewHandlingTouch && mDragHelper.isDragging()) {
                mDragHelper.cancel();
                ev.setAction(MotionEvent.ACTION_DOWN);
            }

            mIsScrollableViewHandlingTouch = true;
            return super.dispatchTouchEvent(ev);
        }
    } else if (action == MotionEvent.ACTION_UP) {
        // If the scrollable view was handling the touch and we receive an up
        // we want to clear any previous dragging state so we don't intercept a touch stream accidentally
        if (mIsScrollableViewHandlingTouch) {
            mDragHelper.setDragState(ViewDragHelper.STATE_IDLE);
        }
    }

    // In all other cases, just let the default behavior take over.
    return super.dispatchTouchEvent(ev);
}
 
Example 20
Source File: SlidingUpPanelLayout.java    From toktok-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(@NonNull MotionEvent ev) {
    // If the scrollable view is handling touch, never intercept
    if (mIsScrollableViewHandlingTouch || !isTouchEnabled()) {
        mDragHelper.abort();
        return false;
    }

    final int action = MotionEventCompat.getActionMasked(ev);
    final float x = ev.getX();
    final float y = ev.getY();
    final float adx = Math.abs(x - mInitialMotionX);
    final float ady = Math.abs(y - mInitialMotionY);
    final int dragSlop = mDragHelper.getTouchSlop();

    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            mIsUnableToDrag = false;
            mInitialMotionX = x;
            mInitialMotionY = y;
            if (!isViewUnder(mDragView, (int) x, (int) y)) {
                mDragHelper.cancel();
                mIsUnableToDrag = true;
                return false;
            }

            break;
        }

        case MotionEvent.ACTION_MOVE: {
            if (ady > dragSlop && adx > ady) {
                mDragHelper.cancel();
                mIsUnableToDrag = true;
                return false;
            }
            break;
        }

        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            // If the dragView is still dragging when we get here, we need to call processTouchEvent
            // so that the view is settled
            // Added to make scrollable views work (tokudu)
            if (mDragHelper.isDragging()) {
                mDragHelper.processTouchEvent(ev);
                return true;
            }
            // Check if this was a click on the faded part of the screen, and fire off the listener if there is one.
            if (ady <= dragSlop
                    && adx <= dragSlop
                    && mSlideOffset > 0 && !isViewUnder(mSlideableView, (int) mInitialMotionX, (int) mInitialMotionY) && mFadeOnClickListener != null) {
                playSoundEffect(android.view.SoundEffectConstants.CLICK);
                mFadeOnClickListener.onClick(this);
                return true;
            }
            break;
    }
    return mDragHelper.shouldInterceptTouchEvent(ev);
}