RecyclerView 适配器不适用于黄油刀

RecyclerView adapter not working with butter knife

为什么 RecyclerView 适配器不能与 ButterKnife 一起使用?

它可以与 findViewById 一起使用,但不能与 ButterKnife 一起使用

适配器Class

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);
        }

    }

}

主要活动Class

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) {

            }
        });
    }

}

在提出这个问题之前我尝试了很多,但我还没有找到解决办法...................................... ..................................................... ......

在 viewHolder 构造函数中将上下文替换为 this

ButterKnife.bind(this,view);

这是指包含您要注入的视图的 ViewHolder class!\

查看 ButterKnife 示例。 https://jakewharton.github.io/butterknife/