I also face same problem. While rewriting Java POJO into Data Classes, i face problem with restricted Inheritance on Kotlin Data Class to use in multiple Data Type for Single PageListAdapter with multiple ViewHolder. But after Kotlin version 1.1 , we can start implementing Interface as Inheritance. But using this pattern, we also have to need explicit cast on Data Classes and ViewModel using keyword 'as'.Combine with @Anthony Cannon answer on Generic Class T, I can able to make it work. But I still don't know the reason why IDE show error on using Generic RecyclerView.ViewHolder and needed explicit cast.
Below is my code.
Empty Interface with nothing in it - Super
interface Super
Data Class TypeA implementing Abstract
data class TypeA (val someValueA: someValueA): Super
Data Class TypeB implementing Abstract
data class TypeB (val someValueB: someValueB): Super
PagedListAdapter for handling multiple Data Class and multiple ViewHolder
class CustomPagedListAdapterV2<T : Super, VH : RecyclerView.ViewHolder> internal constructor(private var controller: Controller) :
CustomPagedListAdapterV2<T , VH>(object : DiffUtil.ItemCallback<T>() {
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean {
return false
}
override fun areContentsTheSame(oldItem: T, newItem: T): Boolean {
// TODO :
return false
}
}) { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.layout_adapter_type_a, parent, false) // return TypeBListViewHolder(itemView, controller) as VH //depend on your own implementation
return TypeAListViewHolder(itemView, controller) as VH
}
override fun onBindViewHolder(holder: VH, position: Int) { // (holder as TypeAListViewHolder).bindModel(getItem(position) as TypeB) //depend on your own implementation
(holder as TypeAListViewHolder).bindModel(getItem(position) as TypeA)
}
}
Initializing PagedListAdapter from Fragment/Activity 's ViewModel
private lateinit var mAdapter: CustomPagedListAdapterV2<Super, RecyclerView.ViewHolder>
viewModel.liveData.observe(viewLifecycleOwner, Observer { t ->
mAdapter.submitList(t as PagedList<Super>)
})
Description