带有凌空图像请求的循环器视图(取消请求)

时间:2014-10-27 15:45:53

标签: android bitmap android-volley android-recyclerview android-viewholder

所以我使用Recycler视图在网格中显示图像,并使用volley库将图像从url下载为位图。

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在这些中间视图上没有被取消,导致那些/那些图像之前闪现在该视图中加载实际图像。

所以我想用标签取消那些中间图像请求,但是无法弄清楚如何在其他并行视图中取消请求!

此外,当我使用volley NetworkImageView(单独取消此类图像取消)时,可以获得完美的效果。但我需要获取每个图像的位图来从中选择颜色,所以我不能使用NetworkImageView。

问:如何在使用recyclerview充气的特定图像视图中取消所有待处理的截击图像请求(除了应加载的图像而不影响其他平行视图)

4 个答案:

答案 0 :(得分:8)

您应该使用ImageLoader类,而不是直接向RequestQueue添加请求。这样,用于获取图像的get()方法将返回ImageContainer类型的对象。

将此ImageContainer保存在ViewHolder中,当视图获得回收后,只需调用已回收图片上的cancelRequest()方法,即可取消请求表演了。

查看NetworkImageView代码。它以类似的方式工作。

答案 1 :(得分:1)

我解决了这个问题! yaaeeye:D @Itai的回答指出了我正确的方向。我仔细查看了NetworkImageView代码并根据我的需要对其进行了修改。所以我做了一个CustumNetworkImageView

public class CustomNetworkImageView extends ImageView {

    // Added code block start

    public interface ResponseObserver {
        public void onError();

        public void onSuccess();
    }

    private ResponseObserver mResponseObserver;

    public void setmResponseObserver(ResponseObserver observer) {
        mResponseObserver = observer;
    }

    private Bitmap bmp = null;

    // Added code block end

    private String mUrl;

    private int mDefaultImageId;

    private int mErrorImageId;

    private ImageLoader mImageLoader;

    public CustomNetworkImageView(Context context) {
        this(context, null);
    }

    public CustomNetworkImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomNetworkImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setImageUrl(String url, ImageLoader imageLoader) {
        mUrl = url;
        mImageLoader = imageLoader;
        loadImageIfNecessary();
    }

    public void setDefaultImageResId(int defaultImage) {
        mDefaultImageId = defaultImage;
    }

    public void setErrorImageResId(int errorImage) {
        mErrorImageId = errorImage;

        // Added code block start

        if (mResponseObserver != null) {
            mResponseObserver.onError();
        }

        // Added code block end

    }

    // Added code block start

    public Bitmap getBitmap() {
        return bmp;
    }

    // Added code block end

    private void loadImageIfNecessary() {
        int width = getWidth();
        int height = getHeight();

        if (width == 0 && height == 0) {
            return;
        }

        if (TextUtils.isEmpty(mUrl)) {
            ImageContainer oldContainer = (ImageContainer) getTag();
            if (oldContainer != null) {
                oldContainer.cancelRequest();
                setImageBitmap(null);
            }
            return;
        }

        ImageContainer oldContainer = (ImageContainer) getTag();

        if (oldContainer != null && oldContainer.getRequestUrl() != null) {
            if (oldContainer.getRequestUrl().equals(mUrl)) {

                return;
            } else {

                oldContainer.cancelRequest();
                setImageBitmap(null);
            }
        }

        ImageContainer newContainer = mImageLoader.get(mUrl,
                ImageLoader.getImageListener(this, mDefaultImageId, mErrorImageId));

        setTag(newContainer);

        final Bitmap bitmap = newContainer.getBitmap();
        if (bitmap != null) {

            setImageBitmap(bitmap);

           // Added code block start

            bmp = bitmap;
            if (mResponseObserver != null) {
                mResponseObserver.onSuccess();
            }

           // Added code block end
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        loadImageIfNecessary();
    }

    @Override
    protected void onDetachedFromWindow() {
        ImageContainer oldContainer = (ImageContainer) getTag();
        if (oldContainer != null) {

            oldContainer.cancelRequest();
            setImageBitmap(null);

            setTag(null);
        }
        super.onDetachedFromWindow();
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        invalidate();
    }
}

使用CustomNetworkImageView:

    viewHolder.getmImageView().setImageUrl(wallThumb.get(i), AppController.getInstance().getImageLoader());

    viewHolder.getmImageView().setmResponseObserver(new CustomNetworkImageView.ResponseObserver() {

            @Override
            public void onError() {
            }

            @Override
            public void onSuccess() {
                // Image is loaded in ImageView.. Do Something..
                Bitmap bitmap = viewHolder.getmImageView().getBitmap();
            }
        });

SetmResponseObserver侦听是否在ImageView中加载了位图,如果任务完成则调用onSuccess ..我还创建了一个函数,它返回图像视图上的位图,可以用作

Bitmap bmp = viewHolde.getmImageView.getBitmap();

我是Android的新手,所以如果我做错了什么,请随时发表评论并纠正我。 Yaaaeeyeee快乐的dayyy

答案 2 :(得分:0)

public class RecyclerViewPagerNetworkImageView extends NetworkImageView {
public RecyclerViewPagerNetworkImageView(Context context) {
    super(context);
}

public RecyclerViewPagerNetworkImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public RecyclerViewPagerNetworkImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onDetachedFromWindow() {
    // delete
    /*if (mImageContainer != null) {
        // If the view was bound to an image request, cancel it and clear
        // out the image from the view.
        mImageContainer.cancelRequest();
        setImageBitmap(null);
        // also clear out the container so we can reload the image if necessary.
        mImageContainer = null;
    }
    super.onDetachedFromWindow();*/
}

}

答案 3 :(得分:0)

我遇到了同样的问题。我无法使用NetworkImageView和图片加载器,因为我想要一个自定义图像请求。

所以我找到了另一种取消航班请求的方法。我只是通过ViewHolder回调在onBindViewHolder对象中传递了图像请求。在ViewHolder中创建了一个方法setRequest(Request request)

在此方法中检查请求是否为空,然后先调用request.cancel(),否则只需将请求设置为持有者。

public class ViewHolder extends RecyclerView.ViewHolder {

    public Request request;

    public ViewHolder(View view) {
        super(view);
    }

    public void setRequest(Request request) {
       if (this.request != null) {
            this.request.cancel();
       }
       this.request = request;
    }
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

        ImageRequest imageRequest = new ImageRequest(.......);
        holder.setRequest(request);

        mRequestQueue.add(request);
}