FragmentPagerAdapter的getItem没有被触发 [英] FragmentPagerAdapter getItem is not being triggered
问题描述
目前,使用 FragmentActivity
,我切换之间的2型使用以下code片段。
私人无效切换(){
片段oldFragment = getSupportFragmentManager()findFragmentById(R.id.content)。
片段片段= NULL;
如果(oldFragment的instanceof ColorFragment){
片段=新ViewPagerFragment();
} 其他 {
片段=新ColorFragment(android.R.color.black);
}
。getSupportFragmentManager()的BeginTransaction()代替(R.id.content,片段).commitAllowingStateLoss()。
}
2片段被触发。
- ColorFragment - 一个简单的片段,它填补了它的背景与纯黑色的颜色
- ViewPagerFragment - 片段包含
ViewPager
。用户可以在紫色的片段,和蓝色片段之间滑动。
在code的负责刷卡的紫色和蓝色的片段如下所示。
私有静态类MyFragmentPagerAdapter扩展FragmentPagerAdapter {
公共MyFragmentPagerAdapter(FragmentManager FM){
超(FM);
}
@覆盖
公众诠释getCount将(){
返回2;
}
@覆盖
公共片段的getItem(INT位置){
开关(位置){
情况下0:
返回新ColorFragment(android.R.color.holo_purple);
默认:
返回新ColorFragment(android.R.color.holo_blue_bright);
}
}
}
不过,我遇到切换在怪异的行为。
- 黑色片段所示。
- 切换。
- 查看传呼机,它可以紫色和蓝色片段之间刷卡所示。
- 切换。
- 黑色片段所示。
- 切换。
- 没有显示,由于MyFragmentPagerAdapter的的getItem不被触发。
我觉得我的情况是类似<一个href="http://stackoverflow.com/questions/12581896/fragmentpageradapter-getitem-is-not-called">FragmentPagerAdapter的getItem不叫
不过,我preFER不使用的页面之间切换时可能更多的开销成本 FragmentStatePagerAdapter
,因为的
任何变通方法来解决这个问题?
我有一个完整的可行的来源$ C $ C演示此问题:<一href="https://www.dropbox.com/s/jok9tz5ktvfcteo/viewpagerbug.zip">https://www.dropbox.com/s/jok9tz5ktvfcteo/viewpagerbug.zip
任何变通方法来解决这个问题?
我已经下载了code和出现问题,因为你不处理这些片段
的权利。大多数precisely你使用嵌套片段
在 ViewPager
根据片段
和该 ViewPager
创建适配器是这样的:
MyFragmentPagerAdapter myFragmentPagerAdapter =新MyFragmentPagerAdapter(this.getFragmentManager());
相反,你应该使用 getChildFragmentManager()
绑定嵌套的片段:
MyFragmentPagerAdapter myFragmentPagerAdapter =新MyFragmentPagerAdapter(this.getChildFragmentManager());
此外,你应该不是通过构造一个片段
传递数据的数据将无法生存的配置变化,不好的事情将开始出现。使用捆绑
代替。
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 Fragments are being toggle.
- 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.
- Black color fragment was shown.
- Toggling.
- View pager, which can swipe between purple and blue fragments shown.
- Toggling.
- Black color fragment was shown.
- Toggling.
- Nothing shown, as MyFragmentPagerAdapter's getItem is not being triggered.
I think my situation is similar to FragmentPagerAdapter getItem is not called
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?
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?
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());
Instead, you should be using getChildFragmentManager()
to bind the nested fragments:
MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(this.getChildFragmentManager());
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没有被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!