Java Code Examples for android.view.MotionEvent#setAction()

The following examples show how to use android.view.MotionEvent#setAction() . 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: LockPatternView.java    From GestureLock with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onHoverEvent(MotionEvent event) {
    AccessibilityManager accessibilityManager =
                (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (accessibilityManager.isTouchExplorationEnabled()) {
        final int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_HOVER_ENTER:
                event.setAction(MotionEvent.ACTION_DOWN);
                break;
            case MotionEvent.ACTION_HOVER_MOVE:
                event.setAction(MotionEvent.ACTION_MOVE);
                break;
            case MotionEvent.ACTION_HOVER_EXIT:
                event.setAction(MotionEvent.ACTION_UP);
                break;
        }
        onTouchEvent(event);
        event.setAction(action);
    }
    return super.onHoverEvent(event);
}
 
Example 2
Source File: DynamicListLayout.java    From dynamiclistview with MIT License 6 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    /**
     * ADDED BY jangc. 
     * Cancel touch-event on start pulling.
     */
    if (mIsNowPullingDown || mIsNowPullingUp) {
        boolean isFirstPulling = mIsFirstPulling;
        onTouch(null, event);
        if (isFirstPulling) {
            if (mDynamicListViewListener != null)
                mDynamicListViewListener.onPullingStatusChanged(this, mDynamicListView, PullingStatus.START, null);

            mIsFirstPulling = false;
            event.setAction(MotionEvent.ACTION_CANCEL);
            ((View) mDynamicListView).onTouchEvent(event);
        }

        return true;
    }

    return super.dispatchTouchEvent(event);
}
 
Example 3
Source File: AttachmentView.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
	switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: {
			if (System.currentTimeMillis() - lastClickTime <= ViewConfiguration.getDoubleTapTimeout()) {
				event.setAction(MotionEvent.ACTION_CANCEL);
				return false;
			}
			break;
		}
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL: {
			lastClickTime = System.currentTimeMillis();
			break;
		}
	}
	return super.onTouchEvent(event);
}
 
Example 4
Source File: VectorClippedEventDelegate.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public boolean onHover(View v, MotionEvent event) {
    if(!isInside(event)) {
        if (mHovered) {
            mHovered = false;
            event.setAction(MotionEvent.ACTION_HOVER_EXIT);
            return v.onHoverEvent(event);
        }

        return false;

    } else {
        if (!mHovered) {
            mHovered = true;
            event.setAction(MotionEvent.ACTION_HOVER_ENTER);
        }

        return v.onHoverEvent(event);
    }
}
 
Example 5
Source File: TouchpadFragment.java    From wearmouse with Apache License 2.0 5 votes vote down vote up
public boolean onTouchEvent(MotionEvent e) {
    if (e.getActionMasked() == MotionEvent.ACTION_DOWN) {
        // Prevent Swipe-To-Dismiss
        MotionEvent cancel = MotionEvent.obtain(e);
        cancel.setAction(MotionEvent.ACTION_CANCEL);
        swipeDismissLayout.onTouchEvent(cancel);
        cancel.recycle();
    }
    gestureDetector.onTouchEvent(e);
    return true;
}
 
Example 6
Source File: ScrollTextView.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        //如果是新的按下事件,则对mBottomFlag重新初始化
        mBottomFlag = mOffsetHeight <= 0;
    }
    //如果已经不要这次事件,则传出取消的信号,这里的作用不大
    if (mBottomFlag) {
        event.setAction(MotionEvent.ACTION_CANCEL);
    }
    return super.dispatchTouchEvent(event);
}
 
Example 7
Source File: CleanEditText.java    From A-week-to-develop-android-app-plan with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
	if ((this.dRight != null) && (event.getAction() == 1)) {
		this.rBound = this.dRight.getBounds();
		int i = (int) event.getRawX();
		if (i > getRight() - 3 * this.rBound.width()) {
			// 点击的位置聚焦
			requestFocus();
			setText("");
			event.setAction(MotionEvent.ACTION_CANCEL);
		}
	}
	return super.onTouchEvent(event);
}
 
Example 8
Source File: InfiniteCycleManager.java    From InfiniteCycleViewPager with Apache License 2.0 5 votes vote down vote up
public boolean onTouchEvent(final MotionEvent event) {
    if (mViewPageable.getAdapter() == null || mViewPageable.getAdapter().getCount() == 0)
        return false;
    if (mIsAutoScroll || mIsInitialItem || mViewPageable.isFakeDragging()) return false;
    if (event.getPointerCount() > MIN_POINTER_COUNT || !mViewPageable.hasWindowFocus())
        event.setAction(MotionEvent.ACTION_UP);
    checkHitRect(event);
    return true;
}
 
Example 9
Source File: CleanEditText.java    From A-week-to-develop-android-app-plan with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
	if ((this.dRight != null) && (event.getAction() == 1)) {
		this.rBound = this.dRight.getBounds();
		int i = (int) event.getRawX();
		if (i > getRight() - 3 * this.rBound.width()) {
			// 点击的位置聚焦
			requestFocus();
			setText("");
			event.setAction(MotionEvent.ACTION_CANCEL);
		}
	}
	return super.onTouchEvent(event);
}
 
Example 10
Source File: SlidingUpPanelLayout.java    From AndroidAnimationExercise 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 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 11
Source File: ResideMenu.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    float currentActivityScaleX = ViewHelper.getScaleX(viewActivity);
    if (currentActivityScaleX == 1.0f)
        setScaleDirectionByRawX(ev.getRawX());

    switch (ev.getAction()){
        case MotionEvent.ACTION_DOWN:
            lastActionDownX = ev.getX();
            lastActionDownY = ev.getY();
            isInIgnoredView = isInIgnoredView(ev) && !isOpened();
            pressedState    = PRESSED_DOWN;
            break;

        case MotionEvent.ACTION_MOVE:
            if (isInIgnoredView || isInDisableDirection(scaleDirection))
                break;

            if(pressedState != PRESSED_DOWN &&
                    pressedState != PRESSED_MOVE_HORIZANTAL)
                break;

            int xOffset = (int) (ev.getX() - lastActionDownX);
            int yOffset = (int) (ev.getY() - lastActionDownY);

            if(pressedState == PRESSED_DOWN) {
                if(yOffset > 25 || yOffset < -25) {
                    pressedState = PRESSED_MOVE_VERTICAL;
                    break;
                }
                if(xOffset < -50 || xOffset > 50) {
                    pressedState = PRESSED_MOVE_HORIZANTAL;
                    ev.setAction(MotionEvent.ACTION_CANCEL);
                }
            } else if(pressedState == PRESSED_MOVE_HORIZANTAL) {
                if (currentActivityScaleX < 0.95)
                    showScrollViewMenu();

                float targetScale = getTargetScale(ev.getRawX());
                ViewHelper.setScaleX(viewActivity, targetScale);
                ViewHelper.setScaleY(viewActivity, targetScale);
                ViewHelper.setScaleX(imageViewShadow, targetScale + shadowAdjustScaleX);
                ViewHelper.setScaleY(imageViewShadow, targetScale + shadowAdjustScaleY);
                ViewHelper.setAlpha(scrollViewMenu, (1 - targetScale) * 2.0f);

                lastRawX = ev.getRawX();
                return true;
            }

            break;

        case MotionEvent.ACTION_UP:

            if (isInIgnoredView) break;
            if (pressedState != PRESSED_MOVE_HORIZANTAL) break;

            pressedState = PRESSED_DONE;
            if (isOpened()){
                if (currentActivityScaleX > 0.56f)
                    closeMenu();
                else
                    openMenu(scaleDirection);
            }else{
                if (currentActivityScaleX < 0.94f){
                    openMenu(scaleDirection);
                }else{
                    closeMenu();
                }
            }

            break;

    }
    lastRawX = ev.getRawX();
    return super.dispatchTouchEvent(ev);
}
 
Example 12
Source File: RefreshableListView.java    From elemeimitate with Apache License 2.0 4 votes vote down vote up
@Override
public boolean dispatchTouchEvent(final MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_MOVE && getFirstVisiblePosition() == 0) {
        float direction = ev.getY() - mHistoricalY;
        int height = (int) (ev.getY() - mY) / 2 + mInitialHeight;
        if (height < 0) {
            height = 0;
        }

        float deltaY = Math.abs(mY - ev.getY());
        ViewConfiguration config = ViewConfiguration.get(getContext());
        if (deltaY > config.getScaledTouchSlop()) {

            // Scrolling downward
            if (direction > 0) {
                // Refresh bar is extended if top pixel of the first item is
                // visible
                if (getChildAt(0).getTop() == 0) {
                    if (mHistoricalTop < 0) {

                        // mY = ev.getY(); // TODO works without
                        // this?mHistoricalTop = 0;
                    }

                    // Extends refresh bar
                    setHeaderHeight(height);

                    // Stop list scroll to prevent the list from
                    // overscrolling
                    ev.setAction(MotionEvent.ACTION_CANCEL);
                    mFlag = false;
                }
            } else if (direction < 0) {
                // Scrolling upward

                // Refresh bar is shortened if top pixel of the first item
                // is
                // visible
                if (getChildAt(0).getTop() == 0) {
                    setHeaderHeight(height);

                    // If scroll reaches top of the list, list scroll is
                    // enabled
                    if (getChildAt(1) != null && getChildAt(1).getTop() <= 1 && !mFlag) {
                        ev.setAction(MotionEvent.ACTION_DOWN);
                        mFlag = true;
                    }
                }
            }
        }

        mHistoricalY = ev.getY();
    }
    try {
        return super.dispatchTouchEvent(ev);
    } catch (Exception e) {
        return false;
    }
}
 
Example 13
Source File: CardStackLayout.java    From AndroidDemo with MIT License 4 votes vote down vote up
@Override
    public boolean onTouchEvent(MotionEvent event) {

        if (!isEnabled() || viewCanScrollUp()) {
            return false;
        }

        int action = event.getAction();
        int pointIndex;
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG, "touch action down");
                pointerId = event.getPointerId(0);
                pointIndex = event.findPointerIndex(pointerId);
                if (pointIndex < 0) {
                    return false;
                }
                isDragging = false;
                downY = event.getY(pointIndex);
                return true;//消耗掉才会触发下面的 move
            case MotionEvent.ACTION_MOVE:
                pointIndex = event.findPointerIndex(pointerId);
                if (pointIndex < 0) {
                    return false;
                }
                float y = event.getY(pointIndex);
                checkScrollBound(y);
                if (isDragging) {
                    //处理
                    float dy = y - lastMotionY;
//                    moveAllView(dy);
                    if (dy < 0 && targetCurrentOffset + dy <= targetEndOffset) {//父布局到顶了,事件重写下发
                        moveAllView(dy);
                        //重新下发,必须先触发down才能使move被子view拦截到
                        Log.d(TAG, "dispatch action down");
                        int tmp = event.getAction();
                        event.setAction(MotionEvent.ACTION_DOWN);
                        dispatchTouchEvent(event);
                        event.setAction(tmp);
                    } else if (dy > 0 && targetCurrentOffset + dy >= limitOffset) {//target 还原,如果已经还原就不让再下滑
                        Log.d(TAG, "到达限制区域");
                        if (targetCurrentOffset != limitOffset) {
                            moveAllView(limitOffset - targetCurrentOffset);
                            targetCurrentOffset = limitOffset;
                        }
                        isDragging = false;
                    } else {
                        moveAllView(dy);
                    }
                    lastMotionY = y;
                }
                break;
            case MotionEvent.ACTION_POINTER_UP:
                onSecondaryPointerUp(event);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                pointerId = -1;
                isDragging = false;
                break;
        }
        return isDragging;
    }
 
Example 14
Source File: SlidingUpPanelLayout.java    From AndroidSlidingUpPanel with Apache License 2.0 4 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();

    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 < mMaxSlideOffset) {
                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 15
Source File: PinnedSectionListView.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

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

    if (action == MotionEvent.ACTION_DOWN
            && mTouchTarget == null
            && mPinnedSection != null
            && isPinnedViewTouched(mPinnedSection.view, x, y)) { // create touch target

        // user touched pinned view
        mTouchTarget = mPinnedSection.view;
        mTouchPoint.x = x;
        mTouchPoint.y = y;

        // copy down event for eventually be used later
        mDownEvent = MotionEvent.obtain(ev);
    }

    if (mTouchTarget != null) {
        if (isPinnedViewTouched(mTouchTarget, x, y)) { // forward event to pinned view
            mTouchTarget.dispatchTouchEvent(ev);
        }

        if (action == MotionEvent.ACTION_UP) { // perform onClick on pinned view
            super.dispatchTouchEvent(ev);
            performPinnedItemClick();
            clearTouchTarget();

        } else if (action == MotionEvent.ACTION_CANCEL) { // cancel
            clearTouchTarget();

        } else if (action == MotionEvent.ACTION_MOVE) {
            if (Math.abs(y - mTouchPoint.y) > mTouchSlop) {

                // cancel sequence on touch target
                MotionEvent event = MotionEvent.obtain(ev);
                event.setAction(MotionEvent.ACTION_CANCEL);
                mTouchTarget.dispatchTouchEvent(event);
                event.recycle();

                // provide correct sequence to super class for further handling
                super.dispatchTouchEvent(mDownEvent);
                super.dispatchTouchEvent(ev);
                clearTouchTarget();

            }
        }

        return true;
    }

    // call super if this was not our pinned view
    return super.dispatchTouchEvent(ev);
}
 
Example 16
Source File: SuggestionStripView.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(final MotionEvent me) {
    if (!mMoreSuggestionsView.isShowingInParent()) {
        // Ignore any touch event while more suggestions panel hasn't been shown.
        // Detecting sliding up is done at {@link #onInterceptTouchEvent}.
        return true;
    }
    // In the sliding input mode. {@link MotionEvent} should be forwarded to
    // {@link MoreSuggestionsView}.
    final int index = me.getActionIndex();
    final int x = mMoreSuggestionsView.translateX((int)me.getX(index));
    final int y = mMoreSuggestionsView.translateY((int)me.getY(index));
    me.setLocation(x, y);
    if (!mNeedsToTransformTouchEventToHoverEvent) {
        mMoreSuggestionsView.onTouchEvent(me);
        return true;
    }
    // In sliding suggestion mode with accessibility mode on, a touch event should be
    // transformed to a hover event.
    final int width = mMoreSuggestionsView.getWidth();
    final int height = mMoreSuggestionsView.getHeight();
    final boolean onMoreSuggestions = (x >= 0 && x < width && y >= 0 && y < height);
    if (!onMoreSuggestions && !mIsDispatchingHoverEventToMoreSuggestions) {
        // Just drop this touch event because dispatching hover event isn't started yet and
        // the touch event isn't on {@link MoreSuggestionsView}.
        return true;
    }
    final int hoverAction;
    if (onMoreSuggestions && !mIsDispatchingHoverEventToMoreSuggestions) {
        // Transform this touch event to a hover enter event and start dispatching a hover
        // event to {@link MoreSuggestionsView}.
        mIsDispatchingHoverEventToMoreSuggestions = true;
        hoverAction = MotionEvent.ACTION_HOVER_ENTER;
    } else if (me.getActionMasked() == MotionEvent.ACTION_UP) {
        // Transform this touch event to a hover exit event and stop dispatching a hover event
        // after this.
        mIsDispatchingHoverEventToMoreSuggestions = false;
        mNeedsToTransformTouchEventToHoverEvent = false;
        hoverAction = MotionEvent.ACTION_HOVER_EXIT;
    } else {
        // Transform this touch event to a hover move event.
        hoverAction = MotionEvent.ACTION_HOVER_MOVE;
    }
    me.setAction(hoverAction);
    mMoreSuggestionsView.onHoverEvent(me);
    return true;
}
 
Example 17
Source File: TouchInterceptionFrameLayout.java    From Android-ObservableScrollView with Apache License 2.0 4 votes vote down vote up
private MotionEvent obtainMotionEvent(MotionEvent base, int action) {
    MotionEvent ev = MotionEvent.obtainNoHistory(base);
    ev.setAction(action);
    return ev;
}
 
Example 18
Source File: StickyNavLayout.java    From SprintNBA with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(event);
    int action = event.getAction();
    float y = event.getY();

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            if (!mScroller.isFinished())
                mScroller.abortAnimation();
            mLastY = y;
            return true;
        case MotionEvent.ACTION_MOVE:
            float dy = y - mLastY;

            if (!mDragging && Math.abs(dy) > mTouchSlop) {
                mDragging = true;
            }
            if (mDragging) {
                scrollBy(0, (int) -dy);
                // 如果topView隐藏,且上滑动时,则改变当前事件为ACTION_DOWN
                if (getScrollY() == mTopViewHeight && dy < 0) {
                    event.setAction(MotionEvent.ACTION_DOWN);
                    dispatchTouchEvent(event);
                    isInControl = false;
                    isSticky = true;
                } else {
                    isSticky = false;
                }
            }
            mLastY = y;
            break;
        case MotionEvent.ACTION_CANCEL:
            mDragging = false;
            recycleVelocityTracker();
            if (!mScroller.isFinished()) {
                mScroller.abortAnimation();
            }
            break;
        case MotionEvent.ACTION_UP:
            mDragging = false;
            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityY = (int) mVelocityTracker.getYVelocity();
            if (Math.abs(velocityY) > mMinimumVelocity) {
                fling(-velocityY);
            }
            recycleVelocityTracker();
            break;
    }

    return super.onTouchEvent(event);
}
 
Example 19
Source File: SwipeItemLayout.java    From PocketEOS-Android with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 判断是否可以拖拽View
 */
@SuppressLint("RtlHardcoded")
private void checkCanDragged(MotionEvent ev) {
    if (mIsDragged) {
        return;
    }

    float dx = ev.getX() - mDownX;
    float dy = ev.getY() - mDownY;
    boolean isRightDrag = dx > mTouchSlop && dx > Math.abs(dy);
    boolean isLeftDrag = dx < -mTouchSlop && Math.abs(dx) > Math.abs(dy);

    if (mIsOpen) {
        // 开启状态下,点击在content上就捕获事件,点击在菜单上则判断touchSlop
        int downX = (int) mDownX;
        int downY = (int) mDownY;
        if (isTouchContent(downX, downY)) {
            mIsDragged = true;
        } else if (isTouchMenu(downX, downY)) {
            mIsDragged = (isLeftMenu() && isLeftDrag) || (isRightMenu() && isRightDrag);
        }

    } else {
        // 关闭状态,获取当前即将要开启的菜单。
        if (isRightDrag) {
            mCurrentMenu = mMenus.get(Gravity.LEFT);
            mIsDragged = mCurrentMenu != null;
        } else if (isLeftDrag) {
            mCurrentMenu = mMenus.get(Gravity.RIGHT);
            mIsDragged = mCurrentMenu != null;
        }
    }

    if (mIsDragged) {
        // 开始拖动后,分发down事件给DragHelper,并且发送一个cancel取消点击事件
        MotionEvent obtain = MotionEvent.obtain(ev);
        obtain.setAction(MotionEvent.ACTION_DOWN);
        mDragHelper.processTouchEvent(obtain);
        if (getParent() != null) {
            // 解决和父控件的滑动冲突。
            getParent().requestDisallowInterceptTouchEvent(true);
        }
    }
}
 
Example 20
Source File: OverlayPanelEventFilter.java    From delion with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@link MotionEvent} inheriting from a given |e| event.
 * @param e The {@link MotionEvent} to inherit properties from.
 * @param action The MotionEvent's Action to be used.
 * @return A new {@link MotionEvent}.
 */
private MotionEvent copyEvent(MotionEvent e, int action) {
    MotionEvent event = MotionEvent.obtain(e);
    event.setAction(action);
    return event;
}