Adding child Fragment to Parent Fragment withing a ViewPager in Android

前端 未结 1 1578
梦如初夏
梦如初夏 2021-01-15 08:08

I\'m developing an app that has some tabs,something like this \"app

Each tab is a Fragment

相关标签:
1条回答
  • 2021-01-15 08:33

    If what you want is replace a fragment inside the viewpager with another fragment, take a look at this. However I would suggest you to create another Fragment (a container), and move your viewpager and fragments inside of this new fragment:

    enter image description here

    This way when the user clicks an item of a list, you can replace the MainFragment with a Details Fragment.

    About the ChildFragmentManager(), it is used when you have nested fragments. If you change your implementation as I suggested, you would need to pass a ChildFragmentManager to your PagerAdapter:

    viewPager.setAdapter(new AdapterView(getChildFragmentManager()));
    

    Another thing about your commented lines of code inside VideosFragment. Make your activity commit the fragment transaction instead of doing it inside the Fragment. For a better explanation, read this.

    0 讨论(0)
提交回复
热议问题