How to use RecyclerView with Android Databinding

In this android programming source code example, we are going to use RecyclerView with Android Databinding.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to use RecyclerView with Android Databinding.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="countryAdapter"
            type="com.bluapp.androidview2.firebaseMlKitAndDataBinding.FirebaseMlKitAndDataBindingActivity16.CountryAdapter" />
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="3dp">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adapter="@{countryAdapter}"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

MainActivity.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bluapp.androidview2.BR;
import com.bluapp.androidview2.R;
import com.bluapp.androidview2.databinding.ActivityFirebaseMlKitAndDataBinding16Binding;
import com.bluapp.androidview2.databinding.ItemDatabind16RowBinding;

import java.util.ArrayList;
import java.util.List;

public class FirebaseMlKitAndDataBindingActivity16 extends AppCompatActivity {
    private ActivityFirebaseMlKitAndDataBinding16Binding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_firebase_ml_kit_and_data_binding16);
        populateData();
    }

    private void populateData(){
        List<CountryModel> countryModelList = new ArrayList<>();
        countryModelList.add(new CountryModel("Nigeria"));
        countryModelList.add(new CountryModel("India"));
        countryModelList.add(new CountryModel("Sweden"));
        countryModelList.add(new CountryModel("Usa"));
        countryModelList.add(new CountryModel("Ghana"));
        countryModelList.add(new CountryModel("South Africa"));

        CountryAdapter countryAdapter = new CountryAdapter(countryModelList, this);
        binding.setCountryAdapter(countryAdapter);
    }

    public class CountryModel{
        private String countryName;
        public CountryModel(String countryName){
            this.countryName = countryName;
        }

        public String getCountryName(){
           return countryName;
        }
    }

    public interface CustomClickListner{
        void cardClicked(CountryModel countryModel);
    }

    public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.ViewHolder> implements CustomClickListner{
        private List<CountryModel> countryModelList;
        private Context context;

        public CountryAdapter(List<CountryModel> countryModelList, Context context){
            this.countryModelList = countryModelList;
            this.context = context;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
            ItemDatabind16RowBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_databind16_row, parent, false);
            return new ViewHolder(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position){
            CountryModel countryModel = countryModelList.get(position);
            holder.bind(countryModel);
            holder.itemDatabind16RowBinding.setItemClickListner(this);
        }

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


        public class ViewHolder extends RecyclerView.ViewHolder{
            public ItemDatabind16RowBinding itemDatabind16RowBinding;

            public ViewHolder(ItemDatabind16RowBinding itemDatabind16RowBinding){
                super(itemDatabind16RowBinding.getRoot());
                this.itemDatabind16RowBinding = itemDatabind16RowBinding;
            }

            public void bind(Object object){
                itemDatabind16RowBinding.setVariable(BR.model, object);
                itemDatabind16RowBinding.executePendingBindings();
            }
        }

        public void cardClicked(CountryModel countryModel){
            Toast.makeText(context, "Item Click "+countryModel.countryName, Toast.LENGTH_LONG).show();
        }
    }
}

item_databind16_row.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="model"
            type="com.bluapp.androidview2.firebaseMlKitAndDataBinding.FirebaseMlKitAndDataBindingActivity16.CountryModel" />

        <variable
            name="itemClickListner"
            type="com.bluapp.androidview2.firebaseMlKitAndDataBinding.FirebaseMlKitAndDataBindingActivity16.CustomClickListner" />
    </data>
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="@{() -> itemClickListner.cardClicked(model)}"
        app:cardUseCompatPadding="true">

       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:orientation="vertical"
           android:layout_margin="8dp">

           <TextView
               android:id="@+id/country"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="@{model.countryName}" />

       </LinearLayout>


    </androidx.cardview.widget.CardView>
</layout>

app/build.gradle

dataBinding {
        enabled = true
    }

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment