Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

like image 702
Raykud Avatar asked Dec 08 '11 03:12

Raykud


People also ask

What is the difference between PagerAdapter and FragmentPagerAdapter?

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 .

What is FragmentPagerAdapter?

Implementation of PagerAdapter that represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page. This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs.

How do I refresh FragmentPagerAdapter?

You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.


2 Answers

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.

like image 193
jegumi Avatar answered Oct 17 '22 01:10

jegumi


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

like image 23
LOG_TAG Avatar answered Oct 17 '22 02:10

LOG_TAG