使用ViewPager在片段内的片段 [英] Fragment inside a fragment with ViewPager
问题描述
是否可以在Android的片段中放入片段?考虑一下:说,我使用Fragment和ViewPager实现可滑动选项卡.现在,在每个可滑动片段中,我想实现另一个片段-有点像嵌套在另一个片段中的片段.但是需要将一个片段附加到Activity类.那怎么办呢?
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?
推荐答案
是否可以在Android的片段中放入片段?
Is it possible to put a fragment inside a fragment in Android?
使用Android支持程序包的片段反向移植,是的.此外,Android 4.2及更高版本(API级别17+)上的本机片段支持嵌套片段.但是,API级别11-16中的本机片段则没有.
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.
说,我使用Fragment和ViewPager实现可滑动选项卡.现在,在每个可滑动片段中,我想实现另一个片段-有点像嵌套在另一个片段中的片段.但是需要将一个片段附加到Activity类.那怎么办呢?
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?
此示例项目演示了ViewPager
本身位于一个片段中.关键是您提供给FragmentPagerAdapter
的FragmentManager
必须是外部片段(即getChildFragmentManager()
)的子片段管理器.
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()
).
这篇关于使用ViewPager在片段内的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!