0

第一次使用 Epoxy Library。得到以下错误

rocess: in.droom, PID: 25269
com.airbnb.epoxy.IllegalEpoxyUsage: You must set an id on a model before adding it. Use the @AutoModel annotation if you want an id to be automatically generated for you.

这是控制器代码:

   class MyMatchesController : EpoxyController() {
    override fun buildModels() {
        for (i in 0 until 10) {
            fillBestMatchesNotification {
                id(i)
                bestMatchesCount("100")
            }
        }
    }
}

这是模型代码

    @EpoxyModelClass(layout = R.layout.fill_best_match_notification_layout)
abstract class FillBestMatchesFormNotificationModel : EpoxyModelWithHolder<FillBestMatchesFormNotificationModel.ViewHolder>() {

    @EpoxyAttribute
    var id: Long = 0

    @EpoxyAttribute
    var bestMatchesCount = ""


    override fun getDefaultLayout() = R.layout.fill_best_matches_notification_layout

    @CallSuper
    override fun bind(holder: ViewHolder) {
        super.bind(holder)
        holder.countTV.text = bestMatchesCount
    }

    override fun createNewHolder(): ViewHolder {
        return ViewHolder()
    }

    inner class ViewHolder : EpoxyHolder() {
        lateinit var countTV: TextView
        override fun bindView(itemView: View) {
            countTV = itemView.findViewById(R.id.matchedCountNotificationTV)
        }

    }
}

我试图从 Model 类中删除id但仍然是同样的错误。

4

1 回答 1

0

id是一个保留属性,不能用 . 声明为新字段@EpoxyAttribute

只需定义您所需的属性。id设置器(具有不同的重载)将自动生成。

所以删除下面的行。一切都会好起来的。

@EpoxyAttribute
var id: Long = 0
于 2021-03-23T15:58:39.823 回答