问题描述
FragmentPagerAdapter
和FragmentStatePagerAdapter
有什么区别?
关于FragmentPagerAdapter
Google指南说:
About FragmentPagerAdapter
Google's guide says:
关于FragmentStatePagerAdapter
:
所以我只有3个片段.但是它们都是具有大量数据的独立模块.
So I have just 3 fragments. But all of them are separate modules with a large amount of data.
Fragment1
处理一些数据(用户输入),并通过活动将其传递到Fragment2
中,这只是一个简单的ListFragment
. Fragment3
也是ListFragment
.
Fragment1
handles some data (which users enter) and passes it via activity into Fragment2
, which is just a simple ListFragment
. Fragment3
is also a ListFragment
.
我的问题是:我应该使用哪个适配器? FragmentPagerAdapter
或FragmentStatePagerAdapter
?
So my questions are: Which adapter should I use? FragmentPagerAdapter
or FragmentStatePagerAdapter
?
推荐答案
就像文档所说的那样,请仔细考虑.如果要像书本阅读器这样的应用程序,则不希望立即将所有片段都加载到内存中.您希望在用户读取时加载和销毁Fragments
.在这种情况下,您将使用FragmentStatePagerAdapter
.如果仅显示3个标签",其中不包含大量数据(例如Bitmaps
),则FragmentPagerAdapter
可能很适合您.另外,请记住,默认情况下ViewPager
会将3个片段加载到内存中.您提到的第一个Adapter
可能会破坏View
层次结构并在需要时重新加载它,第二个Adapter
仅保存Fragment
的状态并完全破坏它,如果用户随后返回该页面,则状态被检索.
Like the docs say, think about it this way. If you were to do an application like a book reader, you will not want to load all the fragments into memory at once. You would like to load and destroy Fragments
as the user reads. In this case you will use FragmentStatePagerAdapter
. If you are just displaying 3 "tabs" that do not contain a lot of heavy data (like Bitmaps
), then FragmentPagerAdapter
might suit you well. Also, keep in mind that ViewPager
by default will load 3 fragments into memory. The first Adapter
you mention might destroy View
hierarchy and re load it when needed, the second Adapter
only saves the state of the Fragment
and completely destroys it, if the user then comes back to that page, the state is retrieved.
这篇关于FragmentPagerAdapter和FragmentStatePagerAdapter有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!