问题
Is it possible to put a fragment inside a fragment in Android? Consider this: Say, I implement swipeable tabs using Fragment and ViewPager. Now, inside each of these swipeable fragments, I want to implement another fragment - kind of like a fragment nested inside another fragment. But a fragment needs to be attached to an Activity class. So how can this be done?
回答1:
Is it possible to put a fragment inside a fragment in Android?
Using the Android Support package's backport of fragments, yes. Also, native fragments on Android 4.2 and higher (API Level 17+) support nested fragments. However, native fragments from API Level 11-16 do not.
Say, I implement swipeable tabs using Fragment and ViewPager. Now, inside each of these swipeable fragments, I want to implement another fragment - kind of like a fragment nested inside another fragment. But a fragment needs to be attached to an Activity class. So how can this be done?
This sample project demonstrates having fragments in a ViewPager
and having the ViewPager
itself be in a fragment. The key is that the FragmentManager
you supply to your FragmentPagerAdapter
must be the child fragment manager of the outer fragment (i.e., getChildFragmentManager()
).
来源:https://stackoverflow.com/questions/17494294/fragment-inside-a-fragment-with-viewpager