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

查看:271
本文介绍了如何使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结合使用.我想要的只是网格布局可以平滑滚动(就像默认的gallery app),没什么花哨的,但是网格布局管理器的默认实现以生涩"的方式滚动视图.我尝试从上面的链接实现该方法,但未成功.

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的文档 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.

我发现了这三部分教程,但内容很少帮助.所以,我想问的是:是否可以在LinearSmoothScroller中实现某种模板代码,或者通过扩展RecyclerView.SmoothScroller并实现平滑滚动来实现?即使代码取决于网格布局中的项目数和每行的项目数,也必须有某种方法可以轻松实现.我在这里想念什么吗?

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

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.

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

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