将布局视图绑定到架构组件

AndroidX 库包含架构组件,可用于设计可靠、可测试且可维护的应用。数据绑定库可与架构组件无缝协作,进一步简化界面的开发。应用中的布局可以绑定到架构组件中的数据,这有助于您管理界面控制器的生命周期,并通知界面有关数据的变化。

本页介绍了如何将架构组件整合到您的应用中,以便充分利用数据绑定库。

使用 LiveData 将数据变化通知给界面

您可以使用 LiveData 对象作为数据绑定来源,自动向界面发出数据更改通知。如需详细了解此架构组件,请参阅 LiveData 概览

与实现 Observable 的对象(例如可观察字段)不同,LiveData 对象了解已订阅数据更改的观察者的生命周期。了解这一点可以带来许多好处,详见使用 LiveData 的优势。在 Android Studio 3.1 及更高版本中,您可以在数据绑定代码中将可观察字段替换为 LiveData 对象。

如需将 LiveData 对象与绑定类一起使用,您需要指定生命周期所有者,以定义 LiveData 对象的范围。以下示例在绑定类实例化后将 activity 指定为生命周期所有者:

Kotlin

class ViewModelActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Inflate view and obtain an instance of the binding class.
        val binding: UserBinding = DataBindingUtil.setContentView(this, R.layout.user)

        // Specify the current activity as the lifecycle owner.
        binding.setLifecycleOwner(this)
    }
}

Java

class ViewModelActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Inflate view and obtain an instance of the binding class.
        UserBinding binding = DataBindingUtil.setContentView(this, R.layout.user);

        // Specify the current activity as the lifecycle owner.
        binding.setLifecycleOwner(this);
    }
}

如下一部分所述,您可以使用 ViewModel 组件将数据绑定到布局。在 ViewModel 组件中,您可以使用 LiveData 对象转换数据或合并多个数据源。以下示例展示了如何在 ViewModel 中转换数据:

Kotlin

class ScheduleViewModel : ViewModel() {
    val userName: LiveData

    init {
        val result = Repository.userName
        userName = Transformations.map(result) { result -> result.value }
    }
}

Java

class ScheduleViewModel extends ViewModel {
    LiveData username;

    public ScheduleViewModel() {
        String result = Repository.userName;
        userName = Transformations.map(result, result -> result.value);
    }
}

使用 ViewModel 管理界面相关数据

数据绑定库可与 ViewModel 组件无缝协作。ViewModel 会公开布局观察到的数据并对其更改做出响应。通过将 ViewModel 组件与数据绑定库结合使用,您可以将界面逻辑从布局中移出,移入组件中,以便于测试。数据绑定库可确保视图在需要时与数据源绑定和解除绑定。其余的大部分工作包括确保公开正确的数据。如需详细了解此架构组件,请参阅 ViewModel 概览

如需将 ViewModel 组件与数据绑定库一起使用,您必须实例化从 ViewModel 类继承的组件,获取绑定类的实例,并将 ViewModel 组件分配给绑定类中的属性。以下示例展示了如何将该组件与库一起使用:

Kotlin

class ViewModelActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Obtain the ViewModel component.
        val userModel: UserModel by viewModels()

        // Inflate view and obtain an instance of the binding class.
        val binding: UserBinding = DataBindingUtil.setContentView(this, R.layout.user)

        // Assign the component to a property in the binding class.
        binding.viewmodel = userModel
    }
}

Java

class ViewModelActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Obtain the ViewModel component.
        UserModel userModel = new ViewModelProvider(this).get(UserModel.class);

        // Inflate view and obtain an instance of the binding class.
        UserBinding binding = DataBindingUtil.setContentView(this, R.layout.user);

        // Assign the component to a property in the binding class.
        binding.viewmodel = userModel;
    }
}

在您的布局中,使用绑定表达式将 ViewModel 组件的属性和方法分配给相应的视图,如以下示例所示:

<CheckBox
    android:id="@+id/rememberMeCheckBox"
    android:checked="@{viewmodel.rememberMe}"
    android:onCheckedChanged="@{() -> viewmodel.rememberMeChanged()}" />

使用 Observable ViewModel 更好地控制绑定适配器

您可以使用实现 Observable 接口的 ViewModel 组件,向其他应用组件通知数据变化,类似于使用 LiveData 对象的方式。

在某些情况下,即使您失去了 LiveData 的生命周期管理功能,您可能也更倾向于使用实现 Observable 接口的 ViewModel 组件,而不是使用 LiveData 对象。通过使用实现 ObservableViewModel 组件,您可以更好地控制应用中的绑定适配器。例如,借助此模式,您可以更好地控制数据更改时的通知;此外,您还可以指定自定义方法来设置双向数据绑定中的属性值。

如需实现可观察的 ViewModel 组件,您必须创建一个继承自 ViewModel 类并实现 Observable 接口的类。您可以使用 addOnPropertyChangedCallback()removeOnPropertyChangedCallback() 方法提供观察者订阅或退订通知时的自定义逻辑。您还可以在 notifyPropertyChanged() 方法中提供属性更改时运行的自定义逻辑。以下代码示例展示了如何实现可观察的 ViewModel

Kotlin

/**
 * A ViewModel that is also an Observable,
 * to be used with the Data Binding Library.
 */
open class ObservableViewModel : ViewModel(), Observable {
    private val callbacks: PropertyChangeRegistry = PropertyChangeRegistry()

    override fun addOnPropertyChangedCallback(
            callback: Observable.OnPropertyChangedCallback) {
        callbacks.add(callback)
    }

    override fun removeOnPropertyChangedCallback(
            callback: Observable.OnPropertyChangedCallback) {
        callbacks.remove(callback)
    }

    /**
     * Notifies observers that all properties of this instance have changed.
     */
    fun notifyChange() {
        callbacks.notifyCallbacks(this, 0, null)
    }

    /**
     * Notifies observers that a specific property has changed. The getter for the
     * property that changes must be marked with the @Bindable annotation to
     * generate a field in the BR class to be used as the fieldId parameter.
     *
     * @param fieldId The generated BR id for the Bindable field.
     */
    fun notifyPropertyChanged(fieldId: Int) {
        callbacks.notifyCallbacks(this, fieldId, null)
    }
}

Java

/**
 * A ViewModel that is also an Observable,
 * to be used with the Data Binding Library.
 */
class ObservableViewModel extends ViewModel implements Observable {
    private PropertyChangeRegistry callbacks = new PropertyChangeRegistry();

    @Override
    protected void addOnPropertyChangedCallback(
            Observable.OnPropertyChangedCallback callback) {
        callbacks.add(callback);
    }

    @Override
    protected void removeOnPropertyChangedCallback(
            Observable.OnPropertyChangedCallback callback) {
        callbacks.remove(callback);
    }

    /**
     * Notifies observers that all properties of this instance have changed.
     */
    void notifyChange() {
        callbacks.notifyCallbacks(this, 0, null);
    }

    /**
     * Notifies observers that a specific property has changed. The getter for the
     * property that changes must be marked with the @Bindable annotation to
     * generate a field in the BR class to be used as the fieldId parameter.
     *
     * @param fieldId The generated BR id for the Bindable field.
     */
    void notifyPropertyChanged(int fieldId) {
        callbacks.notifyCallbacks(this, fieldId, null);
    }
}

其他资源

如需详细了解数据绑定,请参阅下面列出的其他资源。