0

EpoxyModelWithHolder在单个 Epoxy 模型类中使用多个布局。如何进行 ViewBinding?目前,我正在使用Kotlin-android-extensions. 下面是我的代码

@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
}}
4

1 回答 1

0

首先添加epoxy的依赖:

def epoxyVersion = '4.6.3'
implementation "com.airbnb.android:epoxy:$epoxyVersion"
implementation "com.airbnb.android:epoxy-databinding:$epoxyVersion"
kapt "com.airbnb.android:epoxy-processor:$epoxyVersion"

之后我们需要配置环氧树脂。为此,我们需要在 app 模块中创建一个 package-info.java 文件并编写以下代码。

@EpoxyDataBindingPattern(rClass = R.class, layoutPrefix = "epoxy_item")
interface Config {
}

然后我们需要记住一件事,每当我们创建一个布局时,它应该是 adata binding layout并且它的名称应该遵循layoutPrefix变量中定义的值package-info,例如,在我的例子中,layoutPrefix 就是epoxy_item这样,每当我命名一个布局时,它都以 . 开头epoxy_item

示例布局:(布局名称 -> 环氧树脂项目加载)

<?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">

<data>

    <variable
        name="isVisible"
        type="Boolean" />


</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:isVisible="@{isVisible}">

    <ProgressBar
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:theme="@style/ProgressBlueTheme"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
 </layout>

现在将此布局添加到环氧树脂回收器视图:

        epoxyRecycler.withModels {
               loading {
                id("progress layout")
                isVisible(isLoading)// boolean variable
            }
        }

这就是我们需要做的一切:)

于 2022-01-28T12:47:17.497 回答