How to use Epoxy with Databinding (PART 3)
Simple Listing using Epoxy Databinding
Like in previous Part we have seen how simple to list item using Epoxy ViewHolder.
In this post, we gonna use Databinding to list items on RecyclerView/EpoxyRecyclerView
Listing with Epoxy databinding is bit different, like in android we do databinding by defining <data></data>
block or using BindingAdapter. In Epoxy we use these features directly and we don’t write any extra class to handle all these functions.
3. Show List using Databinding
Enable databinding in build.gradle and epoxy-databinding dependencies
android {
\\
...
dataBinding {
enabled = true
}
}
dependencies {
def epoxyVersion = "3.11.0"
implementation "com.airbnb.android:epoxy:$epoxyVersion"
//Databinding
implementation "com.airbnb.android:epoxy-databinding:$epoxyVersion"
kapt "com.airbnb.android:epoxy-processor:$epoxyVersion"
}
We need to add this package-info.java file also to generate code for all epoxy databinding layout
@EpoxyDataBindingPattern(rClass = R.class, layoutPrefix = "epoxy")
package com.dastnaiqbal.epoxysample;
import com.airbnb.epoxy.EpoxyDataBindingPattern;
Like class name explaining @EpoxyDataBindingPattern
, we have to define the name pattern like layoutPrefix = "epoxy"
for epoxy databinding, to seperate it out with other layouts. all the layout which start with "epoxy"
for eg epoxy_databinding.xml , Epoxy processor autogenerates all the code for these layouts.
Layout epoxy_databinding.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="title"
type="java.lang.String" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:padding="8dp"
android:text="@{title}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
As you can see it’s same like android databinding no other epoxy stuff.
Now we just have to implement SimpleDatabindingController to set all the data on EpoxyRecyclerView/RecyclerView,
SimpleDatabindingController is same like SimpleDataController or SimpleViewHolderController
Controller SimpleDatabindingController
class SimpleDataBindingController : TypedEpoxyController<List<String>>() {
private val TAG = this::class.java.simpleName
override fun buildModels(data: List<String>?) {
data?.forEachIndexed { index, str ->
databinding { //This is your layout name after prefix "epoxy_databinding"
id(index)
title(str)
}
}
}
}
As you can its same like other controllers which we have defined for viewholder and dataclass, the only thing we can notice here is databinding{}
block, don’t confuse with Android databinding, it’s a layout name epoxy_databinding.xml ,
Block name generates whatever comes after layoutPrefix ("epoxy")
, which we have defined in package-info.java. and block name generates in camelCase if multiple underscores(_) in layout name for eg epoxy_my_databinding.xml
then block name will be myDatabinding{}
So we have defined our layout, our controller now we just need to attach in RecyclerView
package com.dastnaiqbal.epoxysample
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.dastnaiqbal.epoxysample.databinding.SimpleDataBindingController
import com.dastnaiqbal.epoxysample.dataclass.SimpleDataController
import com.dastnaiqbal.epoxysample.viewholder.SimpleViewHolderController
import kotlinx.android.synthetic.main.activity_main.*
/**
*
* "Iqbal Ahmed" created on 22/08/2020
*/
class MainActivity : AppCompatActivity() {
private val dataClassController by lazy {
SimpleDataController()
}
private val viewHolderController by lazy {
SimpleViewHolderController()
}
private val dataBindingController by lazy {
SimpleDataBindingController()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/**
* Listing using Data Clas
*/
// rv.adapter = dataClassController.adapter
// dataClassController.setData(ArrayList<String>().apply {
// repeat(10) {
// add("Item Data Class #$it")
// }
// })
/**
* Listing using ViewHolder
*/
// rv.adapter = viewHolderController.adapter
// viewHolderController.setData(ArrayList<String>().apply {
// repeat(10) {
// add("Item View Holder #$it")
// }
// })
/**
* Listing using Databinding
*/
rv.adapter = dataBindingController.adapter
dataBindingController.setData(ArrayList<String>().apply {
repeat(10) {
add("Item Databinding #$it")
}
})
}
}
Congratulations You populated your list in Epoxy using Databinding.
In this post, we used Kotlin Databinding that autogenerate EpoxyModel for databinding and Simple Controller,
In the Next post, we gonna use CustomView as an Epoxy Model and Instead of creating a separate adapter, we gonna use extension method to populate data on Epoxy RecyclerView.
SourceCode Link: https://github.com/DastanIqbal/EpoxySample/tree/epoxy/databinding
Happy Coding !!