FragmentPagerAdapter inside Fragment

隐身守侯 提交于 2019-12-21 08:55:24

问题


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

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.

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)

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.

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

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.

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.

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.

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.

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.


回答1:


This question is a possible duplicate of Navigating back to FragmentPagerAdapter -> fragments are empty.

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.




回答2:


brockoli you can used not "good way". But for my it's worked.

You can use in view 2 layouts, where you bind fragments. First - for fragment with fragments. Second - for other fragments.

On replace fragment with fragments do not replace, but only change visibility first layout to gone and add new fragment to second layout. On back, remove fragment from second layout and set visibility for first layout to visible.



来源:https://stackoverflow.com/questions/12063802/fragmentpageradapter-inside-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!