Android Jetpack中DataBinding将布局视图绑定到架构组件(七)


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

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

本页介绍了如何将架构组件整合到您的应用中,以进一步凸显使用数据绑定库的优势。

使用 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 组件来将数据绑定到布局。在 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 组件分配给绑定类中的属性。以下示例展示了如何将组件与库结合使用:

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 对象的方式类似。

在某些情况下,您可能更愿意使用实现 Observable 接口的 ViewModel 组件,而不是使用 LiveData 对象,即使这样会失去对 LiveData 的生命周期管理功能也不影响。使用实现 Observable 的 ViewModel 组件可让您更好地控制应用中的绑定适配器。例如,这种模式可让您更好地控制数据更改时发出的通知,您还可以指定自定义方法来设置双向数据绑定中的属性值。

如需实现可观察的 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 should 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 should 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);
        }
    } 

猜你喜欢

转载自blog.csdn.net/u014657752/article/details/123153298