Is there any alternative to nested Fragments?

后端 未结 5 1977
醉酒成梦
醉酒成梦 2021-01-12 17:42

I\'ve read around that nesting Fragments should be avoided (eg. here), but I can\'t see how to do the following:

I\'m working on a tab application (android:min

5条回答
  •  情深已故
    2021-01-12 17:51

    You can use ViewPager and the FragmentPagerAdapter for this. ViewPager allows users to swipe between views or (in your case) Fragments. To show tablike-controls, use ViewPagerIndicator.

    Using the layout you described, instead of loading a Fragment into the FrameLayout, inflate it with a layout like this:

    
    
    
        
        
    
    
    

    Next, assign a FragmentPagerAdapter to your ViewPager, which will then load your Fragments.

    Also take a look at my answer here. It gives a more detailed example. Note however that it extends PagerAdapter, instead of the FragmentPagerAdapter you should be using.

提交回复
热议问题