Search code examples
androidandroid-recyclerviewtype-parameter

In creating an adapter for a recyclerview, why one has to append the RecyclerView.Adapter which it extends with <[NameOfAdapter].Viewholder>?


In my search how to create a RecyclerView I came across several examples where the adapter of the RecyclerView is created as follows:

public class MyAdapter extends 
RecyclerView.Adapter<MyAdapter.ViewHolder> {
...
}

Why is <MyAdapter.ViewHolder> appended here? It might be something what I think is called a type parameter (like with List<T> for instance), but in the documentation of RecyclerView.Adapter I'm not able to find a confirmation that indeed this concerns a type parameter.


Solution

  • The full name (packages included) is android.support.v7.widget.RecyclerView.Adapter<VH extends android.support.v7.widget.RecyclerView.ViewHolder>. So RecyclerView.Adapter does in fact have a RecyclerView.ViewHolder as type parameter. This is used in the onCreateViewHolder and onBindViewHolder methods so that these know what kind of ViewHolder they're dealing with.