Shared publicly  - 
 
Nesting Fragments in a ViewPager.

A new solution to an old problem: Back in August Mark Murphy of +CommonsWare asked Twitter how to show multiple children in a ViewPager and received a few interesting responses. My own solution was a custom view pager containing one ViewGroup (with its own view hierarchy) per page, similar to  +Nicolas Klein's  but a bit more generic. I refactored that code this morning to use a simple FragmentPagerAdapter instead. Each page consists of a single fragment which in turn contains more fragments. Here is an example, compatible alllll the way back to API v4.

I got rid of good bit of code, and what was left was much more elegant. Nested fragments are really nice.
5
6
Edouard Marquez's profile photoPhilippe Banwarth's profile photoZhelyazko Atanasov's profile photoJoe AmRhein's profile photo
10 comments
 
Awesome, can't wait to give this a read. Can you recommend any good fragment tutorials?
 
Nested fragments are very powerful. We started using it to dynamically insert content defined by a fragment into a DialogFragment.
Phil H
 
+Marty Ballard the best thing to do is to get on and build an activity, or download Action Bar Sherlock and its samples. You'll be all fragmented in no time at all, I was and I've not looked back...
 
+Joel Pedraza epic response: the 'multiple children in a ViewPager' "challenge" was more about providing a Gallery-like solution using ViewPager than just displaying multiple children - in fact, populating multiple children into a single page was just one of the potential solutions uncovered. So the goal was to have neighbouring pages be displayed, which naturally means each page should be less wide than the screen width. Whereas ViewPager's typical (and intended it seems) use is to display 1 page at a time, each page taking up the screen width.
The only solution on offer in the +CommonsWare post that I've tried is the setChildClipping(false) one, which works OK up until the point you want to use a PageTransformer where it yields bigger problems (neighbouring pages won't be displayed in the 'out state' of the transformation without lots of coercing).
Getting back to your post, from a quick look at your project I don't think you're addressing that problem exactly, but rather an idea that stemmed off it in providing a general responsive layout solution using ViewPager.
And so, what you've done is a really good solution for providing alternative layouts for tablets: e.g. create a layout-sw600dp-land alternative for each page's layout that contains more content/fragments.
Also having that ParentFragment for each page provides the best solution I've found for alternating the content of a single page...because changing the page Fragment itself requires re-instantiation thus resetting of the ViewPager's adapter thus reloading of neighbouring pages, and is just a nasty way to do things.
 
+Linden Darling From Mark's post "While this was not what I had in mind from my Twitter inquiry, Dave’s approach does solve a different problem: how does one really replace Gallery, now that it is deprecated?"

I'm using my approach to do what you've described, show two-pane layouts on sw600dp. Some master/detail flows reside inside the ViewPager with ActionBar tabs and swipes to navigate between them. As you can imagine my selection callbacks just got much cleaner :)

Oh and yes, Fragments rock!
 
+Linden Darling No worries. TBH a Gallery replacement is a much more interesting problem to discuss anyways. I remain thankful that it's not one I've had a need to solve.
 
Hey Joel, I was having a problem using multiple fragments with the ViewPager.  It wasn't exactly like what you posted here, but what you posted helped me figure out what my problem was.  Anyways, just wanted to thank you.
Add a comment...