FragmentPagerAdapter内片段片段、FragmentPagerAdapter

2023-09-06 04:36:22 作者:独领风骚

我有一点点实现设计的麻烦围绕多个ViewPagers。

I'm having a bit of trouble implementing a design based around multiple ViewPagers.

在一个较高的水平,我都只是一个的FrameLayout,因为它的内容FragmentActivity。我有我想要显示3个不同的片段。所有3是全屏幕的,只有1将在同一时间内使用。

At a high level, I have a FragmentActivity with just a FrameLayout as it's content. I have 3 different Fragments that I want to display. All 3 are full screen and only 1 will be used at a time.

片段1是与一些TextViews和ImageViews一个基本片段。 片段2具有ViewPager和FragmentPagerAdapter哺养它一些简单的片段。 片段3具有ViewPager和FragmentPagerAdapter哺养它一些简单的片段(即从片段2不同)

Fragment 1 is a basic fragment with some TextViews and ImageViews. Fragment 2 has a ViewPager and a FragmentPagerAdapter that feeds it several simple fragments. Fragment 3 has a ViewPager and a FragmentPagerAdapter that feeds it several simple fragments (that are different from Fragment 2)

在我FragmentActivity的onCreate()我得到的FragmentManager并开始交易,以取代无论是在我的FrameLayout与片段2的新实例。

In my FragmentActivity onCreate() I get the FragmentManager and begin a transaction to replace whatever is in my FrameLayout with a new instance of Fragment 2.

在这一点上一切工作正常。我在片段2 ViewPager完美的作品。

At this point everything is working as expected. My ViewPager in Fragment 2 works perfectly.

现在我有一个替换片段2在我的FrameLayout与片段3这也能正常工作的新实例的菜单选项。

Now I have a menu option that replaces the Fragment 2 in my FrameLayout with a new instance of Fragment 3. This also works fine.

当我试图把片段2回的FrameLayout替换为另一个事务的问题就出现了。我看到我的PagerIndicater在顶部,但我的页面是空白。

The problem arises when I try to put Fragment 2 back into the FrameLayout with another replace transaction. I see my PagerIndicater at the top, but my pages are blank.

我试着只创建我的片段2的一个新实例,并调用替换事务。我也试着设置变量在我的片段,当我打电话取代之前我的更换而不是创建一个新的实例添加findFragmentByTag检查。后两者给我的空白页的相同的结果,我的第二个替代。

I've tried just creating a new instance of my Fragment 2 and calling a replace transaction. I've also tried setting a tag on my Fragments when I call replace and adding a findFragmentByTag check before my replace instead of creating a new instance. Both gave me the same result of blank pages after my second replace.

有关参考 我的第一个设计是简单地用一个ViewPager和ViewIndicater一个FragmentActivity。我只有片段2和片段3从我上面的描述和菜单选项,它们之间进行切换。要切换我不得不定义2个不同的FragmentPagerAdapters,只是叫ViewPager.setAdapter设置所选FragmentPagerAdapter。这是工作的很好,但现在我需要的是不使用ViewPager在所有新顶级片段。这就是为什么我决定把我的ViewPagers伸到自己的片段。我的想法是,我只是换在我的碎片到的FrameLayout。

For reference My first design was simply a FragmentActivity with a ViewPager and a ViewIndicater. I only had Fragment 2 and Fragment 3 from my description above and a menu option to switch between them. To switch I had 2 different FragmentPagerAdapters defined and just called ViewPager.setAdapter to set the selected FragmentPagerAdapter. This was working perfectly, but now I need a new top level Fragment that isn't using ViewPager at all. This is why I decided to move my ViewPagers out into their own Fragments. My idea being that I would just swap in my fragments to a FrameLayout.

我没有我的code在我的面前,现在,所以我不能发表任何,但我会添加一些code到明天,以帮助推动回答我的问题。

I don't have my code in front of me right now so I can't post any, but I'll add some code to my question tomorrow to help facilitate answers.

推荐答案

这个问题是Navigating回到FragmentPagerAdapter - >片段是空的。

如果您的应用程序可以处理它(API 17),使用getChildFragmentManager().这个问题似乎使用片段时,承载您ViewPager并使用FragmentPagerAdapter发生。更改为FragmentStatePagerAdapter似乎解决问题为好,但我还是觉得用getChildFragmentManager()是做的最明智的事情。

If your app can handle it (API 17), use getChildFragmentManager(). This problem seems to occur when using a Fragment to host your ViewPager and using FragmentPagerAdapter. Changing to FragmentStatePagerAdapter seemed to fix the problem as well, but I still think using getChildFragmentManager() is the smartest thing to do.

 
精彩推荐
图片推荐