java RecyclerView 中的 setRecycledViewPool 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43332873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:20:54  来源:igfitidea点击:

setRecycledViewPool method in RecyclerView

javaandroidandroid-layoutandroid-recyclerviewandroid-view

提问by Abhilash Reddy

I am trying to understand what the setRecycledViewPool method actually does along with the RecyclerView in the following line of code where mrecyclerViewis a RecyclerView object:

我试图了解 setRecycledViewPool 方法与以下代码行中的 RecyclerView 一起实际做什么,其中mrecyclerView是 RecyclerView 对象:

mrecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());

I read the Android documentation linkand I still don't understand what it does clearly. Can someone explain to me its use and when to use it?

我阅读了 Android 文档链接,但我仍然不明白它的作用。有人可以向我解释它的用途以及何时使用它吗?

回答by azizbekian

From 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.

回收视图池允许多个RecyclerView共享一个公共的废料视图池。如果您有多个RecyclerView的适配器使用相同的视图类型,这会很有用,例如,如果您有多个数据集,其中ViewPager显示的项目视图类型相同。

By default, 5 ViewHolders are retained in the pool for a particular viewType. If you want to change that count, it may be done this way:

默认情况下,ViewHolder池中为特定viewType保留5秒。如果您想更改该计数,可以通过以下方式完成:

recyclerView.getRecycledViewPool()
            .setMaxRecycledViews(SOME_VIEW_TYPE, POOL_CAPACITY);

From thisblog 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);
    }
});

回答by Alex Kisel

setRecycledViewPool(...)can be useful when we have a nested RecyclerView. See thisblog post for details. A short description of the same link is added here.

setRecycledViewPool(...)当我们有一个嵌套的 RecyclerView 时会很有用。有关详细信息,请参阅博客文章。此处添加了同一链接的简短说明。

Consider a case where you have a nested RecyclerViews and inner RecycleViews share the same view structure. RecycledViewPoolprovides a seemlessway to share views between these inner (nested) RecyclerViews.

考虑一个嵌套RecyclerViews 和内部RecycleViews 共享相同视图结构的情况。RecycledViewPool提供了一种在这些内部(嵌套)之间共享视图的无缝方式RecyclerView

An example of such case could be seen in the following image:

在下图中可以看到这种情况的一个例子:

enter image description here

在此处输入图片说明

As you can see the types of views for both lists are same.

如您所见,两个列表的视图类型相同。

回答by android developer

When the RecyclerView is expected to show more than 5 views on the screen at the same time (5 is the current default), it's recommended to enlarge it to what you think will be the max. This will allow to do an actual recycling (the purpose of RecyclerView...) when scrolling, to avoid re-creation of Views.

当 RecyclerView 预计在屏幕上同时显示 5 个以上的视图时(5 是当前默认值),建议将其放大到您认为的最大值。这将允许在滚动时进行实际回收(RecyclerView...的目的),以避免重新创建视图。

In fact, because I actually think that it's almost never known how many will be be shown on the screen (different screens resolutions, etc...) , I think it's best to just set it to be the max :

事实上,因为我实际上认为几乎永远不知道屏幕上会显示多少(不同的屏幕分辨率等...),我认为最好将其设置为 max :

fun RecyclerView.setMaxViewPoolSize(maxViewTypeId: Int, maxPoolSize: Int) {
    for (i in 0..maxViewTypeId)
        recycledViewPool.setMaxRecycledViews(i, maxPoolSize)
}

Usage:

用法:

    recyclerView.setMaxViewPoolSize(MAX_TYPE_FOR_THE_ADAPTER_YOU_MADE, Int.MAX_VALUE)

I personally don't understand why it's not the default behavior. The whole point of using a RecyclerView is to recycle the Views. When scrolling, it should, by default, recycle views that were just used.

我个人不明白为什么这不是默认行为。使用 RecyclerView 的全部意义在于回收视图。滚动时,默认情况下,它应该回收刚刚使用的视图。

回答by DejanRistic

I haven't used it myself, but from what I can understand reading the docs its a way to use views you recycle in one RecyclerView with another.

我自己没有使用过它,但是根据我阅读文档的理解,这是一种使用您在一个 RecyclerView 中与另一个 RecyclerView 回收的视图的方法。

So if you have a RecyclerView with a bunch of decked out CardViews, and you would like to recycle those same views for another RecyclerView you could pass it a shared RecycledViewPool. Now both RecyclerView will take from the shared view pool.

因此,如果您有一个带有一堆装饰的 CardView 的 RecyclerView,并且您想为另一个 RecyclerView 回收这些相同的视图,您可以将它传递给一个共享的 RecycledViewPool。现在两个 RecyclerView 都将从共享视图池中获取。