I am using Android Image Slider library for showing images on a slider. However, some images are not loading because backend requires authentication. So I need a listener for not loading images.
This is library: inside abstract BaseSliderView class, there is ImageLoadListener interface. I am setting listener using setOnImageLoadListener method.
public abstract class BaseSliderView {
.....
private ImageLoadListener mLoadListener;
.....
protected void bindEventAndShow(final View v, ImageView targetImageView){
....
rq.into(targetImageView,new Callback() {
@Override
public void onSuccess() {
if(v.findViewById(R.id.loading_bar) != null){
v.findViewById(R.id.loading_bar).setVisibility(View.INVISIBLE);
}
}
@Override
public void onError() {
if(mLoadListener != null){
mLoadListener.onEnd(false,me);
}
if(v.findViewById(R.id.loading_bar) != null){
v.findViewById(R.id.loading_bar).setVisibility(View.INVISIBLE);
}
}
});
}
/**
* set a listener to get a message , if load error.
* @param l
*/
public void setOnImageLoadListener(ImageLoadListener l){
mLoadListener = l;
}
.....
public interface ImageLoadListener{
void onStart(BaseSliderView target);
void onEnd(boolean result,BaseSliderView target);
}
.....
}
I checked, when image is not loaded, interface onEnd method is called in library module.
But on app module, onEnd method is not called even in library module it is called.
Why is this happening? Should not onEnd method be called in app module? How to solve this problem?
I could solve this problem using greenrobot's EventBus library. First of all, I have added library dependency to library build.gradle file:
compile 'org.greenrobot:eventbus:3.0.0'
Created class for event:
public class ImageLoadErrorEvent {
String url;
ImageView imageView;
public ImageLoadErrorEvent(String url, ImageView imageView) {
this.url = url;
this.imageView = imageView;
}
public String getUrl() {
return url;
}
public ImageView getImageView() {
return imageView;
}
}
Posted on BaseSliderView class:
@Override
public void onError() {
if(mLoadListener != null){
mLoadListener.onEnd(false,me);
EventBus.getDefault().post(new ImageLoadErrorEvent(mUrl, targetImageView));
}
if(v.findViewById(R.id.loading_bar) != null){
v.findViewById(R.id.loading_bar).setVisibility(View.INVISIBLE);
}
}
In Activity, inside onCreate method, registered EventBus:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
EventBus.getDefault().register(this);
Then created onMessageEvent:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(ImageLoadErrorEvent event) {
MyToast.show("Error");
}
Yay, now it is working!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.