与 Kotlin 通用适配器的类型不匹配
Type Mismatch with Kotlin Generic Adapter
我正在尝试创建一个基础适配器 class,它可以由所有其他 RecyclerView 适配器扩展,因为它们彼此之间没有太大区别。
这是我的 BaseAdapter
class:
private const val IS_EMPTY = 0
private const val IS_NOT_EMPTY = 1
abstract class BaseAdapter<T>(
@LayoutRes open val layoutId: Int,
private val dataList: ArrayList<T>?
) : RecyclerView.Adapter<BaseViewHolder<Any>>() {
abstract fun setViewHolder(parent: ViewGroup): BaseViewHolder<Any>
abstract fun bind(containerView: View, item: T)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<Any> {
return setViewHolder(parent)
}
override fun onBindViewHolder(holder: BaseViewHolder<Any>, position: Int) {
bind(holder.containerView, dataList!![position])
}
override fun getItemCount(): Int = dataList!!.size
override fun getItemViewType(position: Int): Int = if (dataList!!.size == 0) IS_EMPTY else IS_NOT_EMPTY
}
这是我的 BaseViewHolder
class:
abstract class BaseViewHolder<in T: Any>(override val containerView: View) : RecyclerView.ViewHolder(containerView),
LayoutContainer
来自 class (LessonsRecyclerViewAdapter
) 的片段,我正在尝试实现它:
override fun setViewHolder(parent: ViewGroup): BaseViewHolder<Any> {
val view = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
return LessonsViewHolder(view)
//Type mismatch: Required: BaseViewHolder<Any> Found: LessonsRecyclerViewAdapter.LessonsViewHolder
}
LessonsViewHolder
只是一个空的 class,扩展 BaseViewHolder
:
class LessonsViewHolder(override val containerView: View): BaseViewHolder<Lesson>(containerView)
当 LessonsViewHolder
扩展 BaseViewHolder
时,为什么我会收到类型不匹配错误?
那是因为您混合使用 T
和 Any
类型,试试这个:
BaseViewHolder
abstract class BaseViewHolder<T>(override val containerView: View) :
RecyclerView.ViewHolder(containerView),
LayoutContainer
BaseViewAdapter
abstract class BaseAdapter<T>(
@LayoutRes open val layoutId: Int,
private val dataList: ArrayList<T>?
) : RecyclerView.Adapter<BaseViewHolder<T>>() {
abstract fun setViewHolder(parent: ViewGroup): BaseViewHolder<T>
abstract fun bind(containerView: View, item: T)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<T> {
return setViewHolder(parent)
}
override fun onBindViewHolder(holder: BaseViewHolder<T>, position: Int) {
bind(holder.containerView, dataList!![position])
}
override fun getItemCount(): Int = dataList!!.size
override fun getItemViewType(position: Int): Int =
if (dataList!!.size == 0) IS_EMPTY else IS_NOT_EMPTY
}
我正在尝试创建一个基础适配器 class,它可以由所有其他 RecyclerView 适配器扩展,因为它们彼此之间没有太大区别。
这是我的 BaseAdapter
class:
private const val IS_EMPTY = 0
private const val IS_NOT_EMPTY = 1
abstract class BaseAdapter<T>(
@LayoutRes open val layoutId: Int,
private val dataList: ArrayList<T>?
) : RecyclerView.Adapter<BaseViewHolder<Any>>() {
abstract fun setViewHolder(parent: ViewGroup): BaseViewHolder<Any>
abstract fun bind(containerView: View, item: T)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<Any> {
return setViewHolder(parent)
}
override fun onBindViewHolder(holder: BaseViewHolder<Any>, position: Int) {
bind(holder.containerView, dataList!![position])
}
override fun getItemCount(): Int = dataList!!.size
override fun getItemViewType(position: Int): Int = if (dataList!!.size == 0) IS_EMPTY else IS_NOT_EMPTY
}
这是我的 BaseViewHolder
class:
abstract class BaseViewHolder<in T: Any>(override val containerView: View) : RecyclerView.ViewHolder(containerView),
LayoutContainer
来自 class (LessonsRecyclerViewAdapter
) 的片段,我正在尝试实现它:
override fun setViewHolder(parent: ViewGroup): BaseViewHolder<Any> {
val view = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
return LessonsViewHolder(view)
//Type mismatch: Required: BaseViewHolder<Any> Found: LessonsRecyclerViewAdapter.LessonsViewHolder
}
LessonsViewHolder
只是一个空的 class,扩展 BaseViewHolder
:
class LessonsViewHolder(override val containerView: View): BaseViewHolder<Lesson>(containerView)
当 LessonsViewHolder
扩展 BaseViewHolder
时,为什么我会收到类型不匹配错误?
那是因为您混合使用 T
和 Any
类型,试试这个:
BaseViewHolder
abstract class BaseViewHolder<T>(override val containerView: View) :
RecyclerView.ViewHolder(containerView),
LayoutContainer
BaseViewAdapter
abstract class BaseAdapter<T>(
@LayoutRes open val layoutId: Int,
private val dataList: ArrayList<T>?
) : RecyclerView.Adapter<BaseViewHolder<T>>() {
abstract fun setViewHolder(parent: ViewGroup): BaseViewHolder<T>
abstract fun bind(containerView: View, item: T)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<T> {
return setViewHolder(parent)
}
override fun onBindViewHolder(holder: BaseViewHolder<T>, position: Int) {
bind(holder.containerView, dataList!![position])
}
override fun getItemCount(): Int = dataList!!.size
override fun getItemViewType(position: Int): Int =
if (dataList!!.size == 0) IS_EMPTY else IS_NOT_EMPTY
}