问题描述
所以我使用回收器视图在网格中显示图像,并使用 volley 库从 url 下载图像作为位图.
So i am using recycler view to show images in a grid and downloading images from url as bitmaps using volley library.
public void onBindViewHolder(final TrendingAdapter.ViewHolder viewHolder, int i) {
ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
if (bitmap != null) {
viewHolder.getmImageView().setImageBitmap(bitmap);
}
}
}, 0, 0, null,
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
AppController.getInstance().addToRequestQueue(request);
}
问题是当我在下载图像之前滚动并跳过一个或多个视图并且该视图被回收时,图像下载请求不会在那些中间视图上取消,导致该/那些图像在实际图像加载在该视图中.
The problem is when i scroll and skip one or more views before the image is downloaded and that view is recycled the image donwnload reqest is not canceled on those intermediate view resulting in a flash of that/those image(s) before the actual image is loaded in that view.
所以我想使用标签取消那些中间图像请求,但无法弄清楚它是如何导致在其他并行视图中取消请求的!
So i thought of canceling those intermediate image requests using tags but cannot figure out how as it results in canceling of request in other parallel views as well!
此外,当我使用 volley NetworkImageView(自行处理此类图像取消)时,也能提供完美的结果.但是我需要获取每个图像的位图以从中选择颜色,因此我无法使用 NetworkImageView.
Also when i use volley NetworkImageView (that handels such image canceling by itself) gives perfect results. But i need to get bitmap of every image to pick colors from it so i cannot use NetworkImageView.
Q) 我如何在使用 recyclerview 膨胀的特定图像视图上取消所有待处理的齐射图像请求(除了它应该加载的并且不影响其他并行视图的请求)?
推荐答案
您应该使用 ImageLoader
类,而不是直接向 RequestQueue
添加请求.这样,用于获取图像的 get()
方法将返回一个 ImageContainer
类型的对象.
You should use the ImageLoader
class instead of directly adding a request to the RequestQueue
. That way, the get()
method, which is used to get images, will return an object of type ImageContainer
.
将此ImageContainer
保存在ViewHolder
中,当一个视图被回收时,只需调用回收图像上的cancelRequest()
方法即可如果尚未执行,则取消请求.
Save this ImageContainer
in the ViewHolder
, and when a view gets recycled, simply call the cancelRequest()
method on the recycled image to cancel the request if it hasn't been performed yet.
查看NetworkImageView
的代码.它以类似的方式工作.
Take a look at the NetworkImageView
's code. It works in a similar way.
这篇关于带齐射图像请求的回收器视图(取消请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!