Skip to content Skip to sidebar Skip to footer

How Can Add Same OnClick For All ImageView In Recyclerview?

My project do bus ticket booking So, when click item in recyclierview that opens bus seat as 50 imageView. I know how can add onClick for one imageView but for 50 imageView that I

Solution 1:

Firstly implement the interface OnClickListener to your ViewHolder

 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener 

Then add the click listeners to image view like this

private ViewHolder(View view) {
                super(view);

      //Your code here after

 imageV_seat1.setOnClickListener(this);
    //add for all the image views


}

And on Override method onClick user Switch statement to get the fast execution

public void onClick(View v) {

        switch (v.getId()) {

            case R.id.imageV_seat1:

               // Code Here
                break;

     // add for reset ids

}
}

Solution 2:

To make code small make use of lambda:

holder.yourImage.setOnClickListener(view -> {
    // do something here
});

Solution 3:

You can implement onCLickListener and in onClick() method check its imageView, If its imageView you can write your logic.

 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener

@Override
public void onClick(View view) {
    if(view instanceof ImageView)
   //write your logic here or you can get view.getId method also to write logic for individual imageview
}

Solution 4:

This can be implemented using some logical game. Though I have not tested it as I do not have IDE available now but the code should work.

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
    private Button mButton; 

     private ViewHolder(View view)
    { 
        .....Your existing code

        button1.setOnClickListener(this); 
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        .
        .
        .
        button50.setOnClickListener(this);
    } 

    @Override
    public void onClick(View view) 
    { 
        switch (view.getId()) { 
        case R.id.button1: 
        case R.id.button2:
        case R.id.button3:
        .
        .
        .
        case R.id.button50:
            // Do something... by doing this all case will come to this block to execute.
            // Remember, do not use break in any of the above case.
        } 
    } 
}

Let me know if it solved your issue.


Solution 5:

Just do like that:-

holder.yourImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // your code hare
            }
        });

Post a Comment for "How Can Add Same OnClick For All ImageView In Recyclerview?"