滚动时加载Android Glide旧图像

时间:2016-03-14 14:11:37

标签: android image gridview android-glide

我正在使用Glide,我在Gridview中下载并显示所有图像。但是在下载期间我在gridview中滚动时遇到问题。滑动加载以前的图像并用最新的图像替换它们但看起来像是滞后。

如果未加载图像,如何加载任何内容?我和毕加索有同样的问题并修复它我已经设置了这段代码imageView.setImageDrawable(null);但是使用Glide这不起作用。

适配器getView

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View view;
    final ImageView imageView;
    final TextView legend;

    // Soft display
    if(convertView == null){
        view = mLayoutInflater.inflate(R.layout.thumbnail_gallery, parent, false);
    }else{
        view = convertView;
    }

    // Get layout item (Image and Legend)
    imageView = (ImageView) view.findViewById(R.id.iv_thumbnail);
    legend = (TextView) view.findViewById(R.id.text_thumb);

    // Do not display old images
    imageView.setImageDrawable(null);

    // Get the file path
    final File file = new File(mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES), mPInfoList.get(position).getFilename());
    mToSave = !file.exists();

    // Get the download path image
    String pic = Globals.SERVER_NAME+Globals
                        .ACCOUNT_SERVER_PATH+mPInfoList
                        .get(position).getFolderPath()+"/"+
                        VgzTools.addSuffix(mPInfoList.get(position).getFilename(), "-thumb");

    // Check file exist
    if(!file.exists()){

        // Create a Simple Target for Glide
        SimpleTarget<Bitmap> simpleTarget = new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(final Bitmap resource, GlideAnimation glideAnimation) {
                // Thread to save the image
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            // Image saving
                            FileOutputStream fileOutput = new FileOutputStream(file);
                            resource.compress(Bitmap.CompressFormat.JPEG, 100, fileOutput);
                            fileOutput.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

                imageView.setImageBitmap(resource);
                // Set the legend
                if (mPInfoList.get(position).getFilename() != null) {
                    legend.setText(mPInfoList.get(position).getLegend());
                }
            }
        };

        // File doesn't exist Save image
        Glide.with(mContext)
                .load(pic)
                .asBitmap()
                .into(simpleTarget);

        mToSave = true;
    }else{
        // File exist then display it
        Glide.with(mContext)
                .load(file)
                .into(imageView);
        mToSave = false;
    }

    return view;
}

1 个答案:

答案 0 :(得分:-1)

如果您没有开始加载到imageView,请在其上调用Glide#clear()。你的最后一个if / else语句应如下所示:

if (!file.exists()) {
    Glide.clear(imageView);
    // New thread etc.
} else {
    // File exist then display it
    Glide.with(mContext)
            .load(file)
            .into(imageView);
    mToSave = false;
}