神之bug 嵌套RecyclerView谜之滚动

序言

有些bug,来无影,去无踪。你不知道它怎么产生的。比如下面这个,描述一下症状,每次Fragment切换的时候,页面都会自己滚动一点好像要把自己对齐。

《神之bug 嵌套RecyclerView谜之滚动》 这里写图片描述

布局如下

《神之bug 嵌套RecyclerView谜之滚动》 这里写图片描述

XML实现如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/top_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/TopBarHeight"
        android:background="@color/colorPrimary">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/ic_logo" />
    </RelativeLayout>

    <com.trs.library.widget.statusviewlayout.StatusViewLayout
        android:id="@+id/status_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.scwang.smartrefresh.layout.SmartRefreshLayout
            android:id="@+id/ptr"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:srlEnableHeaderTranslationContent="true"
            app:srlEnableLoadmore="true">

            <com.scwang.smartrefresh.layout.header.ClassicsHeader
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srlClassicsSpinnerStyle="FixedBehind" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/white" />

            <com.scwang.smartrefresh.layout.footer.ClassicsFooter
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </com.scwang.smartrefresh.layout.SmartRefreshLayout>

    </com.trs.library.widget.statusviewlayout.StatusViewLayout>
</LinearLayout>

用的是最近比较火的SmartRefreshLayout。

分析

一开始我看到的是这样的

《神之bug 嵌套RecyclerView谜之滚动》 这里写图片描述

1.自定义Layout出问题

每一次切换回来最顶部的自定义Layout都显示不全,也就是下面这个,实现可以看我的上一篇博客三角形兼梯形布局

《神之bug 嵌套RecyclerView谜之滚动》 这里写图片描述

于是把这个控件替换成RelativeLayout高度固定,但是bug依然存在。因此排除此项。

2.SmartRefreshLayout的问题

这个页面使用的了SmartRefreshLayout,详情见这里Android智能下拉刷新框架-SmartRefreshLayout
虽然是比较有名的第三方库,但是bug也是可能有的。所以开始阅读他的源码,但是在阅读了他的源码以后,发现没什么问题。通过打断点发现,在页面发生滚动的时候它的方法根本就没有调用。

3.手机的bug

我怀疑是不是开启了类属于三星的眼动功能,于是用了同事的小米进行测试,排除。

4.RecyclerView的问题

既然排除了所以可能性,那么最不可能的答案也是答案了。在RecyclerView中负责滚动的是LayoutManger,于是我自定义了LayoutManger,发现确实有Log输出
测试代码如下:

 @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new MyLayoutManger(getActivity());
    }

    private class MyLayoutManger extends LinearLayoutManager {

        public MyLayoutManger(Context context) {
            super(context);
        }

        @Override
        public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
            Log.i("zzz", "MyLayoutManger scrollHorizontallyBy dy=" + dy);
            return super.scrollVerticallyBy(dy, recycler, state);
        }

    }

每一次切换回来的Log输出

10-13 16:29:53.968 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=53
10-13 16:29:53.988 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=63
10-13 16:29:53.998 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=53
10-13 16:29:54.018 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=39
10-13 16:29:54.038 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=36
10-13 16:29:54.048 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=26
10-13 16:29:54.068 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=21
10-13 16:29:54.088 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=16
10-13 16:29:54.108 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=12
10-13 16:29:54.118 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=8
10-13 16:29:54.138 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=7
10-13 16:29:54.158 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=5
10-13 16:29:54.168 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=3
10-13 16:29:54.188 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=1
10-13 16:29:54.208 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=2
10-13 16:29:54.238 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=1

调用链如下

《神之bug 嵌套RecyclerView谜之滚动》 这里写图片描述

可以看出来都是Framework层的调用,没有我们自己的代码。也没有SmartRefreshLayout的代码。既然找到问题了,就去Google。果然这是RecyclerView嵌套的bug。

《神之bug 嵌套RecyclerView谜之滚动》 这里写图片描述

总结

在遇到bug的时候,一定要耐心仔细,从最容易出错的地方找起,可以先从自己找齐,但是如果自身没有问题,也不能放过第三方库,认为第三方库就完美无缺。当遇到第三方库有问题的时候首先可以去翻阅Github中的issues这样会更快捷,但是如果还没有人阅读这类问题,那么我还得去read the funk source code (我曾经就通过跟踪断点,找出张鸿洋一个库里面代码的逻辑问题,然后去提了issues。)因此当我们遇到bug时不要怕,要平心静气,理性分析,合理假设,逐一排除。

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