FragmentPagerAdapter 和 FragmentStatePagerAdapter 有什么区别? [英] What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?
问题描述
FragmentPagerAdapter
和 FragmentStatePagerAdapter
有什么区别?
关于 FragmentPagerAdapter
Google 的指南说:
About FragmentPagerAdapter
Google's guide says:
这个版本的寻呼机最适合在有少量通常更多的静态片段要被分页,例如一组选项卡.用户访问的每个页面的片段将保留在内存,尽管它的视图层次结构在不可见时可能会被破坏.这可能会导致使用大量内存,因为碎片实例可以保持任意数量的状态.对于更大的集合的页面,考虑 FragmentStatePagerAdapter
.
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. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state. For larger sets of pages, consider
FragmentStatePagerAdapter
.
关于FragmentStatePagerAdapter
:
这个版本的寻呼机在人数较多的时候比较有用的页面,更像是一个列表视图.当页面不可见时用户,他们的整个片段可能会被销毁,只保留该片段的保存状态.这允许寻呼机保持很多与每个访问页面相关的内存更少FragmentPagerAdapter
以潜在的更多开销为代价在页面之间切换.
This 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.
所以我只有 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 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!