为什么RecyclerView适配器不能与ButterKnife一起使用?
它与findViewById一起使用,但不与ButterKnife一起使用
适配器类
public class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.viewHolder>{
Context context;
List<CategoriesModels> categoriesModels;
public CategoriesAdapter(Context context,List<CategoriesModels> categoriesModels){
this.context = context;
this.categoriesModels = categoriesModels;
}
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_categories, viewGroup, false);
viewHolder viewHolder = new viewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull viewHolder viewHolder, int i) {
}
@Override
public int getItemCount() {
return categoriesModels.size();
}
class viewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.img_flag)
ImageView img_flag;
@BindView(R.id.txt_title)
TextView txt_title;
@BindView(R.id.img_correct)
ImageView img_correct;
public viewHolder(View view) {
super(view);
ButterKnife.bind(context,view);
}
}
}
MainActivity类
public class CategoriesActivity extends AppCompatActivity {
@BindView(R.id.txt_select_language)
TextViewFont txt_select_language;
@BindView(R.id.rv_categories)
RecyclerView rv_categories;
List<CategoriesModels> categoriesModels = new ArrayList<>();
CategoriesAdapter categoriesAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
ButterKnife.bind(CategoriesActivity.this);
//show data
ShowDate();
}
private void ShowDate() {
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://*****").addConverterFactory(GsonConverterFactory.create()).build();
RetrofitService retrofitService = retrofit.create(RetrofitService.class);
Call<List<CategoriesModels>> call = retrofitService.get_categories();
call.enqueue(new Callback<List<CategoriesModels>>() {
@Override
public void onResponse(Call<List<CategoriesModels>> call, Response<List<CategoriesModels>> response) {
List<CategoriesModels> list = response.body();
for (CategoriesModels i : list) {
categoriesModels.add(new CategoriesModels(i.getId(), i.getTitle(), i.getPhoto(), i.getShortcut()));
}
categoriesAdapter = new CategoriesAdapter(CategoriesActivity.this, categoriesModels);
rv_categories.setLayoutManager(new LinearLayoutManager(CategoriesActivity.this, LinearLayoutManager.VERTICAL, false));
rv_categories.setAdapter(categoriesAdapter);
}
@Override
public void onFailure(Call<List<CategoriesModels>> call, Throwable t) {
}
});
}
}
在提出主题之前,我做了很多尝试,但是我还没有找到解决方案..................... ................................................... ....
答案 0 :(得分:1)
内部viewHolder构造函数替换上下文为
ButterKnife.bind(this,view);
这是指ViewHolder类,其中包含要注入的视图!\
看看ButterKnife示例。 https://jakewharton.github.io/butterknife/