除了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());
}
稍后在适配器中的其他方法中
name_product.setTextSize();
category_product.setTextSize();
我 name_product
和category_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在哪里调用执行一个解决方案的方法
是在调用该方法时将视图传递为参数
from where are you calling that method which executes
one solution is while calling the method pass the view as parameter