如何为 Recycler View 中卡片视图中的每个项目创建点击监听器

How can I make a click listener for every Item I have in my card view inside my Recycler View

我是 Android 的新手,我有一个项目要做,但我一直坚持这个。我正在尝试从我的卡片视图中制作不同的项目来做不同的事情。目前我可以点击任何我想要的项目,但所有的项目都会做同样的事情。我怎样才能让每个项目做自己的事情。这是我的适配器:

package com.mecachrome.ffbf;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.List;


public class reviewsAdapter extends RecyclerView.Adapter<reviewsAdapter.ViewHolder> {

private Context context;
private List<reviews> review;
private reviewsAdapter.ItemClickListener mItemListener;


public reviewsAdapter(Context context, List<reviews> review,reviewsAdapter.ItemClickListener itemClickListener) {
    this.context = context;
    this.review = review;
    this.mItemListener = itemClickListener;
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(context).inflate(R.layout.reviews_card_layout,parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    reviews currentReview = review.get(position);
    holder.rev_username.setText(currentReview.getUsername());
    holder.rev_text.setText(currentReview.getText());
    holder.rev_user_rating.setRating(currentReview.getUserrating());
    Picasso.get().load(currentReview.getProfilepicUrl()).fit().centerCrop().into(holder.rev_user_pic);
    holder.rev_like_nr.setText(String.valueOf(currentReview.getLikes()));
    holder.rev_dislike_nr.setText(String.valueOf(currentReview.getDislikes()));

    holder.rev_like_btn.setOnClickListener(view ->{
           mItemListener.onItemClick(review.get(position));
        });

}

@Override
public int getItemCount() {
    return review.size();
}

public interface ItemClickListener{
    void onItemClick(reviews place);
}


public class ViewHolder extends RecyclerView.ViewHolder implements  View.OnClickListener{
    public TextView rev_username;
    public TextView rev_text;
    public RatingBar rev_user_rating;
    public ImageView rev_user_pic;
    public ImageView rev_like_btn;
    public ImageView rev_dislike_btn;
    public ImageView rev_delete_btn;
    public TextView rev_like_nr;
    public TextView rev_dislike_nr;

    public ViewHolder(@NonNull View itemView)  {
        super(itemView);

        rev_username = itemView.findViewById(R.id.rcv_username);
        rev_text = itemView.findViewById(R.id.rcv_user_review);
        rev_user_rating = itemView.findViewById(R.id.rcv_user_rating);
        rev_user_pic = itemView.findViewById(R.id.rcv_user_profile_img);
        rev_like_btn = itemView.findViewById(R.id.iv_like);
        rev_dislike_btn = itemView.findViewById(R.id.iv_dislikes);
        rev_delete_btn = itemView.findViewById(R.id.rcv_delete_btn);
        rev_like_nr = itemView.findViewById(R.id.rcv_likes);
        rev_dislike_nr = itemView.findViewById(R.id.rcv_dislikes);
        
    }
    @Override
    public void onClick(View v) {

    }
}
}

这里是 activity:

package com.mecachrome.ffbf;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class Restaurant_Reviews extends AppCompatActivity {

Button add_review;
TextView restaurant_name;
ImageView rev_like_btn;
RecyclerView rv_reviews;
reviewsAdapter myadapter;
private places place;

private ArrayList<reviews> reviewsArrayList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_restaurant_reviews);

    place = getIntent().getParcelableExtra("restaurant");
    DatabaseReference databaseref = FirebaseDatabase.getInstance().getReference("restaurants").child(place.getId()).child("reviews");

    restaurant_name = findViewById(R.id.tv_add_restaurant_name);
    restaurant_name.setText(place.getName()+ " Reviews");
    rev_like_btn = findViewById(R.id.iv_like);

    reviewsArrayList = new ArrayList<>();

    databaseref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            for (DataSnapshot dss: snapshot.getChildren()){

                reviews rev = dss.getValue(reviews.class);
                reviewsArrayList.add(rev);

            }
           myadapter = new reviewsAdapter(Restaurant_Reviews.this, reviewsArrayList, place -> {
           Toast.makeText(Restaurant_Reviews.this, "Clicked", Toast.LENGTH_SHORT).show();
            });

            rv_reviews.setAdapter(myadapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(Restaurant_Reviews.this, error.getMessage(), Toast.LENGTH_SHORT).show();
        }

    });

    add_review = findViewById(R.id.btn_add_review);
    add_review.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Restaurant_Reviews.this, add_review.class);
            intent.putExtra("restaurant",place);
            startActivity(intent);
        }
    });

    rv_reviews = findViewById(R.id.rv_restaurant_reviews);
    rv_reviews.setHasFixedSize(true);
    rv_reviews.setLayoutManager(new LinearLayoutManager(this));


}
}

只需在 onBindViewHolder

上添加 holder.itemView setOnClickListener(view ->{ //TODO })
@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    
        reviews currentReview = review.get(position);
        holder.rev_username.setText(currentReview.getUsername());
        holder.rev_text.setText(currentReview.getText());
        holder.rev_user_rating.setRating(currentReview.getUserrating());
        Picasso.get().load(currentReview.getProfilepicUrl()).fit().centerCrop().into(holder.rev_user_pic);
        holder.rev_like_nr.setText(String.valueOf(currentReview.getLikes()));
        holder.rev_dislike_nr.setText(String.valueOf(currentReview.getDislikes()));
    
        holder.itemView.setOnClickListener(view ->{
               mItemListener.onItemClick(review.get(position));
            });
    
    }
public interface ItemClick{
    void onItemClick(View view,int position,Reviews place);
}
onBindViewHolder(){
    holder.rev_like_btn.setOnClickListener(view ->{
           mItemListener.onItemClick(view,position,review.get(position));
        });
}
public setItemClick(ItemClick itemClick){
    this.itemClick = itemClick
}
adapter.setItemClick(new ItemClick{
    @Override
    void onItemClick(view,position,place){//TODO()}
})

编辑

onBindViewHolder(){
    holder.rev_like_btn.setOnClickListener(view ->{
         mItemListener.onItemClick(view,position,review.get(position));
        });
    holder.rev_delete_btn.setOnClickListener(view ->{
         mItemListener.onItemClick(view,position,review.get(position));
        });
    holder.rev_like_nr.setOnClickListener(view ->{
         mItemListener.onItemClick(view,position,review.get(position));
        });
    holder.rev_dislike_nr.setOnClickListener(view ->{
         mItemListener.onItemClick(view,position,review.get(position));
        });
}
adapter.setItemClick(new ItemClick{
    @Override
    void onItemClick(view,position,place){
        ViewHolder holder = 
            recyclerView.findViewHolderByAdapterPosition(position);
       
    }
})