Fragments within Fragments

前端 未结 6 579
名媛妹妹
名媛妹妹 2020-11-22 09:47

I\'m wondering if this is actually a bug in the Android API:

I have a setup like so:

┌----┬---------┐
|    |         |
|  1 |    2    |
|    |┌------         


        
6条回答
  •  不思量自难忘°
    2020-11-22 10:37

    Nested fragments are supported in android 4.2 and later

    The Android Support Library also now supports nested fragments, so you can implement nested fragment designs on Android 1.6 and higher.

    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();
    

    To get more idea about nested fragments, please go through these tutorials
    Part 1
    Part 2
    Part 3

    and here is a SO post which discuss about best practices for nested fragments.

提交回复
热议问题