java - viewHolder.setItemClickListener() 不工作
问题描述
ItemClickListener() 接口
public interface ItemClickListener {
void onClick(View view, int position, boolean isLongClick);
}
MyProductViewHolder 类
public class MyProductViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView pname,pprice ;
public ImageView pimage;
ItemClickListener itemClickListener;
public MyProductViewHolder(@NonNull View itemView) {
super(itemView);
pname = itemView.findViewById(R.id.product_name);
pprice = itemView.findViewById(R.id.product_price);
pimage = itemView.findViewById(R.id.product_image);
}
@Override
public void onClick(View v) {
itemClickListener.onClick(v,getAdapterPosition(),false);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
}
我的产品类
private void loadProducts() {
auth = FirebaseAuth.getInstance();
user = auth.getCurrentUser();
myProductref = FirebaseDatabase.getInstance().getReference("Admin").child(user.getUid()).child("Products");
adapter = new FirebaseRecyclerAdapter<MyProducts, MyProductViewHolder>(
MyProducts.class,
R.layout.my_products,
MyProductViewHolder.class,
myProductref
) {
@Override
protected void populateViewHolder(final MyProductViewHolder viewHolder, final MyProducts model, int position) {
Glide.with(getActivity()).load(model.getImage1()).into(viewHolder.pimage);
viewHolder.pname.setText(model.getName());
viewHolder.pprice.setText(model.getPrice());
final String pid = model.getPid();
final String name = model.getName();
final String price = model.getPrice();
final String description = model.getDescription();
final String image1 = model.getImage1();
final String image2 = model.getImage2();
final String image3 = model.getImage3();
final String uid = model.getUid();
final String category = model.getCategoryName();
final String date = model.getDate();
final String time = model.getTime();
final String location =model.getLocation();
try {
viewHolder.setItemClickListener((view, position1, isLongClick) -> {
Intent intent = new Intent(getActivity(),ProductDetailsActivity.class);
intent.putExtra("pid",pid);
intent.putExtra("name",name);
intent.putExtra("price",price);
intent.putExtra("description",description);
intent.putExtra("image1",image1);
intent.putExtra("image2",image2);
intent.putExtra("image3",image3);
intent.putExtra("uid",uid);
intent.putExtra("category",category);
intent.putExtra("date",date);
intent.putExtra("time",time);
intent.putExtra("location",location);
startActivity(intent);
});
}
catch (Exception e){
FancyToast.makeText(getActivity(),e.getMessage(),FancyToast.LENGTH_LONG,FancyToast.ERROR,true).show();
}
}
};
adapter.notifyDataSetChanged();
listproduct.setAdapter(adapter);
}
问题出在 loadProducts() 方法 Intent 在 viewholder.setItemClickListener 中不起作用
有人知道为什么它不起作用吗?
解决方案
ViewHolder 实际上并不是一个视图。无需实施OnClickListener
。只需添加:
itemView.setOnClickListener((view) -> { itemClickListener.onClick(view, getAdapterPosition(), false) })
到 viewHolder 构造函数。
推荐阅读
- python-3.x - 如何在 python 的类中使用光线并行性?
- javascript - 填充输入时自动添加值
- javascript - e.source.getActiveSheet(); 触发错误
- scala - 使用 Spark Scala 在满足条件后标记记录
- php - 将静态变量设置为函数 php 类
- ansible - Ansible:不要注册跳过的迭代
- xcode - Xcode“引用包含在框架标题中”
- rest - 如何通过 API 请求使用营销电子邮件模板?
- vb.net - StreamWriter 随机不将最后一行输出到文件中
- openlayers - 我怎么知道 modifyEvent 是否被捕捉