大家!如何注入活動和片段到RecyclerAdapter(匕首2)
我是Dagger2的新手,但我真的很想理解它。因此,首先讓我們看一下RecyclerAdapter的代碼,我已經使用一些應用程序級別注入(DbHelper和應用程序上下文)實現了該代碼。
此外,通知書的,在這裏我們有DataSetChangeListener和DataProviderListener的兩個實例:
- DataSetChangeListener - 是去調用一些片段的方法(這是管理的意見)的接口;
- DataProviderListener - 是一個調用Activity(管理數據模型調用)中的方法的接口;
所以,這裏是代碼:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.EmotionsViewHolder> {
@Inject
DbHelper mDbHelper;
@Inject
Context mContext;
private ArrayList<String> mItemsList;
private DataSetChangeListener mDataSetChangeListener;
private DataProviderListener mDataProviderListener;
public RecyclerAdapter(DataProviderListener dataProviderListener, DataSetChangeListener listener) {
App.getAppComponent().inject(this);
//TODO: Replace with dagger 2 injection
mDataSetChangeListener = listener;
mDataProviderListener = dataProviderListener;
mItemsList = dataProviderListener.getDataSet();
notifyFragmentAboutDataSetStatus();
}
private void notifyFragmentAboutDataSetStatus() {
if (mItemsList.size() == 0) {
mDataSetChangeListener.notifyDataSetIsEmpty();
} else {
mDataSetChangeListener.notifyDataSetIsNotEmpty();
}
}
@Override
public EmotionsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cbt_element, parent, false);
return new EmotionsViewHolder(view);
}
@Override
public void onBindViewHolder(EmotionsViewHolder holder, final int position) {
holder.mEmotionsTextView.setText(mItemsList.get(position));
holder.mEditImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editItem(position);
}
});
holder.mDeleteImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
removeItem(position);
}
});
}
@Override
public int getItemCount() {
return mItemsList.size();
}
public void addItem(String item) {
mItemsList.add(item);
notifyItemInserted(mItemsList.size());
}
private void editItem(int position) {
String currentValue = mItemsList.get(position);
mDataProviderListener.callEditDialog(currentValue);
}
private void removeItem(int position) {
String[] selectionArgs = new String[]{mItemsList.get(position)};
mItemsList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mItemsList.size());
Uri uri = UniquesContract.EmotionsEntity.CONTENT_URI;
String selection = UniquesContract.EmotionsEntity.EMOTION_COL + " = ?";
mContext.getContentResolver().delete(uri, selection, selectionArgs);
}
static class EmotionsViewHolder extends RecyclerView.ViewHolder {
TextView mEmotionsTextView;
ImageView mEditImageView;
ImageView mDeleteImageView;
public EmotionsViewHolder(View view) {
super(view);
mEmotionsTextView = (TextView) view.findViewById(R.id.emotion_text_view);
mEditImageView = (ImageView) view.findViewById(R.id.edit_item_image_view);
mDeleteImageView = (ImageView) view.findViewById(R.id.delete_item_image_view);
}
}
public void setNewDataSet(ArrayList<String> list) {
mItemsList = list;
}
}
我意識到,理論上我可以注入DataSetChangeListener和DataProviderListener,但我怎麼能做到這一點。也許,重點是創建一個新的範圍?但我不知道如何使用它。我怎麼能在RecyclerAdapter中實例化這兩個對象,如果我甚至沒有能力從RecyclerAdapter類中引用它們。將實例作爲構造函數的傳遞者已經不是DI了。我應該在這裏做什麼?
我寧願注入INSI de片段/活動,我需要什麼,並將其作爲參數傳遞給適配器。 –