问题描述
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下划线带有错误:
ExpandableRecyclerAdapter in <...>
is underlined with error:
但是,如果我将ViewHolder类声明为静态类(通过删除内部类),则错误消失了,但对我来说是不可接受的.
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
感谢帮助!
推荐答案
除非您引用的是非实例成员,否则Kotlin不允许在不提供泛型的情况下指定泛型类型. (静态内部类,::class
,伴随方法).由于ViewHolder
是内部类,因此在谈论类型本身时,其身份取决于其外部类的确切说明.
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.
这意味着您不能引用泛型ExpandableRecyclerAdapter.ViewHolder
,必须指定外部类所在的范围.将其更改为ExpandableRecyclerAdapter<T>.ViewHolder
应该可以解决问题.
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.
这篇关于Kotlin问题“类ExpandableRecyclerAdapter需要一种类型参数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!