当前位置:网站首页>Fragment中嵌套ViewPager数据空白页异常问题分析
Fragment中嵌套ViewPager数据空白页异常问题分析
2022-08-09 12:04:00 【谢栋_】
开题思考:Fragment放ViewPager,ViewPager里面是fragment。第一次进入没问题,再次进入ViewPager的fragment时里面内容就没了,出现数据空白页异常问题。
描述:activity中放了两个Fragment,分别为待审核页、已加入页;其中待审核fragment中利用viewpager放了三个fragment,分别为全部、审核加入、审核退出,第二次切换待审核、已加入fragment时,会发现待审核fragment中ViewPager承载的Fragment会出现数据空白页现象。如下图所示:
根据经验感觉是FragmentManager出了问题,因为FragmentManager的职责本来就是负责管理Fragment,包括对Fragment栈中的Fragement进行添加,删除,替换等操作,那既然出现了空白页,肯定是FragmentManager在进行Fragment替换时没有正常工作,查看Fragment以及FragmentActivity的源码发现有三种类型的FragmentManager,分别为getFragmentManager、getSupportFragmentManager、getChildFragmentManager;其中getFragmentManager跟getChildFragmentManager在Fragment类中,而getSupportFragmentManager位于FragmentActivity中,而getFragmentManager虽然在Fragment中,但是官方注释中最后有提到,如果当前Fragment是另外一个Fragment的子类,则当前FragmentManager属于父类,这就意味着,三者虽然相似但管理的对象却是不同的。
三者源码如下:
getFragmentManager:
/**
* Return the FragmentManager for interacting with fragments associated
* with this fragment's activity. Note that this will be non-null slightly
* before {@link #getActivity()}, during the time from when the fragment is
* placed in a {@link FragmentTransaction} until it is committed and
* attached to its activity.
*
* <p>If this Fragment is a child of another Fragment, the FragmentManager
* returned here will be the parent's {@link #getChildFragmentManager()}.
*/
final public FragmentManager getFragmentManager() {
return mFragmentManager;
}
getSupportFragmentManager:
/**
* Return the FragmentManager for interacting with fragments associated
* with this activity.
*/
public FragmentManager getSupportFragmentManager() {
return mFragments.getSupportFragmentManager();
}
getChildFragmentManager:
/**
* Return a private FragmentManager for placing and managing Fragments
* inside of this Fragment.
*/
final public FragmentManager getChildFragmentManager() {
if (mChildFragmentManager == null) {
instantiateChildFragmentManager();
if (mState >= RESUMED) {
mChildFragmentManager.dispatchResume();
} else if (mState >= STARTED) {
mChildFragmentManager.dispatchStart();
} else if (mState >= ACTIVITY_CREATED) {
mChildFragmentManager.dispatchActivityCreated();
} else if (mState >= CREATED) {
mChildFragmentManager.dispatchCreate();
}
}
return mChildFragmentManager;
}
根据google官方的描述(PS:English自译拿不出手),大意为getFragmentManager、跟getSupportFragmentManager是为了获取能管理和当前Activity有关联的Fragment的FragmentManager,也就是说此二者是属于Activity的,再来看下getChildFragment的官方注释
/**
* Return a private FragmentManager for placing and managing Fragments
* inside of this Fragment.
*/
final public FragmentManager getChildFragmentManager() {
上文注释大致意思返回一个私有的FragmentManager来管理Fragment,它属于Fragment。至此整个问题就梳理清楚了,因为文章开题提出的问题,是在Fragment中使用ViewPager装载Fragment,也就是说,在Fragment中应该是用的是Fragemnt类中提供的manager方法,即在给ViewPager设置FragmentManager的时候应该使用getChildFragmentManager。
文章开题提到的给ViewPager设置Adapter:
viewPager.setAdapter(new ManageFragmentPagerAdapter(getFragmentManager(), list));
经过分析应该修改为:
viewPager.setAdapter(new ManageFragmentPagerAdapter(getChildFragmentManager(), list));
归纳总结:getFragmentManager()所得到的是所在fragment 的父容器的管理器,而getChildFragmentManager()所得到的是在fragment
边栏推荐
- JD.com architects tidy up: what are the core technical knowledge points of jvm and performance tuning
- 我们真的需要DApp吗?App真的不能满足我们的幻想吗?
- 微信支付开发流程
- 二叉树的序列化和反序列化
- AI basketball referee, walking is special, ask harden care don't care
- 国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
- ABP 6.0.0-rc.1的新特性
- ThreadLocal的简单理解
- 8、IDEA提交代码出现: Fetch failed fatal: Could not read from remote repository
- C# Get system installed .NET version
猜你喜欢
JD.com architects tidy up: what are the core technical knowledge points of jvm and performance tuning
你没见过的《老友记》镜头,AI给补出来了|ECCV 2022
HAproxy:负载均衡
MySQL principle and optimization of Group By optimization techniques
ABAP 面试题:如何使用 ABAP 编程语言的 System CALL 接口,直接执行 ABAP 服务器所在操作系统的 shell 命令?
How to upload local file trial version in binary mode in ABAP report
超越CLIP的多模态模型,只需不到1%的训练数据!南加大最新研究来了
Win10 compiles the x264 library (there are also generated lib files)
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
Scala 高阶(七):集合内容汇总(上篇)
随机推荐
中科院打脸谷歌:普通电脑追上量子优越性,几小时搞定原本要一万年的计算...
PM2 configuration file
香港服务器如何进行加密?
Shell之常用小工具(sort、uniq、tr、cut)
The core key points of microservice architecture
Django cannot link mysql database
2022牛客多校(六)M. Z-Game on grid
【微服务~远程调用】整合RestTemplate、WebClient、Feign
问题来了:4GB物理内存的机器上申请8G内存能成功吗?
合并两个有序列表
go基础之web获取参数
Information system project managers must memorize the core test sites (63) The main process of project portfolio management & DIPP analysis
FFmpeg compiles and installs on win10 (configure libx264)
超越CLIP的多模态模型,只需不到1%的训练数据!南加大最新研究来了
非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer
How to upload local file trial version in binary mode in ABAP report
AI篮球裁判火了,走步算得特别准,就问哈登慌不慌
AI basketball referee, walking is special, ask harden care don't care
shell脚本------函数的格式,传参,变量,递归,数组
ABAP 报表中如何以二进制方式上传本地文件试读版