簡體   English   中英

啟用視圖狀態保存后,如何在Epoxy ModelView中正確初始化屬性?

[英]How do I properly initialize a property in Epoxy ModelView when view state saving is enabled?

我有以下代碼用於模型視圖。

當我禁用saveViewState = true或將其刪除時, checkbox?.isChecked由環氧樹脂適配器正確設置(根據傳遞給帶注釋的方法的isChecked布爾值,設置為true或false)。 但是,當我啟用它(設置saveViewState = true )時, checkbox?.isChecked值始終設置為false(至少在我在UI中看到的情況下,所有復選框均未選中)。

我將日志放在this.checkbox?.isChecked = isChecked和之后,我看到傳遞的值正確,並且checkbox isChecked屬性設置正確。 我不明白的是,為什么環氧樹脂會覆蓋所有這些內容,並將復選框設置為未檢查狀態(為false),盡管其屬性已設置為例如已檢查狀態。 我試圖在構建模型之后立即在環氧樹脂視圖上執行requestModelBuild ,但會有所延遲,但這沒有幫助。

@ModelView(saveViewState = true)
class RowView: ConstraintLayout {
    constructor(context: Context):
        super(context)
    constructor(context: Context, attributeSet: AttributeSet):
        super(context, attributeSet)
    constructor(context: Context, attributeSet: AttributeSet, styleAttr: Int):
        super(context, attributeSet, styleAttr)

    @TextProp
    fun setText(text: CharSequence) {
        this.checkbox?.text = text
    }

    @ModelProp
    fun setCheckedState(isChecked: Boolean) {
        this.checkbox?.isChecked = isChecked
    }

    @CallbackProp
    fun setOnChangeListener(listener: CompoundButton.OnCheckedChangeListener?) {
        listener?.let { this.checkbox?.setOnCheckedChangeListener(it) }
    }
}

啟用視圖狀態后,如何在環氧樹脂模型視圖內設置復選框狀態? 使用EditText時也會發生此問題嗎? 以及為什么正確填寫了復選框標簽(沒有空文本,傳遞的文本按原樣顯示)?

https://github.com/airbnb/epoxy/issues/681中所述,屬性狀態應存儲在其他位置,並且onChangeListener應該在最后請求模型重建。 即“您無法同時通過已保存狀態和模型道具提供數據,因為它們沖突,已保存狀態會覆蓋模型道具設置”。

為此,我必須進行更改

@ModelView(saveViewState = true)
class RowView: ConstraintLayout {

@ModelView
class RowView: ConstraintLayout {

並像這樣實現模型重建

view.rv.buildModelsWith { controller ->
    model.items.forEach { item ->
        RowViewModel_().id(item.id.name)
            .checkedState(model.itemsChosen[item] ?: false)
            .onChangeListener { buttonView, isChecked ->
                if (buttonView.isShown && buttonView.isPressed) {
                    model.itemsChosen[item] = isChecked
                    controller.requestModelBuild()
                }
            }
            .addTo(controller)
    }
}

測試了一下,它起作用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM