本文介绍了RecyclerView.setItemViewCacheSize 和 RecycledViewPool.setMaxRecycledViews 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说 setItemViewCacheSize

设置在将它们添加到可能共享的回收视图池.

setMaxRecycledViews

设置在池中保存的最大 ViewHolder 数之前丢弃.

但是它们不是都用作获取视图的缓存吗(即,第一个设置 RV 缓存的视图数量,而第二个设置 RVP 缓存的视图数量)?

But don't they both function as a cache where views are taken from (i.e., the first sets the number of views cached by the RV, while the second sets that of the RVP)?

此外,当需要视图时,它首先从 RVP 还是从 RV 的缓存中获取?

Also, when a view is needed, where is it taken first, from the RVP or from the RV's cache?

对于简单的非嵌套回收器视图,两者的最佳(滚动方式,忽略内存)配置是什么?

And what's the optimal (scrolling-wise, ignoring memory) configuration for the two for a simple unnested recyclerview?

推荐答案

这里是 setItemViewCacheSize() 的完整文档:

Here's the full documentation for setItemViewCacheSize():

在将它们添加到潜在共享的回收视图池之前设置要保留的屏幕外视图的数量.

屏幕外视图缓存始终了解附加适配器中的更改,从而允许 LayoutManager 重用那些未修改的视图,而无需返回适配器重新绑定它们.

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.

换句话说,当你滚动 RecyclerView 时,有一个几乎完全离开屏幕的视图,RecyclerView 会保留它,这样你就可以将它滚动回视图而无需重新执行 onBindViewHolder().

In other words, when you scroll the RecyclerView such that there's a view that is just barely completely off-screen, the RecyclerView will keep it around so that you can scroll it back into view without having to re-execute onBindViewHolder().

这与 recycled view pool 不同,后者是 RecyclerView 表示不再需要的视图池,但保留它以避免膨胀新的昂贵任务视图.

This is different from the recycled view pool, which is a pool of views that the RecyclerView has said it doesn't need anymore, but which is kept around to avoid the expensive task of inflating new views.

简而言之,项目视图缓存"以 RecyclerView 可以避免调用 both onCreateViewHolder()onBindViewHolder() 的方式保存元素,而回收的视图池保存这样的元素RecyclerView 可以避免调用 onCreateViewHolder() 但仍然必须调用 onBindViewHolder().

In short, the "item view cache" holds elements in such a way that the RecyclerView can avoid calling both onCreateViewHolder() and onBindViewHolder(), whereas the recycled view pool holds elements such that the RecyclerView can avoid calling onCreateViewHolder() but will still have to call onBindViewHolder().

此外,当需要视图时,它首先从 RVP 还是从 RV 的缓存中获取?

我认为这并不重要,我也不知道一个确切的定义,但一般我认为你可以想象刚刚退出设备视口然后返回视口的视图将从项目视图缓存",而出现在屏幕上但以前不在屏幕上的视图将来自回收的视图池.

I don't think it really matters, and I don't know of a precise definition, but generally I think you can imagine that views that have just exited the device's viewport and then return to the viewport will be taken from the "item view cache", while views that come on-screen but were not previously on-screen will come from the recycled view pool.

对于简单的非嵌套回收器视图,两者的最佳(滚动方式,忽略内存)配置是什么?

只需使用默认值即可.我永远不会考虑更改这些,除非我已经对我的应用程序进行了概要分析并且毫无疑问地确定默认设置对我不起作用.但是,如果我只相信你的话,忽略内存,缓存越大越好.但实际上,只需使用默认值即可.

Just use the defaults. I would never consider changing these unless I had profiled my app and determined beyond a doubt that the defaults weren't working for me. But, if I just take you at your word, ignoring memory, the larger the cache size the better. But really, just use the defaults.

这篇关于RecyclerView.setItemViewCacheSize 和 RecycledViewPool.setMaxRecycledViews 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:33