ExpandableRecyclerAdapter

ExpandableRecyclerAdapter

本文介绍了Kotlin 问题“ExpandableRecyclerAdapter 类需要一个类型参数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

类 ExpandableRecyclerAdapter

预期的一个类型参数T : ExpandableRecyclerAdapter.ListItem>

但是,如果我将 ViewHolder 类声明为静态(通过删除内部),错误就会消失,但对我来说是不可接受的.

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

像这里这样的建议不会有帮助:Kotlin 抽象通用视图持有者的类所期望的一种类型参数

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.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 类需要一个类型参数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 23:59