Nested Fragments

来源:互联网 发布:淘宝售后维修申请 编辑:程序博客网 时间:2024/06/04 22:37

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.


原创粉丝点击