如何在 recyclerview 中为 cardview 设置带有 int 数组的 onclicklistener android

how to set onclicklistener with int array in recyclerview for cardview android

我想点击 Cardview 项目并使用 if else 语句进行其他活动。请帮忙。

MainActivity.java

public class MainActivity 扩展 AppCompatActivity {

private RecyclerView imagesRV;
private int[] images = {R.drawable.image01, R.drawable.image02, R.drawable.image03, R.drawable.image04, R.drawable.image05, R.drawable.image06, R.drawable.image07, R.drawable.image08};
private RecyclerViewAdapter imageAdapter;
//private ArrayList<String> imagePaths;


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


    imagesRV = (RecyclerView) findViewById(R.id.idRVImages);

    prepareRecyclerView();
}

private void prepareRecyclerView() {
    imageAdapter = new RecyclerViewAdapter(MainActivity.this, images);

    GridLayoutManager manager = new GridLayoutManager(MainActivity.this, 2);

    imagesRV.setLayoutManager(manager);
    imagesRV.setAdapter(imageAdapter);
}

}

RecyclerViewAdapter.java

public class RecyclerViewAdapter 扩展 RecyclerView.Adapter {

private final Context context;
private final int[] imagePathArrayList;

public RecyclerViewAdapter(Context context, int[] imagePathArrayList) {
    this.context = context;
    this.imagePathArrayList = imagePathArrayList;
}

@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);

    return  new RecyclerViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
    int imageId = imagePathArrayList[position];

        Picasso.get().load(imageId).placeholder(R.drawable.ic_launcher_background).into(holder.imageIV);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    
            }
        });
        
}

@Override
public int getItemCount() {
    return imagePathArrayList.length;
}

public static class RecyclerViewHolder extends RecyclerView.ViewHolder {

    private final ImageView imageIV;

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

        imageIV = itemView.findViewById(R.id.idIVImage);
    }
}

}

  1. 创建界面:

     public interface MyOnClick {
         void onClick(int imageId);
     }
    
  2. 在构造函数中添加对 MyOnClick 的引用

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {
        ...
        private final MyOnClick myOnClick;
    
        public RecyclerViewAdapter(Context context, int[] imagePathArrayList, MyOnClick myOnClick) {
            this.context = context;
            this.imagePathArrayList = imagePathArrayList;
            this.myOnClick = myOnClick
        }
        ...
        @Override
        public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
            final int imageId = imagePathArrayList[position];
            Picasso.get().load(imageId).placeholder(R.drawable.ic_launcher_background).into(holder.imageIV);
    
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myOnClick.onClick(imageId)
                }
            });
        }
    
  3. 将 MyOnClick 传递给适配器

     public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {
         private RecyclerView imagesRV;
         private int[] images = {R.drawable.image01, R.drawable.image02, R.drawable.image03, R.drawable.image04, R.drawable.image05, R.drawable.image06, R.drawable.image07, R.drawable.image08};
         private RecyclerViewAdapter imageAdapter;
         //private ArrayList<String> imagePaths;
    
         @Override
         protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home_page);
            imagesRV = (RecyclerView) findViewById(R.id.idRVImages);
            prepareRecyclerView();
         }
    
         private void prepareRecyclerView() {
             imageAdapter = new RecyclerViewAdapter(this, images, new MyOnClick() {
                 @Override
                 public void onClick(int imageId) {
                     // Your action
                 }
             });
    
             GridLayoutManager manager = new GridLayoutManager(MainActivity.this, 2);
    
             imagesRV.setLayoutManager(manager);
             imagesRV.setAdapter(imageAdapter);
         }
     }