简体   繁体   English

EpoxyModels 在 Android 的功能模块/库项目中不起作用

[英]EpoxyModels are not working in feature modules/library projects in Android

EpoxyModels are not compiling when using inside feature modules of Android App.使用 Android 应用程序的内部功能模块时,EpoxyModels 未编译。

@EpoxyModelClass(layout = R.layout.layout_foo) //an annotation argument must be a compile-time constant here
abstract class  FooModel : EpoxyModelWithHolder<FooModel.FooHolder>() 
{
            ...
            ...
        
    class FooHolder : BaseEpoxyHolder() 
     {
       val textViewTitle: TextView by bind((R.id.textViewTitle))
       //bind is the method borrowed from [here](https://github.com/airbnb/epoxy/blob/963ef0fd850bd379da7b0be6a2ada25d01ae0ee7/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/helpers/KotlinEpoxyHolder.kt#L20)
     }
}

The above code complains about "an annotation argument must be a compile-time constant" for layout = R.layout.layout_foo line.上面的代码抱怨 layout = R.layout.layout_foo 行的“an annotation argument must be a compile-time constant”。

Again based on the documentation looks like, butterknife needs to be used for library projects(feature modules are kind of library project to some extent) that would generate R2 classes based on this再次基于文档看起来,butterknife 需要用于库项目(功能模块在某种程度上是一种库项目),它将基于生成 R2 类

Below is the modified code with Butterknife which also I think it's overkill.下面是使用 Butterknife 修改后的代码,我也认为它太过分了。 Not sure, why I was not able just to do findviewbyId.不确定,为什么我不能只做 findviewbyId。

ModelClass(layout = R2.layout.layout_foo)
abstract class  FooModel : EpoxyModelWithHolder<FooModel.FooHolder>() {

    @EpoxyAttribute
    lateinit var fooDto: Foo

    override fun bind(holder: FooHolder) {
        holder.textViewTitle.text = fooDto.title
    }

    class FooHolder : BaseEpoxyHolder() {
        @BindView(R2.id.textViewTitle) lateinit var textViewTitle: TextView
    }
}

Below is the error coming from viewholder with butterknife下面是来自带有 butterknife 的 viewholder 的错误

kotlin.UninitializedPropertyAccessException: lateinit property textViewTitle has not been initialized kotlin.UninitializedPropertyAccessException:lateinit 属性 textViewTitle 尚未初始化

As said in:正如所说:

https://github.com/airbnb/epoxy/issues/819#issuecomment-576728923 https://github.com/airbnb/epoxy/issues/819#issuecomment-576728923

There is a problem with using that annotation on library project.在库项目上使用该注释存在问题。

For the moment you can use:目前你可以使用:

@EpoxyModelClass
abstract class MyModel : EpoxyModelWithHolder<MyHolder>() {

    override fun getDefaultLayout(): Int {
        return R.layout.my_layout
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM