Reputation: 6825
Recently I have this to load an image in my View
Glide.with(getApplicationContext()).load(url).into(imageView);
Now, I would love to implement a callback with an interface to know when the image has been fetched from the url, I need this callback in order to show a custom progress bar that I have made.
Searching I have found this but is no quite what I want
.listener(new RequestListener<Uri, GlideDrawable>() {
@Override public boolean onException(Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override public boolean onResourceReady(GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
.into(imageView)
;
because it does not define a custom callback interface for the resource, is there any way to attach a callback to glide so I know when the downlad of the image finishes?
Upvotes: 0
Views: 249
Reputation: 68
You can use the above approach with .listener(), but pass it an object of a new class type which you define. That new class implements RequestListener and take as argument in the constructor any custom callback object you want. The download is ready in onResourceReady(). Assuming you have the custom callback as an object:
CustomCallback CustomCallback;
and start your progress before loading the image with glide:
customCallback.startProgressBar();
Glide.with(this)
.listener(new CustomRequestListener(customCallback))
.into(imageView);
were the custom class is:
private static class CustomRequestListener implements RequestListener<Uri, GlideDrawable> {
private CustomCallback cc;
public CustomRequestListener(CustomCallback cc) {
this.cc = cc;
}
@Override public boolean onException(Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) {
cc.stopProgressBar();
return false;
}
@Override public boolean onResourceReady(GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
cc.stopProgressBar();
return false;
}
}
Upvotes: 1