繁体   English   中英

Android Recyclerview预加载视图

[英]Android Recyclerview preload views

我想知道当用户滚动得更快时,如何避免recyclerview中的视图出现白色的“闪烁”。
当然,可以通过在可见屏幕之外预加载更多视图来避免闪烁

尽管这必须是很常见的任务,但我仍然找不到任何方法可以完成?

我从博客尝试了以下代码:

public class PreCachingLayoutManager extends LinearLayoutManager {
    private static final int DEFAULT_EXTRA_LAYOUT_SPACE = 600;
    private int extraLayoutSpace = -1;
    private Context context;

    public PreCachingLayoutManager(Context context) {
        super(context);
        this.context = context;
    }

    public PreCachingLayoutManager(Context context, int extraLayoutSpace) {
        super(context);
        this.context = context;
        this.extraLayoutSpace = extraLayoutSpace;
    }

    public PreCachingLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
        this.context = context;
    }

    public void setExtraLayoutSpace(int extraLayoutSpace) {
        this.extraLayoutSpace = extraLayoutSpace;
    }

    @Override
    protected int getExtraLayoutSpace(RecyclerView.State state) {
        if (extraLayoutSpace > 0) {
            return extraLayoutSpace;
        }
        return DEFAULT_EXTRA_LAYOUT_SPACE;
    }
}

然后,我通过使用setLayoutManager()将LayoutManager分配给构造函数中的自定义Recyclerview。
这是“自定义”,但我只想在构造函数中设置LayoutManager,这就是为什么我重写了RecyclerView的原因
不幸的是,这没有任何作用

RecyclerView已经是一个非常高效的API,通常如果丢帧,您可以优化项目布局以减轻重量,并确保在回收视图时释放资源。 确实不需要预加载任何内容。

这是一篇博客文章,其中提供了一些有关如何执行这些操作的想法https://blog.workable.com/recyclerview-achieved-60-fps-workables-android-app-tips/

他们在这篇文章中没有提到它,但是如果您的商品有图片,请确保这些图片不要太大。

要点是要尽可能快地绘制视图,因为在滚动时会不断重绘所有内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM