FragmentPagerAdapter getItem 没有被触发

FragmentPagerAdapter getItem is not being triggered(FragmentPagerAdapter getItem 没有被触发)

本文介绍了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.

  1. 显示黑色片段.
  2. 切换.
  3. 查看分页器,可以在显示的紫色和蓝色片段之间滑动.
  4. 切换.
  5. 显示黑色片段.
  6. 切换.
  7. 没有显示,因为 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.最准确地说,您在基于 ViewPagerFragment 中使用嵌套的 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 没有被触发

基础教程推荐