Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a subclass of Recycler.Adapter<VH extends RecylerView.ViewHolder>

I need to define a custom RecyclerView and RecyclerView.Adapter. RecyclerView is OK, but I don't know how to define a subclass of RecyclerView.Adapter because of it's Generic type.

This my code, and Android Studio shows Unexpected bound.

public static abstract class Adapter<VH> extends RecyclerView.Adapter<VH extends RecyclerView.ViewHolder>
like image 531
L. Swifter Avatar asked Dec 09 '22 01:12

L. Swifter


1 Answers

You should define it like this

public static abstract class Adapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
}

the structure is like this

class ABC<-definition-> extends DEF<-usage->

It is because the definition has to be done in quote following the class which you are declaring. In the second quote, you only use it.

The meaning of the whole statement is the VH is definited to be a subclass of RecyclerView.ViewHolder, and the Adapter<VH> is a subclass of RecyclerView.Adapter<VH>. Because VH has the right superclass, so the whole statement would now be valid.

like image 78
Derek Fung Avatar answered May 23 '23 13:05

Derek Fung