posted 9 years ago
I have no idea if this is the most efficient way to do it, but there are problems (at least on older versions of Android) of nesting Fragments inside Fragments. So what I have done is use FrameLayouts in my layout XMLs and then programatically attaching the desired fragment to the FrameLayout when the parent fragment is created. Here is an example.
My main activity is like this. It holds the ViewPager, which has no special tricks to it, really.
I am showing minimum needed for tracking one of the fragments here: StatusFragment, which has smaller fragments it uses. Here is the StatusFragment's UI in portrait and landscape, respectively:
It includes a few sub-layouts. The R.layout.section_status_run layout is this, for example:
And here is the pertinent part of the StatusFragment:
Basically, it takes other fragments and attaches them to FrameLayouts present in the R.layout.fragment_status layout. For example, the R.layout.fragment_status layout includes the R.layout.section_status_run layout which has a FrameLayout with an id of R.id.status_image_display. Line 47/48 of the above code attaches the element to the FrameLayout using a utility class. The utility method looks like this: