【发布时间】:2019-07-04 07:44:30
【问题描述】:
我正在使用 Android 数据绑定适配器,它说,它必须是静态的。因此,我试图使其成为非静态的,并通过遵循this tutorial 将我的类与 Dagger 注入其中。虽然我可以在应用程序中使用 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);
}
}
BindingComponent 类
@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;
}
}
【问题讨论】:
-
你能分享你的
dagger组件和模块的东西吗?正如错误所说,您没有使用@Provides注释标记 Picassso 的任何构造函数 - 因为据我了解这是不可能的,因为它是您的外部类。所以你需要在类中定义函数,标记为@Module,看起来应该像@Provides public Picasso providePicasso() {TODO cretae picasso instance}。 -
@ConstOrVar 请检查我的编辑,也共享匕首类
-
尝试在
AppComponent接口中添加Picasso getPicasso(); -
据我了解,您可以将任何接口设置为组件依赖项 - 因此 dagger 可以使用该接口中的方法来解决其他依赖项。如果你的组件依赖没有公共方法(除了builder),那么dagger使用内部方法是不安全的(因为它是实现的一部分,可以随时更改——公共方法应该被视为契约)。
-
好的。今天晚些时候我将尝试与您分享子组件方法
标签: android data-binding picasso dagger-2 android-binding-adapter