简体   繁体   English

Android-在应用程序模块中未调用库模块的接口方法

[英]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.

相关问题 在任何应用程序模块中未调用方法 onRequestPermissionsResult - Method onRequestPermissionsResult not called in no app module 在 Android 项目查询中引用库模块中的主要应用程序资源 - Referencing main app resources in library module(s) in Android project inquiry Android Instant App:功能模块:该模块不能是android库 - Android Instant App : Feature module : The module cannot be android library 如何使用 android 模块作为应用程序和库 - How to use an android module as app and library 导出依赖于App Engine模块的Android库 - Export Android Library that depends on an App Engine Module Android Studio - App + NDK模块库 - Android Studio - App + NDK Module library 库模块在我的项目应用程序模块android中不起作用 - Library module not working in my projects app module android 应用程序模块中引用的 android 库模块的“未解决的引用”错误 - 'Unresolved reference' errors for android library module referenced in app module android studio 在库模块中导入 .aar 而不是应用模块 - android studio import .aar in library module not app module 如何将项目模块库暴露给 App 模块(Android Studio) - How to expose Project Module Library to App Module (Android Studio)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM