与凌空图像请求循环器视图(取消请求) [英] Recycler view with volley image request (cancel request)

查看:223
本文介绍了与凌空图像请求循环器视图(取消请求)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用回收视图显示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);
}

问题是,当我滚动并跳过一个或多个视图下载图像之前和看法回收图像donwnload reqest不会取消对造成一个闪光的中间视/前的那些图像(S)实际的图像被载入这一观点。

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!

此外,当我使用凌空NetworkImageView(即HANDELS这样的图像本身取消)给出了完美的效果。但我需要得到每个图像的位图来挑选颜色,所以我不能使用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)我如何取消所有未决的大力抽射图像requsts(除了一个应该加载和没有在一个特定的ImageView影响其他平行视图)使用充气recyclerview?

推荐答案

您应该使用 ImageLoader的类,而不是直接添加到一个请求请求队列。这样一来,的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 的code。它的工作原理以类似的方式

Take a look at the NetworkImageView's code. It works in a similar way.

这篇关于与凌空图像请求循环器视图(取消请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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