ScrollView与头+RecycleView嵌套冲突源码分析

前段时间项目中遇到的问题,ScrollView 嵌套 LinearLayout,LinearLayout 中是上面的布局加 RecycleView,这样嵌套,滑动到 RecycleView 的时候像粘住了一样滑的很慢,网上搜一下,发现加入 mRecycleView.setNestedScrollingEnabled(false) 即可解决,当时也没弄明白为什么,就先用着了,直接翻译就是设置嵌套滑动为 false。最近不忙了,看一下源码,原来是这样子啊。

首先看 RecycleView 的 setNestedScrollingEnabled() :
@Override public void setNestedScrollingEnabled(boolean enabled) { getScrollingChildHelper().setNestedScrollingEnabled(enabled); }
getScrollingChildHelper() 做的事情如下,返回一个 NestedScrollingChildHelper 对象,并把当前 RecycleView 作为 child,NestedScrollingChildHelper 的作用官方文档意思是用于实现嵌套滚动子视图兼容的辅助类。
private NestedScrollingChildHelper getScrollingChildHelper() { if (mScrollingChildHelper == null) { mScrollingChildHelper = new NestedScrollingChildHelper(this); } return mScrollingChildHelper; }
NestedScrollingChildHelper.setNestedScrollingEnabled()方法如下,mIsNestedScrollingEnabled 在默认情况下会在 RecycleView 的构造函数中初始为 true。第一次调用该方法,会执行 ViewCompat.stopNestedScroll(mView),并设置 mIsNestedScrollingEnabled 为 false:
public void setNestedScrollingEnabled(boolean enabled) { if (mIsNestedScrollingEnabled) { ViewCompat.stopNestedScroll(mView); } mIsNestedScrollingEnabled = enabled; }
看一下 ViewCompat.stopNestedScroll(mView); 都做了什么:
public static void stopNestedScroll(View view) { IMPL.stopNestedScroll(view); }
IMPL 是 ViewCompatImpl类型,根据不同的版本具体的对象不同,但最终都是调用了view.stopNestedScroll(),这里面的 view 就是 getScrollingChildHelper() 传进去的 RecycleView,下面看一下 RecycleView 的stopNestedScroll() 中做了什么:
@Override public boolean stopNestedScroll() { getScrollingChildHelper().stopNestedScroll(); }
哇塞,又调用了 NestedScrollingChildHelper 的 stopNestedScroll(),下面代码中 mNestedScrollingParent 就是 view.getParent() 获取到的,第一次调用该方法时会执行 ViewParentCompat.onStopNestedScroll(mNestedScrollingParent, mView),并将 mNestedScrollingParent 置为 null。
public void stopNestedScroll() { if (mNestedScrollingParent != null) { ViewParentCompat.onStopNestedScroll(mNestedScrollingParent, mView); mNestedScrollingParent = null; } }
ViewParentCompat.onStopNestedScroll方法如下:
public static void onStopNestedScroll(ViewParent parent, View target) { IMPL.onStopNestedScroll(parent, target); }
IMPL.onStopNestedScroll() 中最终都是调用了 ViewParentCompatLollipop.onStopNestedScroll(parent, target),
public static void onStopNestedScroll(ViewParent parent, View target) { try { parent.onStopNestedScroll(target); } catch (AbstractMethodError e) { Log.e(TAG, "ViewParent " + parent + " does not implement interface " + "method onStopNestedScroll", e); } }
ViewGroup 就是实现了 ViewParent 接口,所以执行的是 ViewGroup 中的 onStopNestedScroll,代码如下:
@Override // Stop any recursive nested scrolling. stopNestedScroll(); mNestedScrollAxes = 0; }
来一条最最华丽的分割线

调用 stopNestedScroll() 时,mNestedScrollingParent 已被置为 null,什么都不会执行,重点是 mNestedScrollAxes = 0,即 SCROLL_AXIS_NONE,那么这样有什么作用呢?
终于到实现的核心代码了
在 ScrollView 的 onInterceptTouchEvent 方法中找到的 getNestedScrollAxes()(获取的就是 mNestedScrollAxes),当 mNestedScrollAxes 改变,影响的是 ScrollView 的滑动事件,下面是主要的代码:
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { ...... switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_MOVE: { ...... if (yDiff > mTouchSlop && (getNestedScrollAxes() & SCROLL_AXIS_VERTICAL) == 0) { mIsBeingDragged = true; ...... } break; } ...... return mIsBeingDragged; }
yDiff > mTouchSlop 是当滑动过一定距离,getNestedScrollAxes() 为 0,所以 getNestedScrollAxes() & SCROLL_AXIS_VERTICAL) == 0 也是 true,所以 mIsBeingDragged = true,onInterceptTouchEvent 就返回 true,表示 ScrollView 拦截了滑动事件不向 child 传递,因此解决了滑动冲突。

如有意见或建议欢迎提出,共同进步!

    原文作者:hexiaosa
    原文地址: https://www.jianshu.com/p/7e17e48e6baf
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞