Issue with RecyclerView.Adapter<> and Kotlin Generics


Hi all,

This was originally posted over in stackoverflow, perhaps you could help me here?

I can’t seem to wrap my head around Kotlin generics, please help

I read here and here and I still can’t determine how to make this work. I have a RecyclerView adapter that uses an abstracted parent BaseFieldVH as the ViewHolder class.

So the Adapter class looks like this:

class MyAdapter() : RecyclerView.Adapter<BaseFieldVH<*>>() {
    override fun onBindViewHolder(holder: BaseFieldVH<*>, position: Int) {

And the ViewHolder implementation looks like this:

abstract class BaseFieldVH<in FF : BaseFormField>
    (itemView: View) : RecyclerView.ViewHolder(itemView) {
    abstract fun bind(formField: FF)

But the actual call to holder.bind(data[position]) is displaying the error:

Out-projected type 'BaseFieldVH<*>' prohibits the use of 'public 
abstract fun bind(formField: FF): Unit defined in ...

use in Nothing?

I’ve also tried defining the Adapter with BaseFieldVH<in Nothing> but then I get a Type Mismatch error for attempting to put Nothing into a function that requires a BaseFormField.

use BaseFormField

Defining the Adapter with BaseFieldVH<iBaseFormField> actually resolves the binding issue, but then in the onCreateViewHolder there is a type mismatch for the view holders:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseFieldVH<BaseFormField> {
    val itemView: View
    when (viewType) {
        HEADER_TYPE -> {
            itemView = LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_header, parent, false)
            return HeaderVH(itemView)

HeaderVH is an extension of BaseFieldVH, and so there’s a type mismatch