Essential Android RecyclerView and LayoutManagers edit forum

RecyclerView with DataBinding

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 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();

Table Of Contents
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
172 RecyclerView and LayoutManagers
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close