开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 1 天,点击查看活动详情
一、背景介绍
先看图说话,是不是一脸懵逼,怎么会呢,我测试的时候是正常的啊!
当时看到这个问题的时候,我也是第一时间用自己的4部测试机进行了验证,发现在上拉加载更多的时候都是没有任何问题的。
后面就联系测试了解到,他们正在做兼容性测试,出现问题的手机是一台Android 5.0系统的手机,于是乎,我就上云测验证,还真被我复现了,这就尴尬了。。。
二、解决方案
一般这种问题能复现,就代表可以解决,下面就是开始找解决方案了,刚开始我一度怀疑是不是我使用的三方的刷新库有问题导致的,难道是版本没有适配好Android低版本的系统。
迅速登录我的GitHub账号,下载官方的demo验证,发现demo是没问题的,这就百思不得其解了,只能一行一行扒代码分析了。
由于这个项目不是我从头写的,而且这部分的逻辑整体比较简单,也没有做详细的review,所以还不是很了解,刷新框架用的是比较流行的 SmartRefreshLayout ,这个基本上每个项目都会用到。
下面贴一下,整个xml布局文件的代码,各位看官看看是否可以看出来什么端倪。
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/window_fragment_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ffffff"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/bg_view"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_150"/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_16dp"
android:text="公告"
android:textColor="#090A1A"
android:textSize="@dimen/sp_16"
android:textStyle="bold"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RelativeLayout
android:id="@+id/close"
android:layout_width="@dimen/dimen_40dp"
android:layout_height="@dimen/dimen_40dp"
android:layout_marginTop="@dimen/dimen_18dp"
android:gravity="right"
android:layout_marginRight="@dimen/dimen_16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="16dp"
android:layout_height="@dimen/dimen_16dp"
android:layout_centerHorizontal="true"
android:background="@drawable/vh_close" />
</RelativeLayout>
<View
android:id="@+id/divider"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0_5"
android:layout_marginTop="@dimen/dp_52"
android:background="#ECEFF1"
app:layout_constraintTop_toTopOf="parent" />
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refresh_layout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/main_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
<com.baselib.widget.EmptyOrErrorWidget
android:id="@+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/refresh_layout"
app:layout_constraintLeft_toLeftOf="@+id/refresh_layout"
app:layout_constraintRight_toRightOf="@+id/refresh_layout"
app:layout_constraintTop_toTopOf="@+id/refresh_layout" />
</androidx.constraintlayout.widget.ConstraintLayout>
上面的几行代码,一眼看上去没什么问题,所以我就直接略过这部分了,从其它方面找解决方案,最后转了一圈,还是没解决,就抱着试试的心态,想是不是我刷新框架里面的头布局和脚布局写的顺序不对导致的呢!
事实证明,我猜对了,还真是由于ClassisHeader和ClassicsFooter以及RecyclerView三者是顺序不规范导致的。
解决后的局部代码如下,只是将三者的顺序写的更加规范了。
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refresh_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="@dimen/dp_52"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
三、总结
总的来说,出现这个问题还是由于我们平时开发写代码过于随意导致的问题,而且像这种问题在高版本的手机上也没有什么问题,但是在低版本的手机上就出现了上拉加载更多的时候白屏了。
最后,代码要规范,否则两行泪!