Onclicklistener On The Specific Item Of The Recyclerview In Android
Solution 1:
Multiple onClick events inside a recyclerView:
publicstaticclassMyViewHolderextendsRecyclerView.ViewHolder implementsView.OnClickListener, View.OnLongClickListener {
public ImageView iconImageView;
public TextView iconTextView;
publicMyViewHolder(final View itemView) {
super(itemView);
iconImageView = (ImageView) itemView.findViewById(R.id.myRecyclerImageView);
iconTextView = (TextView) itemView.findViewById(R.id.myRecyclerTextView);
// set click event
itemView.setOnClickListener(this);
iconTextView.setOnClickListener(this);
// set long click event
iconImageView.setOnLongClickListener(this);
}
// onClick Listener for view@OverridepublicvoidonClick(View v) {
if (v.getId() == iconTextView.getId()) {
Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}
//onLongClickListener for view@OverridepublicbooleanonLongClick(View v) {
final AlertDialog.Builderbuilder=newAlertDialog.Builder(v.getContext());
builder.setTitle("Hello Dialog")
.setMessage("LONG CLICK DIALOG WINDOW FOR ICON " + String.valueOf(getAdapterPosition()))
.setPositiveButton("OK", newDialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
returntrue;
}
}
To get which item was clicked you match the view id i.e. v.getId() == yourViewItem.getId()
Solution 2:
You have to set onClickListener
to the ImageView
s inside the onBindViewHolder
method, refer the following LOCs for reference(code to be inside onBindViewHolder
method)
holder.imageView1.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
//put your code for first imageview here
}
});
holder.imageView2.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
//put your code for second imageView here
}
});
Solution 3:
In Recycle View Holder, Write your onclick listener code inside
@OverridepublicvoidonBindViewHolder(CardHolder holder, finalint position) {
holder.imageView.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
//TODO
}
}
}
Solution 4:
implement the View.OnClickListener
in your ViewHolder
class and implement the onClick
method. Then set the click listener for your ImageView to this click listener. Add the required functionality in the onClick method. If you want to implement the click functionality in other class simply create an interface and declare a click method in it. You can implement this method in the activity/fragment that contains this RecycleView. Then from your view holders onClick method you can invoke the interface method.
Solution 5:
You can check with tag or it of element like this:
publicvoidonClick(View view){
if(view.getId() == image1.getId())
{
}elseif(view.getId() == image2.getId())
{}
}
Post a Comment for "Onclicklistener On The Specific Item Of The Recyclerview In Android"