【问题标题】:Data binding not updating view text数据绑定不更新视图文本
【发布时间】:2019-03-06 16:43:56
【问题描述】:

我正在使用下面的代码实现与Observable的数据绑定

可观察

class Model() : BaseObservable() {

    private var  date :Long?=null
    private var  from :String?=null
    private var  to :String?=null


    @Bindable
    fun getFrom(): String? {
        return from
    }

    @Bindable
    fun getDate(): Long? {
        return date
    }

    @Bindable
    fun getTo(): String? {
        return to
    }

    fun setFrom(data: String) {
        from=data
        notifyPropertyChanged(BR.from);
    }

    fun setTo(data: String) {
        to=data
        notifyPropertyChanged(BR.to)
    }


    fun setDate(data: Long) {
        date=data
        notifyPropertyChanged(BR.date)

    }

    fun formatDate():String? {
        return date?.let { it1 -> TBDate(it1).format("dd MMM, yyyy") }

    }

}

xml的视图

<android.support.design.widget.TextInputLayout
    android:id="@+id/tip_date"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/dp_16"
    android:layout_marginRight="@dimen/dp_18"
    app:layout_constraintBottom_toBottomOf="@+id/today"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/today"
    app:layout_constraintTop_toTopOf="@+id/today">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/et_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/date"
        android:text="@{model.formatDate()}"
        android:onClick="@{() -> handler.onClickDate()}"
        android:editable="false"
        android:focusableInTouchMode="false"
        tools:text="12 Feb, Wednesday" />

</android.support.design.widget.TextInputLayout>

Java

override fun onClickXYZ () {
    val calendar = Calendar.getInstance()
    calendar.add(Calendar.DATE, 1)
    viewModel.setDate(calendar.timeInMillis)
}

预期行为-每当使用 setDate() 更改 Observable 的日期值时,视图应以正确的格式更新日期值

实际行为-View 不会更新日期值。Observable 的 setDate() 被调用但 formatDate() 未被调用

【问题讨论】:

    标签: android data-binding observable viewmodel


    【解决方案1】:

    这个呢:

     var formattedDate: String? = null
    
    
     fun setDate(data: Long) {
         date=data
         formatDate(date)
         notifyPropertyChanged(BR.date)
     }
    
     fun formatDate(date: Long):String? {
         formattedDate = date.let { it1 -> TBDate(it1).format("dd MMM, yyyy") }
         notifyPropertyChanged(BR.formattedDate)
     }
    

    并且在xml中你必须使用:

     <android.support.design.widget.TextInputEditText
            android:id="@+id/et_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/date"
            android:text="@{model.formattedDate}"  <--
            ...
     </android.support.design.widget.TextInputLayout>
    

    【讨论】:

    • formatDate() 返回一个字符串(这是正确格式的日期)。并且您的代码没有在任何地方使用该返回的格式化字符串。
    • notifyPropertyChanged for 格式化日期未调用。添加后我发现如果 setFrom() 和 setTo() 在 setDate() 之前调用,则日期视图不会更新
    • 如果您要采用上述方法,您还需要使字段 formattedDate 可绑定并调用 notifyPropertyChanged(BR.formattedDate) 和适当的 getter 方法(例如 getFormattedDate)。这将通知该字段(例如文本字段)上的任何观察者并更新视图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    相关资源
    最近更新 更多