我在 Kotlin 中使用(技术动态方法调度或运行时多态性。)创建了具有多种视图类型的RecyclerView, 现在我有 ViewHolder ,如下图所示
abstract class BaseViewHolder<T> internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView){
abstract fun bind(_object:T)
}
我有适配器,如下图所示
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.
}
}
现在我无法在表达式When()后显示onCreateViewHolder方法中的空值并显示消息错误“空值不能是非空类型BaseViewHolder <*>的值” 我不知道该如何解决。
答案 0 :(得分:2)
不要在您的onCreateViewHolder方法中返回null,请尝试按以下代码进行操作
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))
}
}
答案 1 :(得分:2)
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")
}
}