Glide - 尝试加载照片时回退或错误后调用方法 [英] Glide - call method after fallback or error when trying load photo

查看:36
本文介绍了Glide - 尝试加载照片时回退或错误后调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Glide - 尝试加载照片时回退或错误后调用方法.

Glide - call method after fallback or error when trying load photo.

嗨!

有没有办法检查 Glide 是否从链接加载照片或在链接无效或照片时使用 fallback/error不可用?

Is there any a way to check if Glide load photo from link or use fallback/error when link isn't valid or photo isn't available?

我的意思是,当 Glide 不加载照片时,我想调用一个方法(加载其他照片).

I mean, I want to call a method (load other photo) when Glide doesn't load photo.

这是我的 Glide 例如:

This is my Glide e.g.:

Glide
        .with(mActivity)
        .load(news.getPagemap().getCseThumbnail().get(0).getSrc())
        .fallback(R.drawable.bg_gradient)
        .error(R.drawable.bg_gradient)
        .centerCrop()
        .crossFade()
        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
        .into(holder.photo);

我尝试比较 ConstantValues - holder.photo.getDrawable().getConstantState().equals(mActivity.getResources().getDrawable(R.drawable.bg_gradient).getConstantState()) 但得到 NullPointerException.

I tried to compare ConstantValues - holder.photo.getDrawable().getConstantState().equals(mActivity.getResources().getDrawable(R.drawable.bg_gradient).getConstantState()) but got NullPointerException.

推荐答案

这对我有帮助:

private void loadPicture(final ViewHolder holder, String photoUrl, final Boolean shouldLoadAgain) {
    holder.progressBar.setVisibility(View.VISIBLE);

    Glide
        .with(mActivity)
        .load(photoUrl)
        .fallback(R.drawable.bg_gradient)
        .error(R.drawable.bg_gradient)
        .centerCrop()
        .crossFade()
        .listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                holder.progressBar.setVisibility(View.GONE);
                if (shouldLoadAgain)
                    loadPicture(holder, mPhotoUrl, false);
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                holder.progressBar.setVisibility(View.GONE);
                return false;
            }
        })
        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
        .into(holder.photo);
}

这篇关于Glide - 尝试加载照片时回退或错误后调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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