How to Add a Fragment Inside a Viewpager Using Nested Fragment (Android 4.2)

How to add a Fragment inside a ViewPager using Nested Fragment (Android 4.2)

Assuming you have created the correct xml layouts. It is now very simple to display fragments in a ViewPager that is hosted by another Fragment.

The following is a parent fragment that displays child fragments:

class ParentViewPagerFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val root = inflater.inflate(R.layout.fragment_parent_viewpager, container, false)

val viewPager = root.findViewById(R.id.viewPager) as ViewPager
// Important: Must use the child FragmentManager or you will see side effects.
viewPager.adapter = MyAdapter(childFragmentManager)

val tabStrip = root.findViewById<TabLayout>(R.id.pagerTabStrip)
tabStrip.setupWithViewPager(viewPager)

return root
}

class MyAdapter internal constructor(fm: FragmentManager) : FragmentPagerAdapter(fm) {

override fun getCount(): Int = 4

override fun getItem(position: Int): Fragment {
val args = Bundle().apply { putInt(ChildFragment.POSITION_KEY, position) }
return ChildFragment.newInstance(args)
}

override fun getPageTitle(position: Int): CharSequence = "Tab $position"
}

companion object {
val TAG: String = ParentViewPagerFragment::class.java.name
}
}

It is important to use Fragment.getChildFragmentManager() when instantiating the FragmentPagerAdapter. Also note that you cannot use Fragment.setRetainInstance() on the children fragments or you'll get an exception. The imports were omitted for brevity.

Source code can be found at: https://github.com/marcoRS/nested-fragments

How to set a ViewPager inside a Fragment

Starting in Android 4.2, there are nested fragments.http://developer.android.com/about/versions/android-4.2.html#NestedFragments The support library now also includes support for this for older Android versions.

So you can do something like this:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
}

Full implementation available here: https://github.com/marcoRS/nested-fragments/tree/master/src/com/burnside/digital/nestedfragments

Adding child Fragment to Parent Fragment withing a ViewPager in Android

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:

Sample Image

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.

Fragment Inside Fragment

AFAIK, fragments cannot hold other fragments.


UPDATE

With current versions of the Android Support package -- or native fragments on API Level 17 and higher -- you can nest fragments, by means of getChildFragmentManager(). Note that this means that you need to use the Android Support package version of fragments on API Levels 11-16, because even though there is a native version of fragments on those devices, that version does not have getChildFragmentManager().

How is it possible to use a single fragment in place of multiple fragments to load same format contents inside of ViewPager

You can certainly do that. If you're using FragmentStatePagerAdapter, just initiate the same fragment with different arguments supplied according to it's position and then in fragment check for those arguments and make changes accordingly.



Related Topics



Leave a reply



Submit