FragmentPagerAdapter getItem is not being triggered(FragmentPagerAdapter getItem 没有被触发)
问题描述
目前,通过 FragmentActivity
,我使用以下代码在 2 种类型的 Fragment 之间切换.
Currently, with a FragmentActivity
, I toggle among 2 type of Fragments using the following code.
private void toggle() {
Fragment oldFragment = getSupportFragmentManager().findFragmentById(R.id.content);
Fragment fragment = null;
if (oldFragment instanceof ColorFragment) {
fragment = new ViewPagerFragment();
} else {
fragment = new ColorFragment(android.R.color.black);
}
getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commitAllowingStateLoss();
}
正在切换 2 个片段.
2 Fragments are being toggle.
- ColorFragment - 一个简单的片段,用纯黑色填充其背景.
- ViewPagerFragment - 片段包含
ViewPager
.用户可以在紫色片段和蓝色片段之间滑动.
- ColorFragment - A simple fragment which fill up its background with solid black color.
- ViewPagerFragment - A fragment contains
ViewPager
. User can swipe between a purple color fragment, and a blue color fragment.
负责刷紫色和蓝色碎片的代码如下.
The code which responsible for swiping purple and blue color fragments are as below.
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 2;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ColorFragment(android.R.color.holo_purple);
default:
return new ColorFragment(android.R.color.holo_blue_bright);
}
}
}
但是,我在切换过程中遇到了奇怪的行为.
However, I encounter the weird behavior during toggling.
- 显示黑色片段.
- 切换.
- 查看分页器,可以在显示的紫色和蓝色片段之间滑动.
- 切换.
- 显示黑色片段.
- 切换.
- 没有显示,因为 MyFragmentPagerAdapter 的 getItem 没有被触发.
我觉得我的情况类似于FragmentPagerAdapter getItem is not called
但是,我不喜欢使用 FragmentStatePagerAdapter
,因为在页面之间切换时可能会产生更多开销.
However, I prefer not to use FragmentStatePagerAdapter
, because of the cost of potentially more overhead when switching between pages.
有什么办法可以解决这个问题吗?
Any workaround to overcome this problem?
我包含一个完整的可行源代码来演示这个问题:https://www.dropbox.com/s/jok9tz5ktvfcteo/viewpagerbug.zip
I include a complete workable source code to demonstrate this problem : https://www.dropbox.com/s/jok9tz5ktvfcteo/viewpagerbug.zip
推荐答案
有什么办法可以解决这个问题吗?
Any workaround to overcome this problem?
我已经下载了您的代码,但出现问题是因为您没有正确处理那些 Fragments
.最准确地说,您在基于 ViewPager
的 Fragment
中使用嵌套的 Fragments
并为该 ViewPager
创建适配器,如下所示:
I've downloaded your code and the problem appears because you don't handle those Fragments
right. Most precisely you use nested Fragments
in the ViewPager
based Fragment
and for that ViewPager
you create the adapter like this:
MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(this.getFragmentManager());
相反,您应该使用 getChildFragmentManager()
来绑定嵌套片段:
Instead, you should be using getChildFragmentManager()
to bind the nested fragments:
MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(this.getChildFragmentManager());
此外,您不应通过构造函数将数据传递给 Fragment
,因为该数据将无法在配置更改后继续存在,并且会开始出现不良情况.请改用 Bundle
.
Also, you shouldn't pass data through a constructor to a Fragment
as that data will not survive a configuration change and bad things will start to appear. Use a Bundle
instead.
这篇关于FragmentPagerAdapter getItem 没有被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:FragmentPagerAdapter getItem 没有被触发
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01