我设法做这样的事情:
class EmptySpaceAdapter(private val context: Context, private val height: Int) : RecyclerView.Adapter<BaseViewHolder<*>>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<*> {
val view = View(context)
view.layoutParams = ViewGroup.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, height)
return EmptySpaceViewHolder(view)
}
override fun getItemCount() = 1
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
override fun onBindViewHolder(holder: BaseViewHolder<*>, position: Int) {
when (holder) {
is EmptySpaceViewHolder -> holder.bind(true, position)
else -> throw IllegalArgumentException("No view holder to show this data, did you forgot to add it to the onBindViewHolder?")
}
}
inner class EmptySpaceViewHolder(itemView: View) : BaseViewHolder<Boolean>(itemView) {
override fun bind(item: Boolean, position: Int) {
Timber.d("onBind empty space")
}
}
}
视图持有者:
abstract class BaseViewHolder<T>(itemView: View) : RecyclerView.ViewHolder(itemView) {
abstract fun bind(item: T, position: Int)
}