Creating a ViewHolder with data binding
As you can see from building a traditional ViewHolder
implementation, there is quite a lot of work and boilerplate code required just to put the data from a single item onto the screen in a layout. Further, it's actually quite expensive in its own right, because every one of the ViewHolder
instances creates and holds an instance of the DateFormatter
where they can easily be shared between all the ClaimItemViewHolder
instances for a RecyclerView
.
In cases like this, data binding can make a huge difference. Using a few tricks, you can actually create a completely generic ViewHolder
implementation that will work for any data object in your application (assuming that you can bind it to a layout file). First, you'll need to create a nice generic ItemPresenter
, and then modify the layout, and then you're ready to create a generic data-binding ViewHolder
implementation. Follow these instructions, and you'll only ever need one ViewHolder
implementation:
- Right...