Kotlin One type argument expected for class for abstract generic view holder

后端 未结 2 1880
遇见更好的自我
遇见更好的自我 2020-12-30 21:55

I\'m trying to create a RecyclerView.Adapter with the following:


View holders - Provided the abstract parent and one son

abstract class Base         


        
相关标签:
2条回答
  • 2020-12-30 22:25

    You should specify the type argument for the BaseSettingsViewHolder in the RecyclerView.Adapter<BaseSettingsViewHolder> type.

    Kotlin, unlike Java, does not have the raw types, so you cannot simply omit the type arguments.

    The closest concept to raw types is star-projected types: use BaseSettingsViewHolder<*> in RecyclerView.Adapter<BaseSettingsViewHolder<*>>, this will mean that the type argument for BaseSettingsViewHolder is unknown.

    0 讨论(0)
  • 2020-12-30 22:36

    1- If you use always SettingTitleViewHolder;

    RecyclerView.Adapter<SettingTitleViewHolder>
    

    2- If you want to use any class which extended BaseSettingViewHolder;

    RecyclerView.Adapter<BaseSettingsViewHolder<*>>
    

    3- Use Any Object Type

    RecyclerView.Adapter<BaseSettingsViewHolder<Any>>
    

    4- Use extended type parameter for adapter class

    class SettingsMenuAdapter<T>:RecyclerView.Adapter<T> where : BaseSettingViewHolder<*>
    

    5- Define out for abstract class type parameter.

    abstract class BaseSettingsViewHolder<out T> RecyclerView.Adapter<BaseSettingsViewHolder<Any>>
    

    7- Read that article => "Generics in Kotlin" ;)

    0 讨论(0)
提交回复
热议问题