![](/img/trans.png)
[英]Method onRequestPermissionsResult not called in no app module
[英]Android - library module's interface method is not called in app module
我正在使用Android Image Slider库在滑块上显示图像。 但是,由于后端需要身份验证,因此未加载某些图像。 所以我需要一个监听器来不加载图像。
这是一个库:在抽象的BaseSliderView类中,有ImageLoadListener接口。 我正在使用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);
}
.....
}
我检查了图像未加载时,库模块中调用了接口onEnd方法。
但是在应用程序模块上,即使在库模块中也没有调用onEnd方法。
为什么会这样呢? 是否应在应用模块中调用onEnd方法? 如何解决这个问题呢?
我可以使用greenrobot的EventBus库解决此问题。 首先,我将库依赖项添加到库build.gradle文件中:
compile 'org.greenrobot:eventbus:3.0.0'
为事件创建了类:
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;
}
}
发表在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);
}
}
在活动的onCreate方法内,注册了EventBus:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
EventBus.getDefault().register(this);
然后创建onMessageEvent:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(ImageLoadErrorEvent event) {
MyToast.show("Error");
}
是的,现在可以了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.