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 没有被触发
- UITextView 内容插图 2022-01-01
- URL编码Swift iOS 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01