fling时无法左右滑动

Posted ShouCeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fling时无法左右滑动相关的知识,希望对你有一定的参考价值。

上下滑动的RecyclerView嵌套可以左右滑动的ViewPager。
RecyclerView滑动fling时,希望能够手动左右滑动。
我们首先确定fling的触发条件:

  @Override
    public boolean onTouchEvent(MotionEvent e) {
         ......
    	switch (action) {
             case MotionEvent.ACTION_UP: {
                mVelocityTracker.addMovement(vtev);
                eventAddedToVelocityTracker = true;
                mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
                final float xvel = canScrollHorizontally
                        ? -mVelocityTracker.getXVelocity(mScrollPointerId) : 0;
                final float yvel = canScrollVertically
                        ? -mVelocityTracker.getYVelocity(mScrollPointerId) : 0;
                if (!((xvel != 0 || yvel != 0) && fling((int) xvel, (int) yvel))) {
                    setScrollState(SCROLL_STATE_IDLE);
                }
                resetScroll();
            } break;
         ......  
    }

找fling()方法,该方法又会调用mViewFlinger.fling(velocityx,velocityY)方法:

 public void fling(int velocityX, int velocityY) {
            setScrollState(SCROLL_STATE_SETTLING);
            mLastFlingX = mLastFlingY = 0;
            // Because you can't define a custom interpolator for flinging, we should make sure we
            // reset ourselves back to the teh default interpolator in case a different call
            // changed our interpolator.
            if (mInterpolator != sQuinticInterpolator) {
                mInterpolator = sQuinticInterpolator;
                mOverScroller = new OverScroller(getContext(), sQuinticInterpolator);
            }
            mOverScroller.fling(0, 0, velocityX, velocityY,
                    Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
            postOnAnimation();
        }

可以看到最终mScrollState赋值为SCROLL_STATE_SETTLING。
其中mScrollState有三种状态:
1、SCROLL_STATE_IDLE = 0; 空闲状态,静止状态
2、SCROLL_STATE_IDLE = 0; 拖动状态,一般是手指滚动
3、SCROLL_STATE_IDLE = 0; 自滑动状态,一般是fling

我们手动左右滑动时继续看源码onInterceptTouchEvent:

   @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
       .....
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                ......
                if (mScrollState == SCROLL_STATE_SETTLING) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    setScrollState(SCROLL_STATE_DRAGGING);
                    stopNestedScroll(TYPE_NON_TOUCH);
                }
				......
        }
        return mScrollState == SCROLL_STATE_DRAGGING;
    }

在DOWN时,会赋值SCROLL_STATE_SETTLING。
返回值 mScrollState == SCROLL_STATE_DRAGGING,为true,也就是RecyclerView会在Down的时候就拦截该事件,左右滑动事件得不到响应。

以上是关于fling时无法左右滑动的主要内容,如果未能解决你的问题,请参考以下文章

fling时无法左右滑动

fling时无法左右滑动

Android NestedScrolling解决滑动冲突问题 - fling问题与NestedScroll++

Android NestedScrolling解决滑动冲突问题 - fling问题与NestedScroll++

使用 ViewPager 滑动时,片段没有调用 OnResume()

底部应用栏在使用片段导航时向上/向下滑动(导航架构组件)