OGeek|极客世界-中国程序员成长平台

标题: android - FragmentPagerAdapter 和 FragmentStatePagerAdapter 有什么区别? [打印本页]

作者: 菜鸟教程小白    时间: 2022-8-1 01:20
标题: android - FragmentPagerAdapter 和 FragmentStatePagerAdapter 有什么区别?

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 .

所以我的问题是 : 我应该使用哪个适配器? FragmentPagerAdapterFragmentStatePagerAdapter ?



Best Answer-推荐答案


就像文档说的那样,这样想。如果你要做一个像阅读器这样的应用程序,你不会希望一次将所有 fragment 加载到内存中。您想加载和销毁Fragments当用户阅读时。在这种情况下,您将使用 FragmentStatePagerAdapter .如果您只显示 3 个不包含大量数据的“标签”(如 Bitmaps ),那么 FragmentPagerAdapter可能很适合你。另外,请记住 ViewPager默认情况下会将 3 个 fragment 加载到内存中。第一个Adapter你提到可能会破坏View层次结构并在需要时重新加载,第二个 Adapter只保存 Fragment 的状态并完全销毁它,如果用户随后返回该页面,则检索状态。

关于android - FragmentPagerAdapter 和 FragmentStatePagerAdapter 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18747975/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4