Responding to clicks in RecyclerView
What if we want to let our users select an item from a presented list? To achieve that, we need to communicate clicks back to our app.
The first step in implementing click interaction is to capture clicks on items at the ViewHolder
level. To maintain separation between our view holder and the adapter, we define a nested OnClickListener
interface in our view holder. We choose to define the interface within the view holder because that and the listener are tightly coupled.
The interface will, in our case, have only one function. The purpose of this function is to inform the owner of the view holder about the clicks. The owner of a view holder is usually a Fragment
or an Activity
. Since we know that a view holder can be reused, we know that it can be challenging to define it at construction time in a way that would tell us which item was clicked (since that item will change over time with reuse).
We work around that by passing the currently...