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的时候就拦截该事件,左右滑动事件得不到响应。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值