This repository shows how Epoxy
library works with Android Databinding,
For more information, check Epoxy library.
apply plugin: 'kotlin-kapt'
kapt {
correctErrorTypes = true
}
android {
...
// Databinding
buildFeatures {
dataBinding true
}
}
// Recycler View
implementation 'androidx.recyclerview:recyclerview:1.1.0'
// Data Binding
kapt 'com.android.databinding:compiler:3.5.0'
// epoxy
kapt 'com.airbnb.android:epoxy-processor:4.1.0'
implementation 'com.airbnb.android:epoxy:4.1.0'
implementation 'com.airbnb.android:epoxy-databinding:4.1.0'
annotationProcessor 'com.airbnb.android:epoxy-processor:4.1.0'
For now, 4.1.0
is the latest version of Epoxy library.
You don't have to use both EpoxyDataBindingPatterns
and EpoxyConfig
If names of resource files has some patterns, use EpoxyDataBindingPatterns.
package dev.haenara.epoxyexample
import com.airbnb.epoxy.EpoxyDataBindingPattern
@EpoxyDataBindingPattern(rClass = R::class, layoutPrefix = "epoxy_")
object EpoxyDataBindingPatterns
If you use R2, use it instead of R::class
If only specific files is used for epoxy, try EpoxyConfig instead.
package dev.haenara.epoxyexample
import com.airbnb.epoxy.EpoxyDataBindingLayouts
import com.airbnb.epoxy.PackageModelViewConfig
@PackageModelViewConfig(rClass = R::class)
@EpoxyDataBindingLayouts(R.layout.epoxy_item_sample)
interface EpoxyConfig
If you use R2, use it instead of R::class
Use RecyclerView from epoxy libaray instead of Android Jetpack🚀
.
<com.airbnb.epoxy.EpoxyRecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Model files that inherits EpoxyModel
would be generated automatically when you build your project👷.
Check this sample file.
<?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="data"
type="dev.haenara.epoxyexample.Data" />
<variable
name="onClick"
type="android.view.View.OnClickListener" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="100dip">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:text="@{data.title}"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@id/tv_subtitle"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
tools:text="Title" />
<TextView
android:id="@+id/tv_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:text="@{data.subtitle}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_title"
app:layout_constraintVertical_chainStyle="packed"
tools:text="Subtitle" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:onClick="@{onClick}"
android:text="Confirm"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Since this resource file's name is epoxy_item_sample.xml
, the generated file name would be EpoxyItemSample
.
Using EpoxyDataBindingPattern, the file name would start after prefix. For instance ItemSample
when prefix is "epoxy_".
Also a closure has been created as same name as models but starting a lower letter like itemSample
, so that you can easily define ItemSample
in EpoxyController.
If a model file hasn't be generated, build or assemble once.
AndroidStudio 4.0.1 has a bug that it cannot follow renaming packages. In case of a trouble with model auto-generation, just restart AndroidStudio.
🙅♂️ ViewHolder is not required, if you uses databinding.
class SampleController(val data: List<Data>) : EpoxyController() { }
Instead of EpoxyController
, you may use TypedEpoxyController
with generic. (or Typed2EpoxyController
, ...)
override fun buildModels() {
dataList.forEachIndexed { index, data ->
itemSample {
id("sample$index")
title(data.title)
subtitle(data.subtitle)
onClick { _ ->
onClick(data)
}
}
}
}
Inside of buildModels()
, you can put model on RecyclerView as just declaring model with simple closure.
Name of model (in this case sample
is the model) is depends on layout resource name.
If you uses TypedEpoxyController
, it has data on buildModels()
and setData()
must be called before requestModelBuild()
.
recycler_view.setController(
SampleController(
listOfData() // Data Set
)
)
recycler_view.requestModelBuild() // let magic happen
MIT License
Copyright (c) 2020 Haenala Shin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.