7

我正在使用 ViewPager2,最新版本'androidx.viewpager2:viewpager2:1.0.0-beta04'

我有 10 页,每页都是片段。

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

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

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

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

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

谁能向我解释这两个属性之间的区别,好吗?

非常感谢

4

1 回答 1

1
/**
 * 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)
于 2021-07-09T00:02:09.790 回答