I was exploring RecyclerView
and I was surprised to see that RecyclerView
does not have onItemClickListener()
. Because RecyclerView
extends
android.view.ViewGroup
and ListView
extends
android.widget.AbsListView
. However I solved my problem by writing onClick
in my RecyclerView.Adapter
:
public static class ViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
public TextView txtViewTitle;
public ImageView imgViewIcon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
}
@Override
public void onClick(View v) {
}
}
But still I want to know why Google removed onItemClickListener()
?
Is there a performance issue or something else?
Android Recyclerview With
onItemClickListener
, Why we cant try this is working likeListView
only.Source : Link
And Set this to RecyclerView:
Here you can handle multiple onclick see below code and it is very efficient
This worked for me:
If you want to add onClick() to the child view of items, for example, a button in item, I found that you can do it easily in onCreateViewHolder() of your own RecyclerView.Adapter just like this:
i don't know whether it's a good way, but it works well. If anyone has a better idea, very glad to tell me and correct my answer! :)
Access the
mainView
ofrowLayout(cell)
for youRecyclerView
and in yourOnBindViewHolder
write this code: