FragmentPagerAdapter的getItem没有被触发 [英] FragmentPagerAdapter getItem is not being triggered

查看:151
本文介绍了FragmentPagerAdapter的getItem没有被触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,使用 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片段被触发。

  • Col​​orFragment - 一个简单的片段,它填补了它的背景与纯黑色的颜色
  • 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);
        }
    }
}
 

不过,我遇到切换在怪异的行为。

  1. 黑色片段所示。
  2. 切换。
  3. 查看传呼机,它可以紫色和蓝色片段之间刷卡所示。
  4. 切换。
  5. 黑色片段所示。
  6. 切换。
  7. 没有显示,由于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.

  1. Black color fragment was shown.
  2. Toggling.
  3. View pager, which can swipe between purple and blue fragments shown.
  4. Toggling.
  5. Black color fragment was shown.
  6. Toggling.
  7. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆