神之bug 嵌套RecyclerView谜之滚动
来源:互联网 发布:淘宝开店支付宝提现 编辑:程序博客网 时间:2024/06/05 04:54
序言
有些bug,来无影,去无踪。你不知道它怎么产生的。比如下面这个,描述一下症状,每次Fragment切换的时候,页面都会自己滚动一点好像要把自己对齐。
布局如下
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。
分析
一开始我看到的是这样的
1.自定义Layout出问题
每一次切换回来最顶部的自定义Layout都显示不全,也就是下面这个,实现可以看我的上一篇博客三角形兼梯形布局
于是把这个控件替换成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=5310-13 16:29:53.988 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=6310-13 16:29:53.998 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=5310-13 16:29:54.018 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=3910-13 16:29:54.038 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=3610-13 16:29:54.048 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=2610-13 16:29:54.068 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=2110-13 16:29:54.088 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=1610-13 16:29:54.108 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=1210-13 16:29:54.118 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=810-13 16:29:54.138 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=710-13 16:29:54.158 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=510-13 16:29:54.168 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=310-13 16:29:54.188 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=110-13 16:29:54.208 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=210-13 16:29:54.238 6990-6990/com.trs.cqjb.gov I/zzz: MyLayoutManger scrollHorizontallyBy dy=1
调用链如下
可以看出来都是Framework层的调用,没有我们自己的代码。也没有SmartRefreshLayout的代码。既然找到问题了,就去Google。果然这是RecyclerView嵌套的bug。
总结
在遇到bug的时候,一定要耐心仔细,从最容易出错的地方找起,可以先从自己找齐,但是如果自身没有问题,也不能放过第三方库,认为第三方库就完美无缺。当遇到第三方库有问题的时候首先可以去翻阅Github中的issues这样会更快捷,但是如果还没有人阅读这类问题,那么我还得去read the funk source code (我曾经就通过跟踪断点,找出张鸿洋一个库里面代码的逻辑问题,然后去提了issues。)因此当我们遇到bug时不要怕,要平心静气,理性分析,合理假设,逐一排除。
- 神之bug 嵌套RecyclerView谜之滚动
- 修复RecyclerView嵌套滚动问题
- RecyclerView中嵌套RecyclerView导致自动滚动
- Android开发之滚动控件RecyclerView
- Bootstrap3modal嵌套|模态框嵌套滚动条bug
- Bootstrap3modal嵌套|模态框嵌套滚动条bug
- Android Material Design之在RecyclerView中嵌套CardView实现
- Android之NestedScrollView 嵌套 RecyclerView 滑动冲突的问题
- RecyclerView嵌套RecyclerView 条目自动上滚的bug
- RecyclerView 嵌套RecyclerView 或者 ScrollView当中嵌套RecyclerView ,子View会自动滚动到顶部
- Android开发之实现水平滚动效果—RecyclerView
- RecyclerView踩坑之输入法弹出数据自动滚动
- 10读书笔记之更强大的滚动控件-----RecyclerView
- 关于RecyclerView之纵向滚动(自己记录用)
- Android---RecyclerView简单实用(下)之横向水平滚动
- android之ScrollView里嵌套ListView(都可滚动)
- android NestedScrolling嵌套滑动实战之联合滚动fling效果
- RecyclerView常见问题解决方案,RecyclerView嵌套自动滚动,RecyclerView 高度设置wrap_content 无作用等问题
- 深度学习-2:tensorflow 入门以及简单的线性拟合
- 关于解决浏览器无法运行小应用程序问题
- Applet小应用程序之间的通讯
- java编写Base64密码器
- Android DEX 文件打开分析
- 神之bug 嵌套RecyclerView谜之滚动
- hdu 1087 Super Jumping! Jumping! Jumping!
- 用链表编写学生管理系统(涵盖文件的输入输出)
- banner的使用(自拟数据)
- 信息管理系统(java)
- hadoop集群安装
- 建立Linux ssh trust
- SMTP邮件发送命令
- java面试总结