OffScreenLimit 设置为 1 的 ViewPager 的 FragmentPagerAdapter
和 FragmentStatePagerAdapter
有什么区别?
关于 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.
我想如果我将 offscreenlimit 设置为 1,它会破坏当前 fragment 的两个下一个 fragment 之外的所有 fragment ,并且它的行为类似于 FragmentStatePagerAdapter。这是正确的吗?
最佳答案
区别与文档中所写的完全一样。但这可能有点令人困惑。
FragmentPagerAdapter
将其 fragment 保存在 FragmentManager
中处于分离状态,当它们不可见时(当它们超过屏幕外限制边界时)和 FragmentStatePagerAdapter
将它们从 FragmentManager
中移除。
offscreenPageLimit
有点不同。其中的所有 fragment 都与 UI 相关联。一旦他们过去了,他们要么是removed , 或 detached .
您可以在 FragmentPagerAdapter 的来源中看到它(第 121 行)和 FragmentStatePagerAdapter (第 144 行)
关于android - FragmentPagerAdapter 与 OffScreenLimit 设置为 1 的 ViewPager 和 FragmentStatePagerAdapter 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30235335/