Fragment inside a fragment with ViewPager

丶灬走出姿态 提交于 2019-12-24 22:40:21

问题


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

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