• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Multiple Fragments in ViewPager

Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reading my post.

I have an application being developed which uses ViewPager. The primary use of the View Pager is to obtain swipe functions in navigating the pages. I'm required to enhance the application by introducing Fragments which render multiple fragments based on the screen resolution/orientation. I've searched the web bout I could not find an example with all these elements; where multiple fragments are used within a ViewPager. Can someone please help me out.

Thank you.
Posts: 4179
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic