Every Android developer knows the workflow how to implement a RecyclerView: Creating a ViewHolder, an adapter and finally apply it to the RecyclerView. Imho it is quite cumbersome to invest so much overhead for a (from the view point of a user) "simple" and (from the view point of a developer) often used UI component like RecyclerViews.
An additional feature in Android development was the introduction of the Data Binding Library at Google I/O 2015 to remove boilerplate code. Data Binding uses declarative layouts and minimises the glue code between programming code and XML. For those who are not yet familiar with the Data Binding Library, check out the official Android developer docs: https://developer.android.com/topic/libraries/data-binding/index.html.
Although Data Binding was already introduced in 2015, there is still no Data Binding extension for lists and RecyclerViews. Wouldn’t it be nice if Data Binding can be used for RecyclerViews?
Like in the ViewHolder-Adapter-RecyclerView approach, we need to define a layout for our list entries. This time we are using data binding.
The ViewModel is the glue code to bind the entry layout to a collection entry.
ViewModel and its binding object
data classGasStation(val businessHours:List<BusinessHours>?=null)
data classBusinessHours(val days:String,val time:String):Serializable
The ViewModel is now announced to the view. The data is introduced by the app:items property via data binding to the declared RecyclerView. The description how the data shall be mapped to the entry layout is defined by app:itemBinding. A usual app:layoutManager must be declared as well.
All RecyclerViews (prices, opening hours etc.) were concisely implemented for our gas price station app SprITdue to this awesome library.
In our point of view, data binding for RecyclerViews is more intuitive, easier to comprehend, easier to learn and much easier to implement than the ViewHolder-Adapter-RecyclerView way. If you need to implement more complex scenarios, checkout the library’s documentation: https://github.com/evant/binding-collection-adapter.