最近在做一款横屏App的时候,遇到,当点击横屏Fragment中的item的时候,跳转到数据activity界面时候,然后点击返回,发现界面数据空白,感觉数据也还在,该走的方法也走了,但是界面就是空白的,这是什么鬼?然后不停的打断点发现该走的流程就是走了,但是界面就是不显示
这个问题出在哪里呢,然后我就开始打印Fragment的生命周期,看看他在这个过程中走了哪些个方法.最后再onResume()方法中,加载如果adapter不为空,就前置刷新这个界面, 如果为空,就网络请求,这样代码写完了之后,发现还是返回后,界面还是无法显示,后来在网上找了很多方法,还是不管用,当时这样想,这条路走不通,是不是xml出了问题,返回的时候,不会走OnCreate 方法,是不是RecyclerView的高度影响了界面的显示,
不能显示前的xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/hotList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.constraint.ConstraintLayout>
修改后,并在跳转其他界面返回后,界面有数据的写法:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/hotList"
android:layout_width="match_parent"
android:layout_height="match_parent"//这行代码很关键
/>
</android.support.constraint.ConstraintLayout>
其他地方的代码根本不用改, 问题得到圆满的解决

在开发横屏App时,遇到点击Fragment中的元素跳转到Activity后,返回时界面显示空白的问题。经过调试,发现生命周期正常,数据也存在。通过跟踪Fragment的生命周期,在onResume()中检查Adapter状态并尝试刷新解决,但未见效。进一步分析,怀疑是XML布局问题,尤其是RecyclerView高度可能导致影响。修改RecyclerView高度设置后,问题解决,返回时界面能正确显示数据。
1491





