background

I often see ViewPager adopt different adapters for assignment, PagerAdapter, FragmentPagerAdapter, FragmentStatePagerAdapter, has been in my heart there is a doubt, just what is the difference between them? ViewPager Selects the corresponding Adapter in the service scenario.

If you also have the same confusion, the following will follow my footsteps, learn together, I believe that through the following learning, your heart of the confusion I will solve for you.

Official website reference article

If English is better, suggest to see the official website, explain better.

Developer. The android. Google. Cn/reference/k…

Developer. The android. Google. Cn/reference/k…

Developer. The android. Google. Cn/reference/k…

Application scenarios of the PagerAdapter

Application scenarios

Each page of a ViewPager is a View, such as a round play diagram.

advantages

For simple views using PagerAdapter, very efficient.

Disadvantages: Complex views with interactive views are not suitable for this scenario.

Application scenarios and advantages and disadvantages of FragmentPagerAdapter

Application scenarios

There are few pages (for example, 5 or 6), and each page is a Fragment, such as the home page of netease Cloud Music.

advantages

When switching, it’s very smooth and smooth.

disadvantages

1. Although View Hierarchy may be destroyed when pages are not visible, the fragment for each page is stored in memory.

2, will consume a certain amount of memory.

FragmentStatePagerAdapter application scenario

Application scenarios

When a large number of pages, this time can use FragmentStatePagerAdapter, more efficient, like ListView.

advantages

1. Fragments are destroyed when the page is not visible.

2. When you have a lot of pages, it doesn’t take up a lot of memory.

3, FragmentStatePagerAdapter to limit page outside of recycling, FragmentStatePagerAdapter when destruction of fragments, will save the data, and then in the next create fragments of time, The data will be rendered again.

Disadvantages:

Since the page is being reloaded, the data is being rerendered.