RecyclerViewPager使用Volley的NetworkImageView翻页时图片有时不显示

如标题描述,经过网上搜索和查看NetworkImageView源码,发现问题出在onDetachedFromWindow()中,具体原因如下

@Override
    protected void onDetachedFromWindow() {
        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();
    }

解决方法,重写onDetachedFromWindow()方法即可

package com.aoeai.tuzhen.view;

import android.content.Context;
import android.util.AttributeSet;

import com.android.volley.toolbox.NetworkImageView;

/** * Created by aoe on 2015/10/12. */
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() {
        // 防止在不显示的时候,取消图片加载请求
    }
}

你可能感兴趣的:(图片)