![](/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.