载入失败滑翔
On Loading Failed Glide
我在我的应用程序中使用滑行来加载图像。它在我的情况下工作正常
if(image_enabled==1){
Glide.with(getContext()).load(constant.SERVER_URL+"images/"+quoteData.get(KEY_PICTURE).apply(myOptions).into(mImageView);
}
else if(image_enabled==0){
Glide.with(getContext()).load(constant.SERVER_URL+"images/"+quoteData.get(KEY_PICTURE)).apply(myOptions).into(mImageView);
}
但如果上述任何条件未能加载图像,我想再加载一个 url。我不知道滑行中轨道加载失败的方法是什么。让我知道是否有人可以帮助我得到它。
谢谢
我想这会对你有所帮助。只需在 .error() 中设置您的 url 它将在失败时加载。
Glide.with(getContext())
.load("your url")
.error("your default drawable")
.into(mImgProfile);
否则你也可以使用下面的
Glide.with(mActivity)
.load("your url")
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
.into(mImgProfile);
如果当前加载失败,这里是加载另一个 url 的完全可行的代码:
代码重用的小扩展:
fun RequestBuilder<Drawable>.setListener(
onReady: () -> Boolean = { false },
onFailed: () -> Boolean,
): RequestBuilder<Drawable> {
return listener(object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>?,
isFirstResource: Boolean,
): Boolean = onFailed()
override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: Target<Drawable>?,
dataSource: DataSource?,
isFirstResource: Boolean,
): Boolean = onReady()
})
}
来自 ViewHolder 的代码示例:
private val avatarImage = view.findViewById<ImageView>(R.id.avatar_image)
private val glideHandler = Handler(Looper.getMainLooper())
fun bind(urlList: List<String>) {
if (urlList.isNotEmpty()) {
loadImage(urlList)
}
}
fun unbind() {
glideHandler.removeCallbacksAndMessages(null)
}
private fun loadImage(urlList: List<String>, index: Int = urlList.indices.first) {
val url = urlList.getOrNull(index) ?: return
Glide.with(avatarImage)
.load(url)
.placeholder(R.drawable.img_holder_load)
.error(R.drawable.img_holder_error)
.setListener {
val newIndex = index + 1
if (newIndex in urlList.indices) {
glideHandler.post { loadImage(urlList, newIndex) }
return@setListener true
}
return@setListener false
}
.into(avatarImage)
}
我在我的应用程序中使用滑行来加载图像。它在我的情况下工作正常
if(image_enabled==1){
Glide.with(getContext()).load(constant.SERVER_URL+"images/"+quoteData.get(KEY_PICTURE).apply(myOptions).into(mImageView);
}
else if(image_enabled==0){
Glide.with(getContext()).load(constant.SERVER_URL+"images/"+quoteData.get(KEY_PICTURE)).apply(myOptions).into(mImageView);
}
但如果上述任何条件未能加载图像,我想再加载一个 url。我不知道滑行中轨道加载失败的方法是什么。让我知道是否有人可以帮助我得到它。 谢谢
我想这会对你有所帮助。只需在 .error() 中设置您的 url 它将在失败时加载。
Glide.with(getContext())
.load("your url")
.error("your default drawable")
.into(mImgProfile);
否则你也可以使用下面的
Glide.with(mActivity)
.load("your url")
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
.into(mImgProfile);
如果当前加载失败,这里是加载另一个 url 的完全可行的代码:
代码重用的小扩展:
fun RequestBuilder<Drawable>.setListener(
onReady: () -> Boolean = { false },
onFailed: () -> Boolean,
): RequestBuilder<Drawable> {
return listener(object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>?,
isFirstResource: Boolean,
): Boolean = onFailed()
override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: Target<Drawable>?,
dataSource: DataSource?,
isFirstResource: Boolean,
): Boolean = onReady()
})
}
来自 ViewHolder 的代码示例:
private val avatarImage = view.findViewById<ImageView>(R.id.avatar_image)
private val glideHandler = Handler(Looper.getMainLooper())
fun bind(urlList: List<String>) {
if (urlList.isNotEmpty()) {
loadImage(urlList)
}
}
fun unbind() {
glideHandler.removeCallbacksAndMessages(null)
}
private fun loadImage(urlList: List<String>, index: Int = urlList.indices.first) {
val url = urlList.getOrNull(index) ?: return
Glide.with(avatarImage)
.load(url)
.placeholder(R.drawable.img_holder_load)
.error(R.drawable.img_holder_error)
.setListener {
val newIndex = index + 1
if (newIndex in urlList.indices) {
glideHandler.post { loadImage(urlList, newIndex) }
return@setListener true
}
return@setListener false
}
.into(avatarImage)
}