Prior to the introduction of RecyclerView
(and its mandatory ViewHolder
pattern), I usually delegate any click events to its corresponding Activity
/Fragment
using setOnItemClickListener()
. (Because I mainly see Activity
/Fragment
as a "controller" object when developing for Android, thus any modification to the view should be done in it.)
Now, as RecyclerView
doesn't really treat its children the same way and that setOnItemClickListener()
(or similar) methods are no longer implemented for it - where should I handle click events that may take place? I don't know.. but handling them in an Adapter
seems awkward to me.
How are we supposed to do it?
Thanks in advance!
Create your own viewHolder for the recycler view as we always do it, and in the onBindView method, set the click listener to the view you wish to perform the click.
@Override
public void onBindViewHolder(final ViewHolder viewHolder, int position) {
viewHolder.mRelContent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// perform ur click here
}
});
}