• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

android - FragmentPagerAdapter 和 FragmentStatePagerAdapter 有什么区别?

[复制链接]
菜鸟教程小白 发表于 2022-8-1 01:20:36 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap