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