RecyclerView嵌套RecyclerView踩的坑

来源:互联网 发布:淘宝卖假货封店 编辑:程序博客网 时间:2024/06/08 09:56

最近项目中需要采用在竖直方向的Recyclerview中嵌套水平RecyclerView,同时顶部还需要使用CoordinatorLayout和CollapsingToolbarLayout实现折叠布局,刚开始觉得没什么难度,使用后发现滑动RecyclerView并不能折叠布局,第一感觉就是嵌套导致了RecyclerView 的滑动响应出了问题,然后做个各种尝试,都没有很顺畅的解决,快要放弃的时候在RecyclerView中看到了setNestedScrollingEnabled的这个方法,接受布尔类型的参数,将水平方向上的RecyclerView设置setNestedScrollingEnabled(false),惊喜发现问题解决;查看源码

/**     * Enable nested scrolling.     *     * <p>This is a delegate method. Call it from your {@link android.view.View View} subclass     * method/{@link android.support.v4.view.NestedScrollingChild} interface method with the same     * signature to implement the standard policy.</p>     *     * @param enabled true to enable nested scrolling dispatch from this view, false otherwise     */    public void setNestedScrollingEnabled(boolean enabled) {        if (mIsNestedScrollingEnabled) {            ViewCompat.stopNestedScroll(mView);        }        mIsNestedScrollingEnabled = enabled;    }

看到代码我们知道,这实际上是禁掉了RecyclerView的NestedScroll;