使用Dagger 2注入数据绑定适配器

问题描述 投票:1回答:2

我正在使用Android数据绑定适配器,它说,它必须是静态的。所以我试图让它非静态,并通过跟随this tutorial将我的类注入到Dagger中。虽然我可以使用我的Picasso实例,它通常在应用程序中由匕首提供,它说Picasso cannot be provided without an @Inject constructor or an @Provides-annotated method

我将@Inject注释添加到我的绑定适配器构造函数中,但仍然得到相同的错误

public class ImageBindingAdapter {

    private final Picasso picasso;

    @Inject
    public ImageBindingAdapter(Picasso picasso) {
        this.picasso = picasso;
    }

    @BindingAdapter("android:src")
    public void loadImage(ImageView view, String url) {
        picasso.load(url).fit().into(view);
    }
}

我认为问题可能与component问题有关,并改变了我的方法并遵循this link并使用subcomponent。但这次匕首不能生成子组件,我不能像在示例中那样设置它

// Build dagger binding subcomponent and set it like default databinding component 
        DataBindingUtil.setDefaultComponent(sApplicationComponent
                .daggerBindingComponentBuilder()
                .build());

如何将我的自定义类注入到Dagger的绑定适配器中,任何帮助都表示赞赏

这是我的匕首课程,它与我上面提到的教程完全相同

ImageBindingAdapter类

public class ImageBindingAdapter {

    private final Picasso picasso;

    @Inject
    public ImageBindingAdapter(Picasso picasso) {
        this.picasso = picasso;
    }

    @BindingAdapter("android:src")
    public void loadImage(ImageView view, String url) {
        picasso.load(url).fit().into(view);
    }
}

BindingModule类

@Module
public class BindingModule {

    @Provides 
    @DataBinding
    ImageBindingAdapter provideImageBindingAdapter(Picasso picasso) {
        return new ImageBindingAdapter(picasso);
    }
}

绑定Component类

@DataBinding
@Component(dependencies = AppComponent.class, modules = BindingModule.class)
public interface BindingComponent extends DataBindingComponent {

}

AppComponent类

@Singleton
@Component(modules = {AndroidSupportInjectionModule.class, AppModule.class, ...})
public interface AppComponent extends AndroidInjector<MyApp> {

    @Component.Builder
    interface Builder {

        @BindsInstance
        Builder application(Application application);
        AppComponent build();

    }

    @Override
    void inject(MyApp instance);
}

AppModule类

@Module
public class AppModule {

    @Singleton
    @Provides
    Picasso picasso(Application application, OkHttp3Downloader okHttp3Downloader) {
        return new Picasso.Builder(app.getApplicationContext())
                .downloader(okHttp3Downloader)
                .indicatorsEnabled(true)
                .build();
    }

     ....

}

应用类

    public class MyApp extends DaggerApplication {

        @Override
        protected AndroidInjector<MyApp> applicationInjector() {

           AppComponent appComponent = DaggerAppComponent.builder().application(this).build();

            appComponent.inject(this);

            BindingComponent bindingComponent = DaggerBindingComponent.builder()
                .appComponent(appComponent)
                .build();
             DataBindingUtil.setDefaultComponent(bindingComponent);

            return appComponent;
        }
    }
android data-binding picasso dagger-2 android-binding-adapter
2个回答
0
投票

正如错误所说,匕首无法解决Picasso依赖。在您的情况下,问题是不同的匕首组件只能直接使用那些依赖关系,即使用方法声明的@Component批注标记的接口。要允许AppComponentPicasso共享BindingComponent,你需要修改app组件:

@Singleton 
@Component(modules = {AndroidSupportInjectionModule.class, AppModule.class, ...}) 
public interface AppComponent extends AndroidInjector<MyApp> { 
    ...
    Picasso getPicasso();
}

之后,匕首可以正确解决Picasso依赖关系,错误应该消失。


0
投票

@BindingAdapter应该是public static void,请参阅Binding adapters docs

© www.soinside.com 2019 - 2024. All rights reserved.