Java Code Examples for android.support.v4.view.MotionEventCompat#getActionMasked()

The following examples show how to use android.support.v4.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: BubbleAnimationLayout.java    From BubbleAnimationLayout with MIT License 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (mContextView != null && mContextView.getVisibility() == VISIBLE) {
        return false;
    }
    int action = MotionEventCompat.getActionMasked(event);

    if (action == MotionEvent.ACTION_DOWN) {
        if (event.getX() < getPaddingLeft() + mIndicatorWidth * 3) {
            mDownPositionX = event.getX();
            return true;
        }
    } else if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        if (mAnimationView != null && event.getX() - mDownPositionX > getWidth() / 10 && mAnimationView.getVisibility() != VISIBLE) {
            startViewAnimation(mDefaultShowAnimator);
            return true;
        }
    }
    return super.onTouchEvent(event);
}
 
Example 2
Source File: ParticleHeartView.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 (onClickListener != null) {
                onClickListener.onClick(v);
            }
            startAnimation();
            callOnClick();
            return true;
        case MotionEvent.ACTION_MOVE:
            return true;
        case MotionEvent.ACTION_UP:

            return false;
    }
    return false;
}
 
Example 3
Source File: VerticalDrawerLayout.java    From MeiZiNews with MIT License 6 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    boolean interceptForDrag = mDragHelper.shouldInterceptTouchEvent(ev);
    boolean interceptForTap = false;

    final int action = MotionEventCompat.getActionMasked(ev);
    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            final float x = ev.getX();
            final float y = ev.getY();

            final View touchedView = mDragHelper.findTopChildUnder((int) x, (int) y);
            if (isContentView(touchedView)) {
                if (mContentScrimOpacity > 0) {
                    interceptForTap = true;
                }
            }

            break;
        }
    }


    return interceptForDrag || interceptForTap;
}
 
Example 4
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 5
Source File: GestureCreator.java    From DrawableView with Apache License 2.0 6 votes vote down vote up
public void onTouchEvent(MotionEvent event) {
  float touchX = (MotionEventCompat.getX(event, 0) + viewRect.left) / scaleFactor;
  float touchY = (MotionEventCompat.getY(event, 0) + viewRect.top) / scaleFactor;

  //Log.d("Drawer", "T[" + touchX + "," + touchY + "] V[" + viewRect.toShortString() + "] S[" + scaleFactor + "]");
  switch (MotionEventCompat.getActionMasked(event)) {
    case MotionEvent.ACTION_DOWN:
      actionDown(touchX, touchY);
      break;
    case MotionEvent.ACTION_MOVE:
      actionMove(touchX, touchY);
      break;
    case MotionEvent.ACTION_UP:
      actionUp();
      break;
    case MotionEventCompat.ACTION_POINTER_DOWN:
      actionPointerDown();
      break;
  }
}
 
Example 6
Source File: SuperSwipeRefreshLayout.java    From AutoRecycleView with Apache License 2.0 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 7
Source File: PullToRefreshView.java    From Bailan with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            return true;
        case MotionEvent.ACTION_MOVE:
            if (!isPullDownEnable && deltaY > 0 && getScrollY() <= 0) {
                break;
            }

            if (!isPullUpEnable && deltaY < 0 && getScrollY() >= 0) {
                break;
            }

            if (isNeedIntercept) {
                if (mStatus == PULL_DOWN && getScrollY() > 0) {
                    break;
                }
                if (mStatus == PULL_UP && getScrollY() < 0) {
                    break;
                }
                scrollBy(0, (int) (-getMoveFloat(yVelocity, deltaY)));

                updateIndicator();
            } else {
                ev.setAction(MotionEvent.ACTION_DOWN);
                dispatchTouchEvent(ev);
                isInControl = false;
            }
            break;
        case MotionEvent.ACTION_UP:
            autoBackToPosition();
            return true;
    }

    return true;
}
 
Example 8
Source File: ToolbarPanelLayout.java    From ToolbarPanel with MIT License 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) {
        dragHelper.cancel();
        return false;
    }
    return dragHelper.shouldInterceptTouchEvent(ev);
}
 
Example 9
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 10
Source File: DottedGridView.java    From mirror with Apache License 2.0 5 votes vote down vote up
/**
 * Override method to intercept only touch events over the drag view and to cancel the drag when
 * the action associated to the MotionEvent is equals to ACTION_CANCEL or ACTION_UP.
 *
 * @param ev captured.
 * @return true if the view is going to process the touch event or false if not.
 */
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        return false;
    }

    mDraggedView = null;
    stream(mViews)
            .filter(v -> isViewHit(v, (int) ev.getX(), (int) ev.getY()))
            .forEach((Action1<BorderView>) v -> mDraggedView = v);

    final int actionMasked = MotionEventCompat.getActionMasked(ev);
    switch (actionMasked & MotionEventCompat.ACTION_MASK) {
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            hideDraggedViewBorder();
            mViewDragHelper.cancel();
            return false;
        case MotionEvent.ACTION_DOWN:
            showDraggedViewBorder();
            mViewDragHelper.processTouchEvent(ev);
            return false;
        default:
            break;
    }
    return mViewDragHelper.shouldInterceptTouchEvent(ev) ||
            mViewDragHelper.isViewUnder(mDraggedView, (int) ev.getX(), (int) ev.getY());
}
 
Example 11
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;
}
 
Example 12
Source File: AutoScrollHelper.java    From android-recipes-app with Apache License 2.0 5 votes vote down vote up
/**
 * Handles touch events by activating automatic scrolling, adjusting scroll
 * velocity, or stopping.
 * <p>
 * If {@link #isExclusive()} is false, always returns false so that
 * the host view may handle touch events. Otherwise, returns true when
 * automatic scrolling is active and false otherwise.
 */
@Override
public boolean onTouch(View v, MotionEvent event) {
    if (!mEnabled) {
        return false;
    }

    final int action = MotionEventCompat.getActionMasked(event);
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mNeedsCancel = true;
            mAlreadyDelayed = false;
            // $FALL-THROUGH$
        case MotionEvent.ACTION_MOVE:
            final float xTargetVelocity = computeTargetVelocity(
                    HORIZONTAL, event.getX(), v.getWidth(), mTarget.getWidth());
            final float yTargetVelocity = computeTargetVelocity(
                    VERTICAL, event.getY(), v.getHeight(), mTarget.getHeight());
            mScroller.setTargetVelocity(xTargetVelocity, yTargetVelocity);

            // If the auto scroller was not previously active, but it should
            // be, then update the state and start animations.
            if (!mAnimating && shouldAnimate()) {
                startAnimating();
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            requestStop();
            break;
    }

    return mExclusive && mAnimating;
}
 
Example 13
Source File: UIListView.java    From Auie with GNU General Public License v2.0 4 votes vote down vote up
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent ev) {
	if (ev.getAction() != MotionEvent.ACTION_DOWN && mTouchView == null){
		return super.onTouchEvent(ev);			
	}
	int action = MotionEventCompat.getActionMasked(ev);
	action = ev.getAction();
	switch (action) {
	case MotionEvent.ACTION_DOWN:
		int oldPos = mTouchPosition;
		lastY = ev.getRawY();
		pressX = ev.getX();
		pressY = ev.getY();
		mTouchState = TOUCH_STATE_NONE;
		mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
		if (mTouchPosition == oldPos && mTouchView != null && mTouchView.isShow()) {
			mTouchState = TOUCH_STATE_X;
			mTouchView.onSwipe(ev);
			return true;
		}

		View view = getChildAt(mTouchPosition - getFirstVisiblePosition());

		if (mTouchView != null && mTouchView.isShow()) {
			mTouchView.smoothCloseMenu();
			mTouchView = null;
			return super.onTouchEvent(ev);
		}
		if (view instanceof MenuLayout) {
			mTouchView = (MenuLayout) view;
		}
		if (mTouchView != null) {
			mTouchView.onSwipe(ev);
		}
		break;
	case MotionEvent.ACTION_MOVE:
		float dy = Math.abs((ev.getY() - pressY));
		float dx = Math.abs((ev.getX() - pressX));
		if (mTouchState == TOUCH_STATE_X) {
			if (mTouchView != null) {
				mTouchView.onSwipe(ev);
			}
			getSelector().setState(new int[] { 0 });
			ev.setAction(MotionEvent.ACTION_CANCEL);
			super.onTouchEvent(ev);
			return true;
		} else if (mTouchState == TOUCH_STATE_NONE) {
			if (Math.abs(dy) > DISTANCE_Y) {
				mTouchState = TOUCH_STATE_Y;
			} else if (dx > DISTANCE_X) {
				mTouchState = TOUCH_STATE_X;
			}
		}
		final float deltaY = ev.getRawY() - lastY;
		lastY = ev.getRawY();
		if (isCanRefresh() && getFirstVisiblePosition() == 0 && (headerView.getVisiableHeight() > 0 || deltaY > 0)) {
			updateHeaderHeight(deltaY / OFFSET_RADIO);
			invokeOnScrolling();
		} else if (isCanLoadmore() && getLastVisiblePosition() == totalItemCount - 1 && (footerView.getBottomMargin() > 0 || deltaY < 0)) {
			updateFooterHeight(-deltaY / OFFSET_RADIO);
		}
		break;
	case MotionEvent.ACTION_UP:
	default:
		if (mTouchState == TOUCH_STATE_X) {
			onTouchHorizontal(ev);
			ev.setAction(MotionEvent.ACTION_CANCEL);
			super.onTouchEvent(ev);
			return true;
		}
		if (getFirstVisiblePosition() == 0) {
			if (isCanRefresh() && headerView.getVisiableHeight() > headerViewHeight) {
				refreshing = true;
				headerView.setState(HeaderView.STATE_REFRESHING);
				if (listViewListener != null) {
					listViewListener.onRefresh();
				}
			}
			resetHeaderHeight();
		}
		if (isCanLoadmore() && getLastVisiblePosition() == totalItemCount - 1) {
			if ((type == TYPE_ONLY_UP_LOADMORD || type == TYPE_BOTH) && footerView.getBottomMargin() > PULL_LOAD_MORE_DELTA) {
				startLoadMore();
			}
			resetFooterHeight();
		}
		break;
	}
	return super.onTouchEvent(ev);
}
 
Example 14
Source File: ItemTouchHelper.java    From letv with Apache License 2.0 4 votes vote down vote up
public void onTouchEvent(RecyclerView recyclerView, MotionEvent event) {
    int newPointerIndex = 0;
    ItemTouchHelper.this.mGestureDetector.onTouchEvent(event);
    if (ItemTouchHelper.this.mVelocityTracker != null) {
        ItemTouchHelper.this.mVelocityTracker.addMovement(event);
    }
    if (ItemTouchHelper.this.mActivePointerId != -1) {
        int action = MotionEventCompat.getActionMasked(event);
        int activePointerIndex = MotionEventCompat.findPointerIndex(event, ItemTouchHelper.this.mActivePointerId);
        if (activePointerIndex >= 0) {
            ItemTouchHelper.this.checkSelectForSwipe(action, event, activePointerIndex);
        }
        ViewHolder viewHolder = ItemTouchHelper.this.mSelected;
        if (viewHolder != null) {
            switch (action) {
                case 1:
                    break;
                case 2:
                    if (activePointerIndex >= 0) {
                        ItemTouchHelper.this.updateDxDy(event, ItemTouchHelper.this.mSelectedFlags, activePointerIndex);
                        ItemTouchHelper.this.moveIfNecessary(viewHolder);
                        ItemTouchHelper.this.mRecyclerView.removeCallbacks(ItemTouchHelper.this.mScrollRunnable);
                        ItemTouchHelper.this.mScrollRunnable.run();
                        ItemTouchHelper.this.mRecyclerView.invalidate();
                        return;
                    }
                    return;
                case 3:
                    if (ItemTouchHelper.this.mVelocityTracker != null) {
                        ItemTouchHelper.this.mVelocityTracker.clear();
                        break;
                    }
                    break;
                case 6:
                    int pointerIndex = MotionEventCompat.getActionIndex(event);
                    if (MotionEventCompat.getPointerId(event, pointerIndex) == ItemTouchHelper.this.mActivePointerId) {
                        if (pointerIndex == 0) {
                            newPointerIndex = 1;
                        }
                        ItemTouchHelper.this.mActivePointerId = MotionEventCompat.getPointerId(event, newPointerIndex);
                        ItemTouchHelper.this.updateDxDy(event, ItemTouchHelper.this.mSelectedFlags, pointerIndex);
                        return;
                    }
                    return;
                default:
                    return;
            }
            ItemTouchHelper.this.select(null, 0);
            ItemTouchHelper.this.mActivePointerId = -1;
        }
    }
}
 
Example 15
Source File: SlidingUpPanelLayout.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);


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

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mDragHelper.cancel();
        return false;
    }

    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();

            // Handle any horizontal scrolling on the drag view.
            if (mIsUsingDragViewTouchEvents && adx > dragSlop && ady < dragSlop) {
                return super.onInterceptTouchEvent(ev);
            }

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

    return mDragHelper.shouldInterceptTouchEvent(ev);
}
 
Example 16
Source File: SlidingUpPanelLayout.java    From AntennaPodSP with MIT License 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    if (!mCanSlide || !mIsSlidingEnabled || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
        mDragHelper.cancel();
        return super.onInterceptTouchEvent(ev);
    }

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mDragHelper.cancel();
        return false;
    }

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

    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            mIsUnableToDrag = false;
            mInitialMotionX = x;
            mInitialMotionY = y;
            if (isDragViewUnder((int) x, (int) y) && !mIsUsingDragViewTouchEvents) {
                interceptTap = true;
            }
            break;
        }

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

            // Handle any horizontal scrolling on the drag view.
            if (mIsUsingDragViewTouchEvents) {
                if (adx > mScrollTouchSlop && ady < mScrollTouchSlop) {
                    return super.onInterceptTouchEvent(ev);
                }
                // Intercept the touch if the drag view has any vertical scroll.
                // onTouchEvent will determine if the view should drag vertically.
                else if (ady > mScrollTouchSlop) {
                    interceptTap = isDragViewUnder((int) x, (int) y);
                }
            }

            if ((ady > dragSlop && adx > ady) || !isDragViewUnder((int) x, (int) y)) {
                mDragHelper.cancel();
                mIsUnableToDrag = true;
                return false;
            }
            break;
        }
    }

    final boolean interceptForDrag = mDragHelper.shouldInterceptTouchEvent(ev);

    return interceptForDrag || interceptTap;
}
 
Example 17
Source File: PullToRefreshView.java    From PullToRefresh with MIT License 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (!isEnabled() || canChildScrollUp() || mRefreshing)
        return false;

    final int action = MotionEventCompat.getActionMasked(ev);

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            setTargetOffsetTop(0, true);
            mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
            mIsBeingDragged = false;
            final float initialMotionY = getMotionEventY(ev, mActivePointerId);
            if (initialMotionY == -1)
                return false;
            mInitialMotionY = initialMotionY;
            break;
        case MotionEvent.ACTION_MOVE:
            if (mActivePointerId == INVALID_POINTER)
                return false;

            final float y = getMotionEventY(ev, mActivePointerId);
            if (-1 == y)
                return false;
            final float yDiff = y - mInitialMotionY;
            if (yDiff > mTouchSlop && !mIsBeingDragged)
                mIsBeingDragged = true;
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mIsBeingDragged = false;
            mActivePointerId = INVALID_POINTER;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            onSecondaryPointerUp(ev);
            break;

    }

    return mIsBeingDragged;
}
 
Example 18
Source File: SwipeRefreshLayout.java    From fangzhuishushenqi with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();

    final int action = MotionEventCompat.getActionMasked(ev);
    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }
    if (!isEnabled() || mReturningToStart || canChildScrollUp() || mRefreshing) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true);
            mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
            mIsBeingDragged = false;
            final float initialDownY = getMotionEventY(ev, mActivePointerId);
            if (initialDownY == -1) {
                return false;
            }
            mInitialDownY = initialDownY;
            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;
            }

            final float y = getMotionEventY(ev, mActivePointerId);
            if (y == -1) {
                return false;
            }
            final float yDiff = y - mInitialDownY;
            if (yDiff > mTouchSlop && !mIsBeingDragged) {
                mInitialMotionY = mInitialDownY + mTouchSlop;
                mIsBeingDragged = true;
                mProgress.setAlpha(STARTING_PROGRESS_ALPHA);
            }
            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 19
Source File: NumberPicker.java    From DatePicker with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled()) {
        return false;
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);

    int action = MotionEventCompat.getActionMasked(event);
    mTouchAction = action;
    if (MotionEvent.ACTION_DOWN == action) {
        getParent().requestDisallowInterceptTouchEvent(true);
        mStartY = (int) event.getY();
        if (!mFlingScroller.isFinished() || !mAdjustScroller.isFinished()) {
            mFlingScroller.forceFinished(true);
            mAdjustScroller.forceFinished(true);
            onScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
        }
    } else if (MotionEvent.ACTION_MOVE == action) {
        mCurrY = (int) event.getY();
        mOffectY = mCurrY - mStartY;

        if (!mCanScroll && Math.abs(mOffectY) < mTouchSlop) {
            return false;
        } else {
            mCanScroll = true;
            if (mOffectY > mTouchSlop) {
                mOffectY -= mTouchSlop;
            } else if (mOffectY < -mTouchSlop) {
                mOffectY += mTouchSlop;
            }
        }

        mStartY = mCurrY;

        computeYPos(mOffectY);

        onScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
        invalidate();
    } else if (MotionEvent.ACTION_UP == action) {
        mCanScroll = false;

        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 {
            adjustYPosition();
            invalidate();
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
    }

    return true;
}
 
Example 20
Source File: TwinklingRefreshLayout.java    From TwinklingRefreshLayout with Apache License 2.0 4 votes vote down vote up
private boolean detectNestedScroll(MotionEvent e) {
    final MotionEvent vtev = MotionEvent.obtain(e);
    final int action = MotionEventCompat.getActionMasked(e);
    final int actionIndex = MotionEventCompat.getActionIndex(e);

    if (action == MotionEvent.ACTION_DOWN) {
        mNestedOffsets[0] = mNestedOffsets[1] = 0;
    }
    vtev.offsetLocation(mNestedOffsets[0], mNestedOffsets[1]);

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mActivePointerId = e.getPointerId(0);
            mLastTouchX = (int) e.getX();
            mLastTouchY = (int) e.getY();
            startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
            break;
        case MotionEventCompat.ACTION_POINTER_DOWN:
            mActivePointerId = e.getPointerId(actionIndex);
            mLastTouchX = (int) e.getX(actionIndex);
            mLastTouchY = (int) e.getY(actionIndex);
            break;
        case MotionEvent.ACTION_MOVE:
            final int index = e.findPointerIndex(mActivePointerId);
            if (index < 0) {
                Log.e("TwinklingRefreshLayout", "Error processing scroll; pointer index for id " +
                        mActivePointerId + " not found. Did any MotionEvents get skipped?");
                return false;
            }

            final int x = (int) e.getX(index);
            final int y = (int) e.getY(index);

            int dx = mLastTouchX - x;
            int dy = mLastTouchY - y;

            if (dispatchNestedPreScroll(dx, dy, mScrollConsumed, mScrollOffset)) {
                dx -= mScrollConsumed[0];
                dy -= mScrollConsumed[1];
                vtev.offsetLocation(mScrollOffset[0], mScrollOffset[1]);
                // Updated the nested offsets
                mNestedOffsets[0] += mScrollOffset[0];
                mNestedOffsets[1] += mScrollOffset[1];
            }

            if (!mIsBeingDragged && Math.abs(dy) > mTouchSlop) {
                final ViewParent parent = getParent();
                if (parent != null) {
                    parent.requestDisallowInterceptTouchEvent(true);
                }
                mIsBeingDragged = true;
                if (dy > 0) {
                    dy -= mTouchSlop;
                } else {
                    dy += mTouchSlop;
                }
            }

            if (mIsBeingDragged) {
                mLastTouchY = y - mScrollOffset[1];

                final int scrolledDeltaY = 0;
                final int unconsumedY = dy - scrolledDeltaY;
                if (dispatchNestedScroll(0, scrolledDeltaY, 0, unconsumedY, mScrollOffset)) {
                    mLastTouchX -= mScrollOffset[0];
                    mLastTouchY -= mScrollOffset[1];
                    vtev.offsetLocation(mScrollOffset[0], mScrollOffset[1]);
                    mNestedOffsets[0] += mScrollOffset[0];
                    mNestedOffsets[1] += mScrollOffset[1];
                }
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            stopNestedScroll();
            mIsBeingDragged = false;
            mActivePointerId = INVALID_POINTER;
            break;
    }
    vtev.recycle();
    return true;
}