在数据绑定中使用LiveData [英] Using LiveData with Data Binding

查看:158
本文介绍了在数据绑定中使用LiveData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着Android体系结构组件的稳定,我开始将所有基本ViewModel更新为 ViewModel .据我了解,建议使用 LiveData 来保存Model类,因为它可以更好地处理生命周期.

With the stabilization of Android Architecture Components I started updating all my basic ViewModels to the new implementation ofViewModel. In my understanding, the usage of LiveData is recommended to hold the Model class since it handles the lifecycle better.

我喜欢使用Data Binding,因为它可以使Java/Kotlin端的代码更清晰,并且不需要观察"值更改来更新UI.但是,如果Model(或ViewModel)扩展了

I like using Data Binding because it makes the code clearer in Java/Kotlin side and it is not needed to "watch" the value changes to update the UI. However the layout using Data Binding only watch data changes if the Model (or the ViewModel) extends BaseObservable and LiveData does not. I understand the one of the main objectives of LiveData is to be observed and updates the UI programmatically but for simple updates, Data Binding is very useful.

已经报告了此问题( GitHub 堆栈溢出),首先是表示1.0版将具有此功能,现在据说此功能正在开发中.

This issue was already reported (GitHub and Stack Overflow) and first was said that the version 1.0 would have it and now is said that this feature is in development.

为了同时使用LiveDataData Binding,我创建了一个扩展BaseObservable的类的非常简单的实现:

In order to use both LiveData and Data Binding, I created a very simple implementation of class that extends BaseObservable:

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.databinding.BaseObservable

class ObservableMutableLiveData<T>() : BaseObservable() {

    private var data: MutableLiveData<T> = MutableLiveData()

    constructor(data: T) : this() {
        this.data.value = data
    }

    public fun set(value: T) {
        if (value != data.value) {
            data.value = value
            notifyChange()
        }
    }

    public fun get(): T? {
        return data.value
    }

    public fun getObservable(): LiveData<T> {
        return data
    }
}

所以基本上我的ObservableMutableLiveData

So basically my ObservableMutableLiveData is a copy of ObservableField using LiveData to store the model and with this implementation, the layout updates after every model update.

问题是:

  • 这是LiveData的错误实现吗?这个包装器是否破坏"了LiveData的功能,例如具有生命周期意识?
  • 据我了解,LiveData是新的ObservableField.这是正确的吗?
  • Is this a bad implementation of LiveData? Does this wrapper "breaks" the functionalities of LiveData, such as be lifecycle-aware?
  • In my understanding, LiveData is the new ObservableField. Is this correct?

推荐答案

Android Studio 3.1(当前在Canary 6中)将解决此问题,因为LiveData可用作observable field:

The Android Studio 3.1 (currently in Canary 6) will fix this issue, since LiveData can be used as observable field:

更新到数据绑定:

Updates to Data Binding:

您现在可以将LiveData对象用作数据绑定表达式中的可观察字段.现在,ViewDataBinding类包含一个新的setLifecycle方法,您需要使用该方法来观察LiveData对象.

You can now use a LiveData object as an observable field in data binding expressions. The ViewDataBinding class now includes a new setLifecycle method that you need to use to use to observe LiveData objects.

来源: Android Studio 3.1 Canary 6现在可用

这篇关于在数据绑定中使用LiveData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆