如何为我在回收站视图中的卡片视图中的每个项目创建一个点击监听器

Posted

技术标签:

【中文标题】如何为我在回收站视图中的卡片视图中的每个项目创建一个点击监听器【英文标题】:How can I make a click listener for every Item I have in my card view inside my Recycler View 【发布时间】:2022-01-05 15:50:39 【问题描述】:

我是 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) 

    


这是活动:

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




【问题讨论】:

如何确定不同的行为? 类名不好。请首字母大写。就像你的代码reviewsAdapter reviews 和下划线不应该出现 【参考方案1】:

只需在onBindViewHolder 上添加holder.itemView setOnClickListener(view -&gt; //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));
            );
    
    

【讨论】:

还是不明白。如果我把 holder.itemView setOnClickListener(view -> //TODO ) 它会点击整张卡片,但在我的卡片中我有另一个元素,比如喜欢按钮、不喜欢按钮和删除按钮,我想要能够单击每个按钮并执行不同的操作。在我的活动类中,我有代码告诉我单击该项目时要做什么。我的问题是我在哪里为每个按钮编写代码,如果我在我的活动类中编写它,无论我点击哪里,它都会做那件事,正如你所看到的那样,我在那里有一个 Toast。 i want to be able to click each button and do diffrent things 那么是什么阻止了你?然后不要使用holder.itemView.setOnClickListener,而是使用您想要添加侦听器的实际组件?有很多资源可用于处理回收站中的点击监听器【参考方案2】:
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);
       
    
)

【讨论】:

这些是我想要点击的项目和一些东西: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); 我的问题是我把上面的代码放在哪里,我把这个持有者设置在哪里做某事。 检查我的编辑。你可以为 viewHolder 做任何事情来保持视图。当你得到 recyclerViewHolder 时。 好的,我把每件商品的代码放在哪里? onItemclicktodo () 对不起,但我还是不明白,我需要将代码放在适配器或活动类中的 onItemclicktodo () 上吗?以及我如何知道哪个项目将使用我的代码?太令人沮丧了,我不明白...

以上是关于如何为我在回收站视图中的卡片视图中的每个项目创建一个点击监听器的主要内容,如果未能解决你的问题,请参考以下文章

如何为同一视图中的每个 UIImageView 分配从相机拍摄的照片?

如何在回收站视图中的每个项目下方创建寻呼机指示器

React Native ListView 回收

XML/android:如何为 RecyclerView 项目制作白色下划线? [复制]

如何为 UICollectionViewFlowLayout 中的每个单元格添加标题/标签补充视图?

如何为一些子成员设置卡片视图的角半径?