Issue
I'm trying to create a RecyclerView.Adapter with the following:
View holders - Provided the abstract parent and one son
abstract class BaseSettingsViewHolder<T>(var viewDataBinding :
ViewDataBinding) : RecyclerView.ViewHolder(viewDataBinding.root) {
abstract fun onBind(data: T, presenter: ISettingsPresenter, position: Int)
}
class SettingsTitleViewHolder(viewDataBinding: ViewDataBinding) : BaseSettingsViewHolder<TitleData>(viewDataBinding) {
override fun onBind(data: TitleData, presenter: ISettingsPresenter, position: Int) {
viewDataBinding.setVariable(BR.titleData, data)
viewDataBinding.setVariable(BR.itemPosition, position)
viewDataBinding.setVariable(BR.settingsPresenter, presenter)
viewDataBinding.executePendingBindings()
}
}
And when trying to declare the adapter:
class SettingsAdapter(var context: Context, var presenter: ISettingsPresenter) : RecyclerView.Adapter<BaseSettingsViewHolder>() {
I'm getting "One type argument expected for class" compile error on the:
RecyclerView.Adapter<BaseSettingsViewHolder>
Appreciate the help!
Solution
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 T : 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" ;)
Answered By - Kaloglu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.