简体   繁体   中英

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

I have:

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

Then:

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

Later in other method in adapter but apart from class RecyclerHolder I need to call:

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

The problem is that:
name_product and category_product are not visible in those methods.
If I initialize it only in adapter (without RecyclerHolder then I can't call:

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

Beacuse variables are not visible.
The simple but "weak" solution is to do it like this:

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

But it doesn't satisfy me.
How can I see the same variables in both methods in Adapter and class RecyclerHolder ?

from where are you calling that method which executes

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

one solution is while calling the method pass the view as parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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