Raykud
Raykud

Reputation: 2484

Difference FragmentPagerAdapter and PagerAdapter

I read the documentation for android pageadapter and fragmentpageadapter and I didn't see any difference. I mean one is a fragment and one isn't but.. is that all? I don't have really much experience with fragments so maybe thats why I don't notice any difference. So whats the difference if I use a FragmentPagerAdapter or a PagerAdapter??

Upvotes: 21

Views: 15513

Answers (2)

jegumi
jegumi

Reputation: 1172

The difference is that you can use Fragments inside a FragmentPageAdapter. If you want to have fragments that are going to be used in other parts of your code this is your Adapter. Otherwise if you only want to have code that isn't going to be reused, you can use PagerAdapter.

Upvotes: 23

LOG_TAG
LOG_TAG

Reputation: 20609

Implementation of PagerAdapter that uses a Fragment to manage each page. But I highly recommend to use FragmentStatePagerAdapter class also handles saving and restoring of fragment's state.

FragmentStatePagerAdapter version of the pager is more useful when there are a large number of pages, working more like a list view. When pages are not visible to the user, their entire fragment may be destroyed, only keeping the saved state of that fragment. This allows the pager to hold on to much less memory associated with each visited page as compared to FragmentPagerAdapter at the cost of potentially more overhead when switching between pages.

When using FragmentPagerAdapter the host ViewPager must have a valid ID set.

Subclasses only need to implement getItem(int) and getCount() to have a working adapter.

Here is an example implementation of a FragmentStatePagerAdapter pager containing fragments of lists

Upvotes: 9

Related Questions