首页 > 解决方案 > 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 在 vi​​ewholder.setItemClickListener 中不起作用

有人知道为什么它不起作用吗?

标签: javaandroidfirebase

解决方案


ViewHolder 实际上并不是一个视图。无需实施OnClickListener。只需添加:

itemView.setOnClickListener((view) -> { itemClickListener.onClick(view, getAdapterPosition(), false) })

到 viewHolder 构造函数。


推荐阅读