Kotlin issue “One type argument expected for class ExpandableRecyclerAdapter”

﹥>﹥吖頭↗ 提交于 2020-01-11 04:10:14

问题


Got problems with RecyclerAdapter ViewHolder

abstract class ExpandableRecyclerAdapter<T : ExpandableRecyclerAdapter.ListItem>(private val context: Context) : RecyclerView.Adapter<ExpandableRecyclerAdapter.ViewHolder>() {
    protected var allItems = ArrayList<T>()

    ...

    open inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
    open class ListItem(val itemType: Int = 0)  
}

ExpandableRecyclerAdapter in <...> is underlined with error:

One type argument expected for class ExpandableRecyclerAdapter< T : ExpandableRecyclerAdapter.ListItem>

However, if I declare my ViewHolder class as static (by removing inner), the error disappears, but it is unacceptable for me.

Advices like here are not going to be helpful: Kotlin One type argument expected for class for abstract generic view holder

Appreciate the help!


回答1:


Kotlin does not allow generic types to be specified without providing generics unless you're referring to a non-instance member of the class. (static inner classes, ::class, companion methods). Since ViewHolder is an inner class its identity depends on the exact specification of its outer class when talking about the type itself.

This means you cannot refer to a generic ExpandableRecyclerAdapter.ViewHolder, you must specify the bounds in which the outer class lies as well. Changing it to ExpandableRecyclerAdapter<T>.ViewHolder should solve the issue.



来源:https://stackoverflow.com/questions/54267436/kotlin-issue-one-type-argument-expected-for-class-expandablerecycleradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!