I'm in the process of writing abstract classes for a ListAdapter with a SelectionTracker<Long>. In my case, the SelectionTracker organizes items by selectionKeys via
interface TrackedListItem {
var selectionKey : Long?
}
Now, the items in the RecyclerView (and hence, the items stored in the ListAdapter) need to bind a "viewModel" (note this doesn't extend ViewModel, I'm just following the conventions outlined in Android Sunflower). Sticking with those same conventions, my ViewHolder should take a ViewBinding as its argument:
abstract class TrackedItemViewHolder(val binding: ViewBinding) : RecyclerView.ViewHolder(binding.root)
I know ahead of time that the binding passed to the ViewHolder has a data variable viewModel that I can bind to, and I will know that this viewModel implements TrackedListItem. How can I create a generic layout (and thus binding) that effectively just passes:
<data><variable
name="viewModel"
type="...TrackedListItem" /></data>
A reasonable workaround for anyone interested is to declare:
abstract class TrackedItemViewHolder(val binding: ViewBinding) :
RecyclerView.ViewHolder(binding.root){
abstract val viewModel : TrackedListItem
...
and then in implementations:
override val viewModel : TrackedListItem
get() = (binding as bindingWithViewModel).viewModel!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With