• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Fragments in ViewPager

 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic