除了recyclerview.viewholder之外,如何参考适配器中的变量?

发布于 2025-02-10 08:25:13 字数 1794 浏览 2 评论 0原文

有:

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

稍后在适配器中的其他方法

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

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

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

但是它不满意我。
我如何在适配器和类recyclerholder的两个方法中看到相同的变量?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

廻憶裏菂餘溫 2025-02-17 08:25:13

您在哪里调用执行一个解决方案的方法

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

是在调用该方法时将视图传递为参数

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文