In this post, we will learn how to integrate RecyclerView with Android Data Binding.
Step 1
Enable Data Binding in your module level gradle. Here is how your build.gradle should look.
- apply plugin: 'com.android.application'
- android {
- compileSdkVersion 24
- buildToolsVersion "23.0.3"
- defaultConfig {
- applicationId "com.androidgig.recyclerviewbinding"
- minSdkVersion 15
- targetSdkVersion 24
- versionCode 1
- versionName "1.0"
- dataBinding {
- enabled true
- }
- }
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- }
- }
- dependencies {
- compile fileTree(dir: 'libs', include: ['*.jar'])
- compile 'com.android.support:appcompat-v7:23.4.0'
- compile 'com.android.support:recyclerview-v7:23.4.0'
- compile 'com.squareup.picasso:picasso:2.5.2'
- }
Create POJO/Model class called User with 3 parameters i.e. name, profile_image & age.
- public class User {
- private String name, profile_image;
- private int age;
- public User(String name, String profile_image, int age) {
- this.name = name;
- this.profile_image = profile_image;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getProfile_image() {
- return profile_image;
- }
- public void setProfile_image(String profile_image) {
- this.profile_image = profile_image;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
Create a layout for Recycler list items
- <?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="user"
- type="com.androidgig.recyclerviewbinding.User" />
- </data>
- <LinearLayout android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <LinearLayout android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="5dp"
- android:background="@drawable/round_corner_view"
- android:orientation="horizontal"
- android:padding="10dp">
- <ImageView android:layout_width="50dp"
- android:layout_height="50dp"
- tools:src="@mipmap/ic_launcher"
- app:url="@{user.profile_image}" />
- <LinearLayout android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:layout_marginLeft="10dp"
- android:orientation="vertical">
- <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@{user.name}"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:textColor="@android:color/black"
- tools:text="Name" />
- <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@{`` + user.age}"
- android:textColor="@android:color/black"
- tools:text="Age" />
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
- </layout>


Join the conversation! Your thoughts help the community grow.