Search code examples
javaandroid-studioandroid-recyclerviewandroid-cardview

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


I want to click on Cardview items and intent to another activities using if else statements. Please help.

MainActivity.java

public class MainActivity extends 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 extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {

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

}


Solution

    1. Create interface:

       public interface MyOnClick {
           void onClick(int imageId);
       }
      
    2. Add a reference to MyOnClick to the constructor

      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. Pass MyOnClick to adapter

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