RecyclerView中的setRecycledViewPool方法
setRecycledViewPool method in RecyclerView
我试图了解 setRecycledViewPool 方法在以下代码行中与 RecyclerView 一起实际执行的操作,其中 mrecyclerView
是 RecyclerView 对象:
mrecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
我阅读了 Android 文档 link 但我仍然不清楚它的作用。有人可以向我解释一下它的用途以及何时使用它吗?
我自己没有使用过它,但从我阅读文档的理解来看,它是一种使用在一个 RecyclerView 和另一个 RecyclerView 中回收的视图的方法。
因此,如果您有一个 RecyclerView 和一堆装饰好的 CardView,并且您想为另一个 RecyclerView 回收这些相同的视图,您可以将共享的 RecycledViewPool 传递给它。现在两个 RecyclerView 都将从共享视图池中获取。
来自docs:
Recycled view pools allow multiple RecyclerViews to share a common pool of scrap views. This can be useful if you have multiple RecyclerViews with adapters that use the same view types, for example if you have several data sets with the same kinds of item views displayed by a ViewPager.
默认情况下,池中会保留 5 ViewHolder
特定 viewType。如果你想改变那个计数,可以这样做:
recyclerView.getRecycledViewPool()
.setMaxRecycledViews(SOME_VIEW_TYPE, POOL_CAPACITY);
来自 this 博客 post:
So how do we choose the optimal size of the pool? It seems that the optimal strategy is to extend the pool right before you’ll need it to be big, and shrink it right afterwards. One dirty way to implement this is the following:
recyclerView.getRecycledViewPool().setMaxRecycledViews(0, 20);
adapter.notifyDataSetChanged();
new Handler().post(new Runnable() {
@Override
public void run() {
recyclerView.getRecycledViewPool()
.setMaxRecycledViews(0, 1);
}
});
当我们有一个嵌套的 RecyclerView 时,setRecycledViewPool(...)
会很有用。有关详细信息,请参阅 this 博客 post。此处添加相同 link 的简短描述。
考虑这样一种情况,其中嵌套的 RecyclerView
和内部的 RecycleView
共享相同的视图结构。 RecycledViewPool
提供了一种无缝 方式来在这些内部(嵌套)RecyclerView
之间共享视图。
下图中可以看到这种情况的一个例子:
如您所见,两个列表的视图类型相同。
当 RecyclerView 预计同时在屏幕上显示超过 5 个视图(当前默认为 5)时,建议将其放大到您认为的最大值。这将允许在滚动时进行实际的回收(RecyclerView 的目的...),以避免重新创建视图。
事实上,因为我实际上认为几乎不知道屏幕上会显示多少(不同的屏幕分辨率等...),所以我认为最好将其设置为最大值:
fun RecyclerView.setMaxViewPoolSize(maxViewTypeId: Int, maxPoolSize: Int) {
for (i in 0..maxViewTypeId)
recycledViewPool.setMaxRecycledViews(i, maxPoolSize)
}
用法:
recyclerView.setMaxViewPoolSize(MAX_TYPE_FOR_THE_ADAPTER_YOU_MADE, Int.MAX_VALUE)
我个人不明白为什么这不是默认行为。使用 RecyclerView 的全部意义在于回收视图。滚动时,默认情况下,它应该回收刚刚使用过的视图。
我试图了解 setRecycledViewPool 方法在以下代码行中与 RecyclerView 一起实际执行的操作,其中 mrecyclerView
是 RecyclerView 对象:
mrecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
我阅读了 Android 文档 link 但我仍然不清楚它的作用。有人可以向我解释一下它的用途以及何时使用它吗?
我自己没有使用过它,但从我阅读文档的理解来看,它是一种使用在一个 RecyclerView 和另一个 RecyclerView 中回收的视图的方法。
因此,如果您有一个 RecyclerView 和一堆装饰好的 CardView,并且您想为另一个 RecyclerView 回收这些相同的视图,您可以将共享的 RecycledViewPool 传递给它。现在两个 RecyclerView 都将从共享视图池中获取。
来自docs:
Recycled view pools allow multiple RecyclerViews to share a common pool of scrap views. This can be useful if you have multiple RecyclerViews with adapters that use the same view types, for example if you have several data sets with the same kinds of item views displayed by a ViewPager.
默认情况下,池中会保留 5 ViewHolder
特定 viewType。如果你想改变那个计数,可以这样做:
recyclerView.getRecycledViewPool()
.setMaxRecycledViews(SOME_VIEW_TYPE, POOL_CAPACITY);
来自 this 博客 post:
So how do we choose the optimal size of the pool? It seems that the optimal strategy is to extend the pool right before you’ll need it to be big, and shrink it right afterwards. One dirty way to implement this is the following:
recyclerView.getRecycledViewPool().setMaxRecycledViews(0, 20);
adapter.notifyDataSetChanged();
new Handler().post(new Runnable() {
@Override
public void run() {
recyclerView.getRecycledViewPool()
.setMaxRecycledViews(0, 1);
}
});
setRecycledViewPool(...)
会很有用。有关详细信息,请参阅 this 博客 post。此处添加相同 link 的简短描述。
考虑这样一种情况,其中嵌套的 RecyclerView
和内部的 RecycleView
共享相同的视图结构。 RecycledViewPool
提供了一种无缝 方式来在这些内部(嵌套)RecyclerView
之间共享视图。
下图中可以看到这种情况的一个例子:
如您所见,两个列表的视图类型相同。
当 RecyclerView 预计同时在屏幕上显示超过 5 个视图(当前默认为 5)时,建议将其放大到您认为的最大值。这将允许在滚动时进行实际的回收(RecyclerView 的目的...),以避免重新创建视图。
事实上,因为我实际上认为几乎不知道屏幕上会显示多少(不同的屏幕分辨率等...),所以我认为最好将其设置为最大值:
fun RecyclerView.setMaxViewPoolSize(maxViewTypeId: Int, maxPoolSize: Int) {
for (i in 0..maxViewTypeId)
recycledViewPool.setMaxRecycledViews(i, maxPoolSize)
}
用法:
recyclerView.setMaxViewPoolSize(MAX_TYPE_FOR_THE_ADAPTER_YOU_MADE, Int.MAX_VALUE)
我个人不明白为什么这不是默认行为。使用 RecyclerView 的全部意义在于回收视图。滚动时,默认情况下,它应该回收刚刚使用过的视图。