RecyclerView with DataBinding

suggest change

Here is a generic ViewHolder class that you can use with any DataBinding layout. Here an instance of particular ViewDataBinding class is created using the inflated View object and DataBindingUtil utility class.

import android.databinding.DataBindingUtil;
import android.view.View;

public class BindingViewHolder<T> extends RecyclerView.ViewHolder{

    private final T binding;

    public BindingViewHolder(View itemView) {
        binding = (T)DataBindingUtil.bind(itemView);

    public T getBinding() {
        return binding;

After creating this class you can use the <layout> in your layout file to enable databinding for that layout like this:

file name: my_item.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="">

            type="ItemModel" />


            android:text="@{item.itemLabel}" />

and here is your sample dataModel:

public class ItemModel {
    public String itemLabel;

By default, Android Data Binding library generates a ViewDataBinding class based on the layout file name, converting it to Pascal case and suffixing “Binding” to it. For this example it would be MyItemBinding for the layout file my_item.xml. That Binding class would also have a setter method to set the object defined as data in the layout file(ItemModel for this example).

Now that we have all the pieces we can implement our adapter like this:

class MyAdapter extends RecyclerView.Adapter<BindingViewHolder<MyItemBinding>>{
    ArrayList<ItemModel> items = new ArrayList<>();
    public MyAdapter(ArrayList<ItemModel> items) {
        this.items = items;

    @Override public BindingViewHolder<MyItemBinding> onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BindingViewHolder<>(LayoutInflater.from(parent.getContext()).inflate(R.layout.my_item, parent, false));

    @Override public void onBindViewHolder(BindingViewHolder<ItemModel> holder, int position) {

    @Override public int getItemCount() {
        return items.size();

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents