http://developer.android.com/about/versions/android-4.2.html#NestedFragments
You can now embed fragments inside fragments. This is useful for a variety of situations in which you want to place dynamic and re-usable UI components into a UI component that is itself dynamic and re-usable. For example, if you useViewPager to create fragments that swipe left and right and consume a majority of the screen space, you can now insert fragments into each fragment page.
To nest a fragment, simply call getChildFragmentManager() on the Fragment in which you want to add a fragment. This returns a FragmentManager that you can use like you normally do from the top-level activity to create fragment transactions. For example, here’s some code that adds a fragment from within an existing Fragment class:
Fragment videoFragment = new VideoPlayerFragment(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.add(R.id.video_fragment, videoFragment).commit();
From within a nested fragment, you can get a reference to the parent fragment by calling getParentFragment().
The Android Support Library also now supports nested fragments, so you can implement nested fragment designs on Android 1.6 and higher.
Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically.
本文介绍了如何在Android中将Fragment嵌套在另一个Fragment内,这是一个非常实用的功能,尤其是在需要创建可复用的UI组件时。文章提供了具体的代码示例来展示如何通过调用getChildFragmentManager()方法来添加子Fragment。

483

被折叠的 条评论
为什么被折叠?



