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
publicclassViewHolderextendsRecyclerView.ViewHolder implementsView.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
publicvoidonClick(View v) {
switch (v.getId()) {
case R.id.imageV_seat1:
// Code Herebreak;
// 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.
publicclassViewHolderextendsRecyclerView.ViewHolder implementsView.OnClickListener
@OverridepublicvoidonClick(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.
publicclassViewHolderextendsRecyclerView.ViewHolder implementsView.OnClickListener {
private Button mButton;
privateViewHolder(View view)
{
.....Your existing code
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
.
.
.
button50.setOnClickListener(this);
}
@OverridepublicvoidonClick(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(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
// your code hare
}
});
Post a Comment for "How Can Add Same Onclick For All Imageview In Recyclerview?"