Я все еще изучаю Dagger 2 и пытался создать простое приложение, используя его.У меня проблемы с работой Picasso, так как я не вижу ошибок в журналах.Вот мой код
AppModule.java
@Module(includes = {AndroidInjectionModule.class, NetworkModule.class, ViewModelModule.class})
public class AppModule {
...
@Provides
@AppScope
Picasso picasso(App app, OkHttp3Downloader okHttp3Downloader) {
return new Picasso.Builder(app.getApplicationContext())
.downloader(okHttp3Downloader)
.loggingEnabled(true)
.build();
}
...
}
NetworkModule.java
Здесь находится зависимость OkHttp3Downloader.
@Module
public class NetworkModule {
@Provides
@AppScope
HttpLoggingInterceptor loggingInterceptor() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> Timber.i(message));
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return interceptor;
}
@Provides
@AppScope
public File file(App app) {
return new File(app.getApplicationContext().getCacheDir(), "okhttp_cache");
}
@Provides
@AppScope
Cache cache(File file) {
return new Cache(file, 10 * 1000 * 1000);
}
@Provides
@AppScope
OkHttpClient okHttpClient(HttpLoggingInterceptor loggingInterceptor, Cache cache) {
return new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.cache(cache)
.build();
}
@Provides
@AppScope
OkHttp3Downloader okHttp3Downloader(OkHttpClient okHttpClient) {
return new OkHttp3Downloader(okHttpClient);
}
}
AppComponent.java
@AppScope
@Component(modules = {AppModule.class, AndroidSupportInjectionModule.class, BuildersModule.class})
public interface AppComponent{
void inject(App app);
@Component.Builder
interface Builder {
@BindsInstance
Builder application(App application);
AppComponent build();
}
}
App.java
Это мой класс App.java, где я инициализировал Dagger.
public class App extends Application implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
@Override
public void onCreate() {
super.onCreate();
Timber.plant(new Timber.DebugTree());
DaggerAppComponent.builder()
.application(this)
.build()
.inject(this);
}
@Override
public AndroidInjector<Activity> activityInjector() {
return dispatchingAndroidInjector;
}
}
И в своей деятельности я использовал инъекцию поля@Inject MoviesAdapter adapter;
и использовал конструктор инъекций в моем адаптере
@Inject
public MoviesAdapter(Picasso picasso) {
this.picasso = picasso;
}
Затем я вызвал picasso.load(...)
метод.Но никакие изображения не загружаются в ImageViews моего RecyclerView.Ниже приведен журнал Пикассо, и он не говорит об ошибке.В чем может быть проблема?Что-то не так в том, как я инициализировал Пикассо с помощью Dagger?Ваша помощь очень ценится.
D/Picasso: Main created [R230] Request{https://image.tmdb.org/t/p/w500//8bcpki9GfXdXj9esFpPtlate8v0.jpg}
D/Picasso: Hunter removed [R222]+466ms from
D/Picasso: Dispatcher enqueued [R230]+4ms
D/Picasso: Hunter executing [R230]+5ms
D/Picasso: Main created [R231] Request{https://image.tmdb.org/t/p/w500//4nKoB6wMVXfsYgRZK5lHZ5VMQ6J.jpg}
D/Picasso: Hunter removed [R223]+635ms from
D/Picasso: Dispatcher enqueued [R231]+5ms
D/Picasso: Hunter executing [R231]+13ms
D/Picasso: Main created [R232] Request{https://image.tmdb.org/t/p/w500//5LYSsOPzuP13201qSzMjNxi8FxN.jpg}
D/Picasso: Hunter removed [R224]+706ms from
D/Picasso: Dispatcher enqueued [R232]+3ms
D/Picasso: Hunter executing [R232]+4ms
D/Picasso: Main created [R233] Request{https://image.tmdb.org/t/p/w500//2lIr27lBdxCpzYDl6WUHzzD6l6H.jpg}
D/Picasso: Hunter removed [R226]+637ms from
D/Picasso: Dispatcher enqueued [R233]+2ms
D/Picasso: Hunter executing [R233]+4ms
D/Picasso: Main created [R234] Request{https://image.tmdb.org/t/p/w500//tCBxnZwLiY1BOKw3tH6AxHZdqPh.jpg}
D/Picasso: Hunter removed [R225]+736ms from
D/Picasso: Hunter executing [R234]+4ms
D/Picasso: Dispatcher enqueued [R234]+3ms
Редактировать
Вкл. onBindViewHolder()
Я позвонил picasso.load()
@Override
public void onBindViewHolder(@NonNull MoviesHolder holder, int position) {
picasso.load("https://image.tmdb.org/t/p/w500/" + movieList.get(position).getPosterPath()).into(holder.ivMovie);
}