RecyclerView 2020: when recyclerview meets data-binding

Tonny
3 min readJun 15, 2020

This post is about how to use only one RecyclerAdapter to deal with multiple recycler views with Android Jetpack, and it’s inspired by a post from Anton Stulnev.

In this demo, I use PagedListAdapter, but you don’t have to. For the complete project written in Android Jetpack, you can view it in my Github. It contains iOS and Android version.

RecyclerItem, an interface for all UI model of recycler view

To eliminate multiple recycler view adapters, generic is a great way to reduce redundancy. But first, let’s create an abstract interface for UI model which is presented in recycler view.

  • The layoutId is the layout resource id for its view holder.
  • The variableId is the binding resource id of the variable under the <data> tag in XML file.
  • The dataToBind, is a UI model, is the real data presented in view older.
  • The id is the unique property in the UI model, which helps the calculation of the difference to improve the rendering performance of the recycler view.
  • a static diffCallback method, it’s a utility method for creating ItemCallback.
RecyclerItem, an abstract item in recycler view

RecyclerAdaper, one generic adapter for all recycler views

This adapter needs diffCallback to match the constructor of PagedListAdapter, and the other two parameters are optional. It supports placeholder rendering before data-bind happened and event listener adding.

RecyclerAdapter, one Adapter for all

Here is a popular trick when creating view holder: use layout id as viewType to eliminate view type enum. The layoutId of the item comes from the RecyclerItem interface, which is just a reference to the resource id in R.layout. It’s a calculated property rather than a stored property, it’s friendly to memory usage.

create view holder by view type

The ViewHolder is a `dumb` and slim view holder. The data binding logic and the event listener logic are moved out from this class.

The onBindViewHolder method binds the data to the variable, do one thing, do it well. There is no any data processing code.

RecyclerItem and RecyclerAdapter in Action

Now it’s time to use them in action. This is the item layout file in recycler view. The binding resource id BR.user is generated by android studio.

list_item_user.xml

<layout ...>
<data>
<variable
name="user"
type="com.tonnysunm.contacts.room.User" />
</data>
<*ConstraintLayout ...>

The UI model, data class User implements RecyclerItem, it declares the layout, the binding variable, what need to bind, and the unique id.

Interface-oriented programming

Finally, it’s time to check it out how this adapter works.

recycler view with users

You can use RecyclerItem and RecyclerAdapter for other recycler views to present Posts, Comments, or Messages UI models without copy-paste the adapter now.

For the whole project to have fun, please view it in my Github. It contains Jetpack components, such as NavigationUI, Paging Library, LiveData, Data-binding, Room, and Coroutine. Thanks for any feedback.

--

--