[英]Android - library module's interface method is not called in app module
I am using Android Image Slider library for showing images on a slider. 我正在使用Android Image 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. 这是一个库:在抽象的BaseSliderView类中,有ImageLoadListener接口。 I am setting listener using setOnImageLoadListener method.
我正在使用setOnImageLoadListener方法设置侦听器。
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. 我检查了图像未加载时,库模块中调用了接口onEnd方法。
But on app module, onEnd method is not called even in library module it is called. 但是在应用程序模块上,即使在库模块中也没有调用onEnd方法。
Why is this happening? 为什么会这样呢? Should not onEnd method be called in app module?
是否应在应用模块中调用onEnd方法? How to solve this problem?
如何解决这个问题呢?
I could solve this problem using greenrobot's EventBus library. 我可以使用greenrobot的EventBus库解决此问题。 First of all, I have added library dependency to library build.gradle file:
首先,我将库依赖项添加到库build.gradle文件中:
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: 发表在BaseSliderView类上:
@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: 在活动的onCreate方法内,注册了EventBus:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
EventBus.getDefault().register(this);
Then created onMessageEvent: 然后创建onMessageEvent:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(ImageLoadErrorEvent event) {
MyToast.show("Error");
}
Yay, now it is working! 是的,现在可以了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.