Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Android Fragment

I just finished Ben Deitch's Android Fragments course and I have to say bravo. I enjoyed it a lot. One thing I had hoped to get out of it thought was creating and using fragments for an undetermined amount of 'tabs'

I had an idea to upgrade the stormy weather app. My idea was to create a single fragment that always displayed the weather at the device's current location. Then allow the user to add a pane using the tabs (without actually displaying to tabs just the swipe left/right) and each new tab pane would have weather for a location the user specified. Its essentially reusing the first fragment that displays all the weather data but only changed the info displayed by what's relevant to the location.

The part I can't figure out is how to account for loading the app without knowing how many tab will be in the app after the user begins using it. Every tutorial I've found has a defined number of tabs be it the messenger app or the recipe app (ingredients / Directions) or any tutorials I have found on youtube.

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

I think you need a ViewPager with a FragmentStatePagerAdapter instead of a FragmentPagerAdapter which is the adapter used in this course. Take a look at these links:

http://developer.android.com/training/implementing-navigation/lateral.html

https://guides.codepath.com/android/ViewPager-with-FragmentPagerAdapter#dynamic-viewpager-fragments