FragmentPagerAdapter 和有什么区别和 FragmentStatePagerAdapter ?
关于 FragmentPagerAdapter 谷歌的指南说:
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 :
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个 fragment 。但它们都是具有大量数据的独立模块。
Fragment1 处理一些数据(用户输入)并通过 Activity 将其传递给 Fragment2 , 这只是一个简单的 ListFragment . Fragment3 也是 ListFragment .
所以我的问题是 : 我应该使用哪个适配器? FragmentPagerAdapter 或 FragmentStatePagerAdapter ?
Best Answer-推荐答案 strong>
就像文档说的那样,这样想。如果你要做一个像阅读器这样的应用程序,你不会希望一次将所有 fragment 加载到内存中。您想加载和销毁Fragments 当用户阅读时。在这种情况下,您将使用 FragmentStatePagerAdapter .如果您只显示 3 个不包含大量数据的“标签”(如 Bitmaps ),那么 FragmentPagerAdapter 可能很适合你。另外,请记住 ViewPager 默认情况下会将 3 个 fragment 加载到内存中。第一个Adapter 你提到可能会破坏View 层次结构并在需要时重新加载,第二个 Adapter 只保存 Fragment 的状态并完全销毁它,如果用户随后返回该页面,则检索状态。
关于android - FragmentPagerAdapter 和 FragmentStatePagerAdapter 有什么区别?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/18747975/
|