gpt4 book ai didi

android - Offscreenpagelimit vs setItemViewCacheSize ViewPager2

转载 作者:行者123 更新时间:2023-12-04 12:02:59 28 4
gpt4 key购买 nike

我正在使用 ViewPager2,
最新版本 'androidx.viewpager2:viewpager2:1.0.0-beta04'
我有 10 页,每一页都是 Fragment。

如果我设置 viewpager2.offscreenPageLimit = 1我必须滚动到第 5 页才能破坏第一页。

发生了什么???,当我滚动到第 3 页时,第一页应该被破坏了吧??

然后我尝试另一种方式使它正确

(viewpager2.getChildAt(0) as RecyclerView).layoutManager?.isItemPrefetchEnabled = false
(viewpager2.getChildAt(0) as RecyclerView).setItemViewCacheSize(1)

这样,当我滚动到第 3 页时,第一页将被破坏,它现在正在工作

任何人都可以向我解释这两个属性之间的区别吗?

非常感谢

最佳答案

/**
* Sets whether the LayoutManager should be queried for views outside of
* its viewport while the UI thread is idle between frames.
*
* <p>If enabled, the LayoutManager will be queried for items to inflate/bind in between
* view system traversals on devices running API 21 or greater. Default value is true.</p>
*
* <p>On platforms API level 21 and higher, the UI thread is idle between passing a frame
* to RenderThread and the starting up its next frame at the next VSync pulse. By
* prefetching out of window views in this time period, delays from inflation and view
* binding are much less likely to cause jank and stuttering during scrolls and flings.</p>
*
* <p>While prefetch is enabled, it will have the side effect of expanding the effective
* size of the View cache to hold prefetched views.</p>
*
* @param enabled <code>True</code> if items should be prefetched in between traversals.
*
* @see #isItemPrefetchEnabled()
*/
(viewpager2.getChildAt(0) as RecyclerView).layoutManager?.isItemPrefetchEnabled = false


/**
* Set the number of offscreen views to retain before adding them to the potentially shared
* {@link #getRecycledViewPool() recycled view pool}.
*
* <p>The offscreen view cache stays aware of changes in the attached adapter, allowing
* a LayoutManager to reuse those views unmodified without needing to return to the adapter
* to rebind them.</p>
*
* @param size Number of views to cache offscreen before returning them to the general
* recycled view pool
*/
(viewpager2.getChildAt(0) as RecyclerView).setItemViewCacheSize(0)

关于android - Offscreenpagelimit vs setItemViewCacheSize ViewPager2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58301987/

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