Travis Griggs
03/15/2020, 5:03 AMclass FungibleRecyclerAdapter<T : RecyclerView.ViewHolder>():RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent:ViewGroup, viewType:Int):T {
return RecyclerView.ViewHolder()
}
override fun getItemCount():Int {
return 1
}
override fun onBindViewHolder(holder:T, position:Int) {
}
}
I got a couple of errors:
1. It complains that FungibleRecyclerAdatper is not a proper subclass because it doesn’t implement all of the methods it should
2. It complains that it does not know what the return ViewHolder() is not what it wants, a T
3. It complains that the onBindViewHolder doesn’t override any method
I can make a “normal” subclass of the Adapter where I specify a specific subclass of RecyclerView.ViewHolder if I want:
MyAdapter() : RecyclerView.Adapter<MyRowViewClass>() { … }
where MyRowViewClass
is a subclass of ViewHolder
just fine. But I wanted to make a generically pluggable one.Travis Griggs
03/15/2020, 6:42 AMclass FungibleRecylerAdapter<T>():RecyclerView.Adapter<T>() where T : RecyclerView.ViewHolder { ... }
louiscad
03/15/2020, 10:22 AMKroppeb
03/15/2020, 12:12 PMclass FungibleRecylerAdapter<T : RecyclerView.ViewHolder>():RecyclerView.Adapter<T>() { ... }
Travis Griggs
03/16/2020, 12:06 AMlouiscad
03/16/2020, 8:12 AMT
in the right hand side of the :
in the class declaration.