android - RecycleView 的 setInitialPrefetchItemCount 不起作用

标签 android android-recyclerview gridlayoutmanager

我试图通过使用支持库 25.1 中引入的新 API 提前预取 View 来改善用户体验,同时滚动图像网格来控制 RecycleView 中预取项目的数量。

GridLayoutManager layoutManager = new GridLayoutManager(this.getContext(), PhotoGridViewHolder.SPAN_SIZE);
layoutManager.setItemPrefetchEnabled(true);
layoutManager.setInitialPrefetchItemCount(30); 
photosRecycleView.setLayoutManager(layoutManager);
... add 100 photos to the adapter ...

我还为我的 ViewHolder 添加了日志记录,这样我就可以查看绑定(bind)是否真的发生了。

void bind(final Photo photo, int position) {
    // I expcet to see 30 logs without event scrolling as the intial set to 30.
    log.d("binding photo: " + photo.getId());
    loadPhoto(photo);
}

但是,它似乎不起作用,因为我加载了一个包含 100 张图像的列表,但我只看到了几个日志条目(仅获取可见项目),但我希望在 30 处看到,因为我将其设置为像这样。

最佳答案

正如@FlanschiFox 和他的回答下方的评论所说,这仅适用于内部 RecyclerView。另外,我写了一个 PreloadLinearLayoutManager here在列表滚动到末尾之前预加载数据 5 项。它可能会对您有所帮助。

关于android - RecycleView 的 setInitialPrefetchItemCount 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46958484/

相关文章:

android - Sherlock 操作栏的菜单项背景

java - 不能在 RecyclerView 中使用 getLayoutPosition() 或 getAdapterPosition()

android - lateinit 属性绑定(bind)尚未初始化

android - 手机旋转时,RecyclerView 不会从 2 列变为 3 列

android - 填充操作栏 "up"指示器图像

android - 在 Whatsapp 等通知上显示表情符号

Android - GridLayoutManager - 垂直加载列表中的项目?

android - 具有水平 GridLayoutManager 的 RecyclerView 将其高度调整到最大行

android - 更新有序的 BroadcastReceiver android 之间的 Intent extras

android - 在包含字符串名称和字符串子文本的 RecyclerView 中搜索字符串名称时出现问题