Android Programming The Big Nerd Ranch Guide, 3rd Edition

(Brent) #1

Chapter 11  Using ViewPager


224

Figure 11.5  FragmentPagerAdapter’s fragment management


Which kind of adapter you should use depends on your application. FragmentStatePagerAdapter is
generally more frugal with memory. CriminalIntent is displaying what could be a long list of crimes,
each of which will eventually include a photo. You do not want to keep all that information in memory,
so you use FragmentStatePagerAdapter.


On the other hand, if your interface has a small, fixed number of fragments, FragmentPagerAdapter
would be safe and appropriate. The most common example of this scenario is a tabbed interface. Some
detail views have enough details to require two screens, so the details are split across multiple tabs.
Adding a swipeable ViewPager to this interface makes the app tactile. Keeping these fragments in
memory can make your controller code easier to manage. Plus, because this style of interface usually
has only two or three fragments per activity, there is little danger of running low on memory.


For the More Curious: How ViewPager Really Works


The ViewPager and PagerAdapter classes handle many things for you behind the scenes. This section
will supply more details about what is going on back there.


A caveat before we get into this discussion: You do not need to understand the nitty-gritty details in
most cases.


But, if you need to implement the PagerAdapter interface yourself, you will need to know how the
ViewPager-PagerAdapter relationship differs from an ordinary RecyclerView-Adapter relationship.

Free download pdf