【发布时间】:2018-05-17 16:04:08
【问题描述】:
我制作了一个 PictureDownloader 来下载图像并设置到我的 viewHolder 中。所以我想在那个viewHolder中注入一个PictureDownloader的实例,但它总是空的,我找不到我的注入错误。我还需要做什么?我需要另一个模块?我必须注入这个 ViewHolder?
这里我带来了所有这些课程:
应用组件:
@Singleton
@Component(modules = {ActivityBindingModule.class,
AppModule.class,
PictureDownloaderModule.class,
AndroidSupportInjectionModule.class})
public interface AppComponent extends AndroidInjector<MarvelApplication> {
PictureDownloader getPictureDownloader();
@Component.Builder
interface Builder {
@BindsInstance
AppComponent.Builder application(Application application);
AppComponent build();
}
}
PictureDonwloaderModule:
@Module
public class PictureDownloaderModule {
@Provides
@Singleton
CacheImageHandler providesCacheImageHandler(Context context) {
return new CacheImageHandler(context);
}
@Singleton
PictureDownloader providesPictureDownloader(CacheImageHandler cacheImageHandler) {
return new PictureDownloader(cacheImageHandler);
}
}
图片下载器:
@Singleton
public class PictureDownloader {
private CacheImageHandler mCacheHandler;
@Inject
public PictureDownloader(CacheImageHandler cacheHandler) {
this.mCacheHandler = cacheHandler;
}
public void download(String url, ImageView imageView) {
if (url == null) {
return;
}
if (imageView == null) {
return;
}
// resetPurgeTimer();
Bitmap bitmap = mCacheHandler.getBitmapFromCache(url);
....
CacheImageHandler:
@Singleton
public class CacheImageHandler {
private Context mContext;
private SoftImageCache mSoftCache;
@Inject
public CacheImageHandler(Context context) {
mContext = context;
int memClass = ( (ActivityManager) mContext.getSystemService( Context.ACTIVITY_SERVICE ) ).getMemoryClass();
int cacheSize = 1024 * 1024 * memClass;
mSoftCache = new SoftImageCache(cacheSize);
}
// Save Bitmap to CacheDir and save it on softCache
public void saveBitmapToCache(String key, Bitmap bitmap) {
if (bitmap != null) {
...
HomeListViewHolder:
public class HomeListViewHolder extends RecyclerView.ViewHolder {
@Inject
PictureDownloader mPictureDownloader;
@BindView(R.id.image_character)
ImageView mImageView;
private View mItemView;
private HomeListListener mListener;
public HomeListViewHolder(View itemView, final HomeListListener listener) {
super(itemView);
ButterKnife.bind(this, itemView);
this.mItemView = itemView;
this.mListener = listener;
}
public void bindModel(final CharacterModel character) {
mPictureDownloader.download(
"https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/shiny/",
mImageView);
mItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mListener != null) {
mListener.onCharacterClick(character);
}
}
});
}
我的应用程序:
public class MarvelApplication extends DaggerApplication {
@Inject
PictureDownloader mPictureDownloader;
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerAppComponent.builder().application(this).build();
}
public PictureDownloader getPictureDownloader(){
return mPictureDownloader;
}
}
【问题讨论】:
-
请have a look here。您既没有进行构造函数注入,也没有进行字段注入,您可以删除除
MarvelApplication中的单个@Inject之外的所有@Inject。请仔细阅读并确保了解何时以及如何使用@Inject -
谢谢我已经阅读了链接,但我看到它的唯一方法是将该类(PictureDownloader)注入我的片段并将其传递给我的适配器的构造函数,然后再传递给构造函数我的 ViewHolder。还有其他方法吗?
标签: java android dagger-2 android-viewholder dagger