gpt4 book ai didi

android - FragmentPagerAdapter 和 FragmentStatePagerAdapter 有什么区别?

转载 作者:IT老高 更新时间:2023-10-28 12:49:48 25 4
gpt4 key购买 nike

FragmentPagerAdapterFragmentStatePagerAdapter有什么区别?

关于 FragmentPagerAdapter Google 的指南说:

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,这只是一个简单的ListFragmentFragment3 也是一个 ListFragment

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

最佳答案

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com