android.support.v4.view.MotionEventCompat Java Examples

The following examples show how to use android.support.v4.view.MotionEventCompat. 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: DragSelectTouchListener.java    From DragSelectRecyclerView with Apache License 2.0 6 votes vote down vote up
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e)
{
    if (!mIsActive)
        return;

    int action = MotionEventCompat.getActionMasked(e);
    switch (action)
    {
        case MotionEvent.ACTION_MOVE:
            if (!mInTopSpot && !mInBottomSpot)
                updateSelectedRange(rv, e);
            processAutoScroll(e);
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
            reset();
            break;
    }
}
 
Example #2
Source File: SlideLayout.java    From SlideActivity with MIT License 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!mCanSlide) return super.onTouchEvent(ev);

    mDragHelper.processTouchEvent(ev);

    final int action = ev.getAction();
    boolean wantTouchEvents = true;

    switch (action & MotionEventCompat.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN: {
            final float x = ev.getX();
            final float y = ev.getY();
            mInitialMotionX = x;
            mInitialMotionY = y;
            break;
        }

        case MotionEvent.ACTION_UP: {
            break;
        }
    }

    return wantTouchEvents;
}
 
Example #3
Source File: SuperSwipeRefreshLayout.java    From Ticket-Analysis with MIT License 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }
    if (!isEnabled() || mReturningToStart
            || (!isChildScrollToTop() && !isChildScrollToBottom())) {
        // 如果子View可以滑动,不拦截事件,交给子View处理
        return false;
    }

    if (isChildScrollToBottom()) {// 上拉加载更多
        return handlerPushTouchEvent(ev, action);
    } else {// 下拉刷新
        return handlerPullTouchEvent(ev, action);
    }
}
 
Example #4
Source File: NestedScrollView.java    From MyBlogDemo with Apache License 2.0 6 votes vote down vote up
private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = (ev.getAction() & MotionEventCompat.ACTION_POINTER_INDEX_MASK) >>
            MotionEventCompat.ACTION_POINTER_INDEX_SHIFT;
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        // TODO: Make this decision more intelligent.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastMotionY = (int) MotionEventCompat.getY(ev, newPointerIndex);
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();
        }
    }
}
 
Example #5
Source File: AutoScrollViewPager.java    From AutoScrollViewPager with Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    int action = MotionEventCompat.getActionMasked(ev);
    if (stopWhenTouch) {
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                if (isAutoScroll) {
                    isStopedWhenTouch = true;
                    stopAutoScroll();
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_OUTSIDE:
                if (isStopedWhenTouch) {
                    startAutoScroll();
                }
                break;
        }
    }
    return super.dispatchTouchEvent(ev);
}
 
Example #6
Source File: ItemTouchHelper.java    From letv with Apache License 2.0 6 votes vote down vote up
private ViewHolder findSwipedView(MotionEvent motionEvent) {
    LayoutManager lm = this.mRecyclerView.getLayoutManager();
    if (this.mActivePointerId == -1) {
        return null;
    }
    int pointerIndex = MotionEventCompat.findPointerIndex(motionEvent, this.mActivePointerId);
    float dy = MotionEventCompat.getY(motionEvent, pointerIndex) - this.mInitialTouchY;
    float absDx = Math.abs(MotionEventCompat.getX(motionEvent, pointerIndex) - this.mInitialTouchX);
    float absDy = Math.abs(dy);
    if (absDx < ((float) this.mSlop) && absDy < ((float) this.mSlop)) {
        return null;
    }
    if (absDx > absDy && lm.canScrollHorizontally()) {
        return null;
    }
    if (absDy > absDx && lm.canScrollVertically()) {
        return null;
    }
    View child = findChildView(motionEvent);
    if (child != null) {
        return this.mRecyclerView.getChildViewHolder(child);
    }
    return null;
}
 
Example #7
Source File: LoopViewPager.java    From MVVM-JueJin with MIT License 6 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    int action = MotionEventCompat.getActionMasked(ev);
    if (stopScrollWhenTouch) {
        if ((action == MotionEvent.ACTION_DOWN)) {
            if (mOnTouchListening!=null){
                mOnTouchListening.onTouchCancleTimer(true);
            }
        } else if (ev.getAction() == MotionEvent.ACTION_UP ) {
            if (mOnTouchListening!=null){
                mOnTouchListening.onTouchCancleTimer(false);
            }
        }
    }
    return super.dispatchTouchEvent(ev);
}
 
Example #8
Source File: ShrinkButton.java    From IndicatorBox with MIT License 6 votes vote down vote up
@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (MotionEventCompat.getActionMasked(event)){
        case MotionEvent.ACTION_DOWN:
            if (!isWindowFocused){
                return true;
            }
            if (animationState == STATE_EXPANDED){
                startWholeAnimation();
            }
            this.callOnClick();
            return true;
        case MotionEvent.ACTION_UP:

            return false;
    }
    return false;
}
 
Example #9
Source File: CustomViewAbove.java    From BigApp_WordPress_Android with Apache License 2.0 6 votes vote down vote up
private void determineDrag(MotionEvent ev) {
	final int activePointerId = mActivePointerId;
	final int pointerIndex = getPointerIndex(ev, activePointerId);
	if (activePointerId == INVALID_POINTER || pointerIndex == INVALID_POINTER)
		return;
	final float x = MotionEventCompat.getX(ev, pointerIndex);
	final float dx = x - mLastMotionX;
	final float xDiff = Math.abs(dx);
	final float y = MotionEventCompat.getY(ev, pointerIndex);
	final float dy = y - mLastMotionY;
	final float yDiff = Math.abs(dy);
	if (xDiff > (isMenuOpen()?mTouchSlop/2:mTouchSlop) && xDiff > yDiff && thisSlideAllowed(dx)) {		
		startDrag();
		mLastMotionX = x;
		mLastMotionY = y;
		setScrollingCacheEnabled(true);
		// TODO add back in touch slop check
	} else if (xDiff > mTouchSlop) {
		mIsUnableToDrag = true;
	}
}
 
Example #10
Source File: AutoScrollViewPager.java    From android-auto-scroll-viewpager with Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
	int action = MotionEventCompat.getActionMasked(ev);
	
	if (action == MotionEvent.ACTION_DOWN)
	{
		stopScroll();
	}
	else if (ev.getAction() == MotionEvent.ACTION_UP)
	{
		resumeScroll();
	}
	
	this.getParent().requestDisallowInterceptTouchEvent(true);
	
	return super.dispatchTouchEvent(ev);
}
 
Example #11
Source File: CustomViewAbove.java    From appcan-android with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void determineDrag(MotionEvent ev) {
    final int activePointerId = mActivePointerId;
    final int pointerIndex = getPointerIndex(ev, activePointerId);
    if (activePointerId == INVALID_POINTER || pointerIndex == INVALID_POINTER)
        return;
    final float x = MotionEventCompat.getX(ev, pointerIndex);
    final float dx = x - mLastMotionX;
    final float xDiff = Math.abs(dx);
    final float y = MotionEventCompat.getY(ev, pointerIndex);
    final float dy = y - mLastMotionY;
    final float yDiff = Math.abs(dy);
    if (xDiff > (isMenuOpen() ? mTouchSlop / 2 : mTouchSlop) && xDiff > yDiff && thisSlideAllowed(dx)) {
        startDrag();
        mLastMotionX = x;
        mLastMotionY = y;
        setScrollingCacheEnabled(true);
        // TODO add back in touch slop check
    } else if (xDiff > mTouchSlop) {
        mIsUnableToDrag = true;
    }
}
 
Example #12
Source File: LoopViewPager.java    From Android-Application-ZJB with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (MotionEventCompat.getActionMasked(ev)) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
        case MotionEventCompat.ACTION_POINTER_DOWN:
            mHandler.removeMessages(AUTO_SCROLL_MESSAGE);
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_POINTER_2_UP:
        case MotionEvent.ACTION_POINTER_3_UP:
        case MotionEvent.ACTION_CANCEL:
            mHandler.removeMessages(AUTO_SCROLL_MESSAGE);
            mHandler.sendEmptyMessageDelayed(AUTO_SCROLL_MESSAGE, delayTimeInMills);
            break;
        default:
            break;
    }
    return super.onTouchEvent(ev);
}
 
Example #13
Source File: ViewDragHelper.java    From android-recipes-app with Apache License 2.0 5 votes vote down vote up
private void saveLastMotion(MotionEvent ev) {
    final int pointerCount = MotionEventCompat.getPointerCount(ev);
    for (int i = 0; i < pointerCount; i++) {
        final int pointerId = MotionEventCompat.getPointerId(ev, i);
        final float x = MotionEventCompat.getX(ev, i);
        final float y = MotionEventCompat.getY(ev, i);
        mLastMotionX[pointerId] = x;
        mLastMotionY[pointerId] = y;
    }
}
 
Example #14
Source File: NestedScrollView.java    From letv with Apache License 2.0 5 votes vote down vote up
private void onSecondaryPointerUp(MotionEvent ev) {
    int pointerIndex = (ev.getAction() & MotionEventCompat.ACTION_POINTER_INDEX_MASK) >> 8;
    if (MotionEventCompat.getPointerId(ev, pointerIndex) == this.mActivePointerId) {
        int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        this.mLastMotionY = (int) MotionEventCompat.getY(ev, newPointerIndex);
        this.mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        if (this.mVelocityTracker != null) {
            this.mVelocityTracker.clear();
        }
    }
}
 
Example #15
Source File: ViewDragHelper.java    From photo-editor-android with MIT License 5 votes vote down vote up
private void saveLastMotion(MotionEvent ev) {
    final int pointerCount = MotionEventCompat.getPointerCount(ev);
    for (int i = 0; i < pointerCount; i++) {
        final int pointerId = MotionEventCompat.getPointerId(ev, i);
        final float x = MotionEventCompat.getX(ev, i);
        final float y = MotionEventCompat.getY(ev, i);
        // Sometimes we can try and save last motion for a pointer never recorded in initial motion. In this case we just discard it.
        if (mLastMotionX != null && mLastMotionY != null
                && mLastMotionX.length > pointerId && mLastMotionY.length > pointerId) {
            mLastMotionX[pointerId] = x;
            mLastMotionY[pointerId] = y;
        }
    }
}
 
Example #16
Source File: LingjuSwipeUpLoadRefreshLayout.java    From AssistantBySDK with Apache License 2.0 5 votes vote down vote up
private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
    }
}
 
Example #17
Source File: VerticalDrawerLayout.java    From MeiZiNews with MIT License 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    mDragHelper.processTouchEvent(ev);

    final int action = ev.getAction();
    switch (action & MotionEventCompat.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN: {

            break;
        }
        case MotionEvent.ACTION_MOVE: {


            return false;
        }
        case MotionEvent.ACTION_UP: {
            final float x = ev.getX();
            final float y = ev.getY();
            final View touchedView = mDragHelper.findTopChildUnder((int) x, (int) y);

            if (touchedView == null) {
                return false;
            }

            if (isContentView(touchedView)) {
                if (mContentScrimOpacity > 0) {
                    closeDrawer();
                }
            }

            break;
        }
        case MotionEvent.ACTION_CANCEL: {
            break;
        }
    }

    return true;
}
 
Example #18
Source File: SheetBehavior.java    From AndroidSweetBehavior with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
    if (!child.isShown()) {
        return false;
    }
    int action = MotionEventCompat.getActionMasked(event);
    if (mState == STATE_DRAGGING && action == MotionEvent.ACTION_DOWN) {
        return true;
    }
    mViewDragHelper.processTouchEvent(event);
    // Record the velocity
    if (action == MotionEvent.ACTION_DOWN) {
        reset();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    // The ViewDragHelper tries to capture only the top-most View. We have to explicitly tell it
    // to capture the bottom sheet in case it is not captured and the touch slop is passed.
    if (action == MotionEvent.ACTION_MOVE) {


        if (mSlideHelper.canScrollHorizontally() != 0 && Math.abs(mInitialY - event.getX()) > mViewDragHelper.getTouchSlop()) {
            mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
        } else if (mSlideHelper.canScrollVertically() != 0 && Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop()) {
            mViewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
        }
    }
    return true;
}
 
Example #19
Source File: DrawerLayout.java    From letv with Apache License 2.0 5 votes vote down vote up
public boolean onInterceptTouchEvent(MotionEvent ev) {
    boolean interceptForDrag = this.mLeftDragger.shouldInterceptTouchEvent(ev) | this.mRightDragger.shouldInterceptTouchEvent(ev);
    boolean interceptForTap = false;
    switch (MotionEventCompat.getActionMasked(ev)) {
        case 0:
            float x = ev.getX();
            float y = ev.getY();
            this.mInitialMotionX = x;
            this.mInitialMotionY = y;
            if (this.mScrimOpacity > 0.0f) {
                View child = this.mLeftDragger.findTopChildUnder((int) x, (int) y);
                if (child != null && isContentView(child)) {
                    interceptForTap = true;
                }
            }
            this.mDisallowInterceptRequested = false;
            this.mChildrenCanceledTouch = false;
            break;
        case 1:
        case 3:
            closeDrawers(true);
            this.mDisallowInterceptRequested = false;
            this.mChildrenCanceledTouch = false;
            break;
        case 2:
            if (this.mLeftDragger.checkTouchSlop(3)) {
                this.mLeftCallback.removeCallbacks();
                this.mRightCallback.removeCallbacks();
                break;
            }
            break;
    }
    if (interceptForDrag || interceptForTap || hasPeekingDrawer() || this.mChildrenCanceledTouch) {
        return true;
    }
    return false;
}
 
Example #20
Source File: ViewDragHelper.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
private void saveLastMotion(MotionEvent ev) {
    final int pointerCount = MotionEventCompat.getPointerCount(ev);
    for (int i = 0; i < pointerCount; i++) {
        final int pointerId = MotionEventCompat.getPointerId(ev, i);
        final float x = MotionEventCompat.getX(ev, i);
        final float y = MotionEventCompat.getY(ev, i);
        mLastMotionX[pointerId] = x;
        mLastMotionY[pointerId] = y;
    }
}
 
Example #21
Source File: SuperSwipeRefreshLayout.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
float getMotionEventY(MotionEvent ev, int activePointerId) {
     int index = MotionEventCompat.findPointerIndex(ev,
            activePointerId);
    if (index < 0) {
        return -1;
    }
    return MotionEventCompat.getY(ev, index);
}
 
Example #22
Source File: BaseItemDraggableAdapter.java    From demo4Fish with MIT License 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 #23
Source File: ListBuddiesAutoScrollHelper.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouch(View v, MotionEvent event) {

    final int action = MotionEventCompat.getActionMasked(event);
    switch (action) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            return false;
    }

    return super.onTouch(v,event);
}
 
Example #24
Source File: SlidingLayout.java    From stynico with MIT License 5 votes vote down vote up
private float getMotionEventY(MotionEvent ev, int activePointerId) {
    final int index = MotionEventCompat.findPointerIndex(ev, activePointerId);
    if (index < 0) {
        return -1;
    }
    return MotionEventCompat.getY(ev, index);
}
 
Example #25
Source File: HorizontalIconView.java    From auid2 with Apache License 2.0 5 votes vote down vote up
private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mPreviousX = ev.getX(newPointerIndex);
        mActivePointerId = ev.getPointerId(newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();
        }
    }
}
 
Example #26
Source File: DragLayout.java    From android-youtube-drag-layout with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mDragHelper.cancel();
        return false;
    }
    return mDragHelper.shouldInterceptTouchEvent(ev);
}
 
Example #27
Source File: CustomViewAbove.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
private void onSecondaryPointerUp(MotionEvent ev) {
	if (DEBUG) Log.v(TAG, "onSecondaryPointerUp called");
	final int pointerIndex = MotionEventCompat.getActionIndex(ev);
	final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
	if (pointerId == mActivePointerId) {
		// This was our active pointer going up. Choose a new
		// active pointer and adjust accordingly.
		final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
		mLastMotionX = MotionEventCompat.getX(ev, newPointerIndex);
		mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
		if (mVelocityTracker != null) {
			mVelocityTracker.clear();
		}
	}
}
 
Example #28
Source File: ViewDragHelper.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 5 votes vote down vote up
private void saveLastMotion(MotionEvent ev) {
    final int pointerCount = MotionEventCompat.getPointerCount(ev);
    for (int i = 0; i < pointerCount; i++) {
        final int pointerId = MotionEventCompat.getPointerId(ev, i);
        final float x = MotionEventCompat.getX(ev, i);
        final float y = MotionEventCompat.getY(ev, i);
        mLastMotionX[pointerId] = x;
        mLastMotionY[pointerId] = y;
    }
}
 
Example #29
Source File: DiscreteSeekBar.java    From Panoramic-Screenshot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled()) {
        return false;
    }
    int actionMasked = MotionEventCompat.getActionMasked(event);
    switch (actionMasked) {
        case MotionEvent.ACTION_DOWN:
            mDownX = event.getX();
            startDragging(event, isInScrollingContainer());
            break;
        case MotionEvent.ACTION_MOVE:
            if (isDragging()) {
                updateDragging(event);
            } else {
                final float x = event.getX();
                if (Math.abs(x - mDownX) > mTouchSlop) {
                    startDragging(event, false);
                }
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            stopDragging();
            break;
    }
    return true;
}
 
Example #30
Source File: RadialTimePickerView.java    From AppCompat-Extension-Library with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!mInputEnabled) {
        return true;
    }

    final int action = MotionEventCompat.getActionMasked(event);
    if (action == MotionEvent.ACTION_MOVE
            || action == MotionEvent.ACTION_UP
            || action == MotionEvent.ACTION_DOWN) {
        boolean forceSelection = false;
        boolean autoAdvance = false;

        if (action == MotionEvent.ACTION_DOWN) {
            // This is a new event stream, reset whether the value changed.
            mChangedDuringTouch = false;
        } else if (action == MotionEvent.ACTION_UP) {
            autoAdvance = true;

            // If we saw a down/up pair without the value changing, assume
            // this is a single-tap selection and force a change.
            if (!mChangedDuringTouch) {
                forceSelection = true;
            }
        }

        mChangedDuringTouch |= handleTouchInput(
                event.getX(), event.getY(), forceSelection, autoAdvance);
    }

    return true;
}