【问题标题】:RecyclerView adapter not working with butter knifeRecyclerView 适配器不适用于黄油刀
【发布时间】:2019-03-11 07:46:03
【问题描述】:

为什么 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) {

            }
        });
    }

}

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

【问题讨论】:

  • 来自文档:public static Unbinder bind(java.lang.Object target, android.view.View source) 使用源View作为视图根的指定目标中的BindView注解字段和方法。参数: target - 视图绑定的目标类。 source - 查看将在其上查找 ID 的根目录。

标签: android android-studio android-recyclerview adapter butterknife


【解决方案1】:

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

ButterKnife.bind(this,view);

这是指 ViewHolder 类,其中包含您要注入的视图!\

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多