如何让 RecyclerView 平滑滚动? [英] How to make RecyclerView scroll smoothly?

查看:36
本文介绍了如何让 RecyclerView 平滑滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更像是一个通用问题,但经过大量搜索和尝试后,我无法理解为什么这如此难以实现.这是我能找到的最接近的答案,但仍然无法实施.

This is more like a generic question, but after lot of search and try I am not able to understand why this is so difficult to achieve. This is the closest answer I can find but still unable to implement.

具体来说,我将 RecyclerView 与 GridLayoutManager 一起使用.我想要的是平滑滚动的网格布局(就像默认的画廊应用程序),没什么特别的,但是网格布局管理器的默认实现以生涩"的方式滚动视图.我试图实现上面链接中的方法,但没有成功.

To be specific I am using RecyclerView with GridLayoutManager. All I want is the grid layout to scroll smoothly (like default gallary app) ,nothing fancy, but the default implementation of grid layout manager scrolls the view in a 'jerky' manner. I tried to implement the method from above link but unsuccessfully.

我也尝试过实现 LinearSmoothScroller 但我不确定如何实现 computeScrollVectorForPosition 方法.Google 的文档关于 computeScrollVectorForPosition 字面上有0 字.

I also tried to implement LinearSmoothScroller but I am not sure how to implement computeScrollVectorForPosition method. Google's documentation on computeScrollVectorForPosition literally has 0 words.

我找到了 这个 3 部分教程,但内容很少帮助.所以,我想问的是:是否有某种模板代码可以在 LinearSmoothScroller 中实现,或者通过扩展 RecyclerView.SmoothScroller 来实现平滑滚动?即使代码取决于 gridlayout 中每行的项目数和项目数,也必须有一些方法可以轻松完成.我在这里遗漏了什么吗?

I found this 3 part tutorial, but it was of very little help. So, all I want to ask is: can there be some kind of template code which we can implement in LinearSmoothScroller or by extending RecyclerView.SmoothScroller and achieve smooth scrolling ? Even if the code depends on number of items and items per row in gridlayout, there has to be some method to do it easily. Am I missing something here ?

推荐答案

Android 中生涩"滚动的典型原因是应用在主应用线程更新 UI 上花费了太多时间.对于 RecyclerView,这意味着在 onBindViewHolder()onCreateViewHolder() 中花费太多时间.每个都需要在亚毫秒内返回,这意味着您不能在其中执行磁盘 I/O 或网络 I/O.

The typical source of "jerky" scrolling in Android is the app taking too much time on the main application thread updating the UI. In the case of RecyclerView, this would mean taking too much time in onBindViewHolder() or possibly in onCreateViewHolder(). Those each need to return in sub-millisecond times, meaning you cannot do disk I/O or network I/O in them.

我想我发现了问题.我正在使用 holder.photo.setImageBitmap(BitmapFactory.decodeFile(itemList.get(position).get‌ AbsolutePath()));onBindViewHolder() 和图像文件每个大约 100kb,列表中大约有十几个图像

think I found the problem. I am using holder.photo.setImageBitmap(BitmapFactory.decodeFile(itemList.get(position).get‌​AbsolutePath())); on onBindViewHolder() and the image file is around 100kb each with about a dozen images in the list

是的,这将在主应用程序线程上进行磁盘 I/O 和图像解码.这将慢到足以导致 UI 卡顿(即生涩"滚动).

Yes, that will be doing disk I/O and image decoding on the main application thread. That will be slow enough to cause jank in the UI (i.e., "jerky" scrolling).

考虑使用图像加载库,例如 Picasso 或 Universal Image Loader,因为它们可以从位图异步填充您的 ImageView.

Consider using an image loading library, like Picasso or Universal Image Loader, as they can populate your ImageView from the bitmap asynchronously.

这个示例应用使用通用图像加载器来帮助填充RecyclerView (with GridLayoutManager),数据源是设备上的可用视频.

This sample app uses Universal Image Loader to help populate the contents of a RecyclerView (with GridLayoutManager), with the data source being the available videos on the device.

这篇关于如何让 RecyclerView 平滑滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆