I am using EpoxyModelWithHolder
with multiple layouts in a single Epoxy model class. How can I do ViewBinding? Currently, I am using Kotlin-android-extensions
. Below is my code
@EpoxyModelClass
abstract class ItemSampleEpoxyModel : EpoxyModelWithHolder() {
@EpoxyAttribute
var itemSampleShelf: ComponentFactoryLanderPagesHeroShelf.ComponentLanderPageHeroShelf.ItemLanderPageHeroShelf? =
null
override fun getDefaultLayout() =
when {
itemSampleShelf?.heroStyle == "Project" -> {
R.layout.item_project_sample
}
itemSampleShelf?.shelfItemType == "Course" -> {
R.layout.item_course_sample
}
else -> {
R.layout.item_sample
}
}
override fun bind(itemHolder: ItemSampleEpoxyHolder) {
itemHolder.titleView.text = itemSampleShelf?.title
} }
class ItemSampleEpoxyHolder : EpoxyHolder() {
lateinit var titleView: AppCompatTextView
override fun bindView(itemView: View) {
titleView = itemView.tv_title
}}
First add dependency of epoxy:
After that we need to configure epoxy. In order to do this we need to make a package-info.java file inside the app module and write the below code.
Then we need to keep one thing in mind that whenever we are creating a layout it should be a
data binding layout
and its name should follow the value defined in thelayoutPrefix
variable inpackage-info
for example in my case the layoutPrefix isepoxy_item
so whenever I name a layout it starts withepoxy_item
.Sample Layout: (Layout Name -> epoxy_item_loading)
Now to add this layout to epoxy recycler view:
That's all we need to do :)
NOTE: Sometimes the auto generated classes for the layout is not generated in android studio, so in that case android studio will show the model as unrecognised and red line below it. In that case you need to rebuild the project. sometimes, clean and rebuild project.}