I created a RecyclerView with multiple view types with(technique Dynamic Method Dispatch or Runtime Polymorphism. ) in Kotlin , now I have ViewHolder As in the figure below
abstract class BaseViewHolder<T> internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView){
abstract fun bind(_object:T)
}
and I have Adapter As in the figure below
class activation_items_main_activity (list: List<out BaseModel>,context: Context):RecyclerView.Adapter<BaseViewHolder<*>>() {
private var mList: List<out BaseModel>? = null
private var mInflator:LayoutInflater ? = null
init {
this.mList = list
this.mInflator = LayoutInflater.from(context)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<*> {
when (viewType)
{
Constant_ViewType_RecyclerView.ViewType.ListOfActivation_Type1 -> return ListOfActivation_MainActivity_Holder(mInflator!!.inflate(R.layout.activities_layout_item,parent,false))
Constant_ViewType_RecyclerView.ViewType.ListOfActivation_Type2 -> return ListOfActivation_MainActivity2_Holder(mInflator!!.inflate(R.layout.activities_layout_items_type2,parent,false))
}
return null // -----> **problem return null**
}
override fun getItemCount(): Int {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onBindViewHolder(holder: BaseViewHolder<*>, position: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getItemViewType(position: Int): Int {
return mList!![position].viewType
}
class ListOfActivation_MainActivity_Holder(itemView: View):BaseViewHolder<ListOfActivation_MainActivity>(itemView)
{
override fun bind(_object: ListOfActivation_MainActivity) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
class ListOfActivation_MainActivity2_Holder(itemView: View):BaseViewHolder<ListOfActivation2_MainActivity>(itemView)
{
override fun bind(_object: ListOfActivation2_MainActivity) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
now I Can't return null value in the onCreateViewHolder method after expression When() and show message error "Null can not be a value of a non-null type BaseViewHolder<*>" I don't know how can I fix it.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<*> {
return when (viewType) {
Constant_ViewType_RecyclerView.ViewType.ListOfActivation_Type1 -> ListOfActivation_MainActivity_Holder(mInflator!!.inflate(R.layout.activities_layout_item, parent, false))
Constant_ViewType_RecyclerView.ViewType.ListOfActivation_Type2 -> ListOfActivation_MainActivity2_Holder(mInflator!!.inflate(R.layout.activities_layout_item2, parent, false))
else -> throw IllegalStateException("Illegal view type")
}
}
Do't return null in your onCreateViewHolder method,try to do as following code
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<*> {
when (viewType)
{
Constant_ViewType_RecyclerView.ViewType.ListOfActivation_Type1 -> return ListOfActivation_MainActivity_Holder(mInflator!!.inflate(R.layout.activities_layout_item,parent,false))
else -> return ListOfActivation_MainActivity2_Holder(mInflator!!.inflate(R.layout.activities_layout_items_type2,parent,false))
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With