Avatar ·

Why is there no onItemClickListener() in RecyclerView and how to properly handle item clicks?

Hi everyone! I just started learning Android development in Java, and I ran into a problem with RecyclerView. Previously, I used ListView, and it had a convenient method setOnItemClickListener() that immediately allowed handling clicks on list items.

\n\n

Now I switched to RecyclerView because everyone says it is more performant and flexible. But I cannot find an analog of onItemClickListener() in it. Why did Android developers remove this method? It seems like such an obvious and necessary feature!

\n\n

Here is an example of my code:

\n\n
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {\n    // ... standard adapter implementation\n}
\n\n

And in the Activity I create a RecyclerView:

\n\n
Recycler