fardad
fardad

Reputation: 67

Android recyclerview with different view type in kotlin

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.

Upvotes: 4

Views: 5423

Answers (2)

Blue Jones
Blue Jones

Reputation: 385

  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")
        }
    }

Upvotes: 6

aolphn
aolphn

Reputation: 2998

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))
    } 

}

Upvotes: 4

Related Questions