且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使RecyclerView平滑滚动?

更新时间:2021-12-28 07:11:36

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和图像解码.这样的速度将足够慢,以至于在用户界面中产生混乱(即生涩"的滚动).

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(带有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.