繁体   English   中英

除了 RecyclerView.ViewHolder,如何引用适配器中的变量?

[英]How to refer to the variable in adapter apart from RecyclerView.ViewHolder?

我有:

public class RecyclerAdapter extends ListAdapter<Cars, RecyclerAdapter.RecHolder>{
        public TextView name_product1;
        public TextView category_product1;

  class RecyclerHolder extends RecyclerView.ViewHolder {
        public TextView name_product;
        public TextView category_product;

     public RecHolder(@NonNull final View itemView, final Recycler_Adapters_items listener) {
          name_Product = itemView.findViewById(R.id.field1);
          category_Product = itemView.findViewById(R.id.field2);
     }
   }
}

然后:

public void onBindViewHolder(@NonNull RecHolder holder, int position) {
     holder.name_product.setText(currentItem.getname_product());
     holder.category_product.setText(currentItem.getcategory_product());
}

稍后在适配器中的其他方法中,但除了class RecyclerHolder我需要调用:

name_product.setTextSize();
category_product.setTextSize();

问题是:
name_productcategory_product在这些方法中不可见。
如果我只在适配器中初始化它(没有RecyclerHolder那么我不能调用:

public void onBindViewHolder(@NonNull RecHolder holder, int position) {
     holder.name_product.setText(currentItem.getname_product());
     holder.category_product.setText(currentItem.getcategory_product());
}

因为变量不可见。
简单但“弱”的解决方案是这样做:

 public RecHolder(@NonNull final View itemView, final Recycler_Adapters_items listener) {
          nameProduct = itemView.findViewById(R.id.field1);
          category_Product = itemView.findViewById(R.id.field2);
          name_product1 = name_product;
          category_product1 = category_product;
     }

但这并不能满足我。
如何在Adapter和类RecyclerHolder的两个方法中看到相同的变量?

你从哪里调用那个执行的方法

name_product.setTextSize();
category_product.setTextSize();

一种解决方案是在调用方法时将视图作为参数传递

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM