FragmentPagerAdapter inside fragment

I'm having trouble implementing a design based on multiple ViewPagers.

At a high level, I have a FragmentActivity with only FrameLayout as the 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 the main fragment with some TextViews and ImageViews. Fragment 2 has a ViewPager and a FragmentPagerAdapter, which passes it a few simple snippets. Fragment 3 has a ViewPager and FragmentPagerAdapter, which passes it a few simple fragments (other than fragment 2)

In my FragmentActivity onCreate (), I get a FragmentManager and start a transaction to replace everything in my FrameLayout with a new instance of fragment 2.

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

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

The problem occurs when I try to return Fragment 2 to FrameLayout with a different transaction replacement. I see that my PagerIndicater is at the top, but my pages are blank.

I tried just creating a new instance of my fragment 2 and invoking a replacement transaction. I also tried to set the tag on my snippets when I call the replacement and add the findFragmentByTag check before the replacement instead of creating a new instance. Both gave me the same blank page result after my second replacement.

For reference, My first design was just FragmentActivity with ViewPager and ViewIndicater. I had only fragment 2 and fragment 3 from my description above and a menu item to switch between them. To switch, I had two different FragmentPagerAdapters that were defined and just called ViewPager.setAdapter to set the selected FragmentPagerAdapter. This works fine, but now I need a new top-level snippet that doesn't use ViewPager at all. That's why I decided to move my ViewPagers to my own snippets. My idea is that I just swap my fragments for FrameLayout.

I donโ€™t have my code in front of me right now, so I canโ€™t publish it, but I will add the code to my question tomorrow to facilitate the answers.

+8
android android-fragments android-viewpager fragmentpageradapter android-nested-fragment
source share
2 answers

This question is a possible duplicate. Transition to FragmentPagerAdapter โ†’ fragments are empty .

If your application can handle it (API 17), use getChildFragmentManager () . This problem occurs when using a fragment to place your ViewPager and using the FragmentPagerAdapter. Going to the FragmentStatePagerAdapter also helped fix the problem, but I still think using getChildFragmentManager () is the smartest thing.

+14
source share

brockoli you can use is not a "good way". But for me it worked.

You can use 2 layouts in a view where you link fragments. First, a fragment with fragments. The second is for other fragments.

When replacing a fragment, the fragments do not replace, but only change the visibility of the first layout to leave, and add a new fragment to the second layout. On the back panel, remove the fragment from the second layout and set the visibility for the visibility of the first layout.

0
source share

All Articles