美文网首页
DataBinding与实体双向绑定(一)

DataBinding与实体双向绑定(一)

作者: 辉涛 | 来源:发表于2022-03-17 23:32 被阅读0次

    前言

    刚开始接触数据双向绑定的时候,是记得可以与实体进行双向绑定的,可能实际业务业务中用的非常少,所以也就没认真去了解相关方面的知识点。也是因为可以使用其它的方法代替它,在今天发现与实体进行双向绑定很方便,又或者我的知识点存在盲区,或者说有更好的办法处理此类相关问题。

    ViewMode驱动的方案

    这个办法,应该是使用最多的,以实际业务举例,比如现在有一个设置个人信息的页面,将xml和ViewModel进行数据双向绑定:
    1,viewModel层:

    /**
     * 作者:Pig Huitao
     * 邮箱:pig.huitao@gmail.com
     * 时间:@date 2022/3/17
     *
     */
    class PersonalViewModel:ViewModel() {
        var major  =  MutableLiveData<String>()
        var school = MutableLiveData<String>()
        
        
        init {
            major.postValue("计算机技术")
            school.postValue("武汉大学")
        }
    }
    

    2 ,xml实现:

    <layout>
        <data>
            <variable
                name="vm"
                type="com.huitao.databindingtest.PersonalViewModel" />
        </data>
        <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".MainActivity">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@{vm.major}" />
    
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@{vm.school}" />
    
        </androidx.appcompat.widget.LinearLayoutCompat>
    </layout>
    

    基本上在实际业务中我都是这么做,如果单单像这两个控件一样绑定,毫无疑问,是没有多大问题,假如现在突然来了20个这样的双向绑定,如果再来点需求数据还需要二次编辑或者从服务端拉取展示,当然我们使用这种也是可行,当数据获取成功后,手动处理,做成这种双向绑定,我想说是真的很繁琐,且容易出错,那有没有简单的方法呢?

    与实体双向绑定

    1,假如我们现在有一个User实体,让它继承BaseObservable:

    
    /**
     * 作者:Pig Huitao
     * 邮箱:pig.huitao@gmail.com
     * 时间:@date 2022/3/17
     *
     */
    class User : BaseObservable() {
        @Bindable
        var name: String? = null
            set(value) {
                field = value
                notifyPropertyChanged(BR.name)
            }
    
        @Bindable
        var school: String? = null
            set(value) {
                field = value
                notifyPropertyChanged(BR.school)
            }
    
        @Bindable
        var major: String? = null
            set(value) {
                field = value
                notifyPropertyChanged(BR.major)
            }
    }
    

    2,ViewModel层:

    
    /**
     * 作者:Pig Huitao
     * 邮箱:pig.huitao@gmail.com
     * 时间:@date 2022/3/17
     *
     */
    class PersonalViewModel : ViewModel() {
        var data = MutableLiveData<User>()
    
        init {
            val user = User()
            user.major = "计算机"
            user.name = "frank"
            user.school = "武汉大学"
            data.postValue(user)
        }
    
    
        fun onChangeMajor() = View.OnClickListener {
            data.value?.major = "土木工程"
        }
    
        fun onChangeSchool() = View.OnClickListener {
            data.value?.school = "深圳大学"
        }
    }
    

    3,xml层:

    <?xml version="1.0" encoding="utf-8"?>
    <layout>
        <data>
            <variable
                name="vm"
                type="com.huitao.databindingtest.PersonalViewModel" />
        </data>
        <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".MainActivity">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="@{vm.onChangeMajor()}"
                android:text="@{vm.data.major}" />
    
    
            <TextView
                android:layout_width="match_parent"
                android:onClick="@{vm.onChangeSchool()}"
                android:layout_height="wrap_content"
                android:text="@{vm.data.school}" />
    
        </androidx.appcompat.widget.LinearLayoutCompat>
    </layout>
    
    

    总结

    从上面的例子,由于数据量小,没法看出优势,在实际问题中,第二种将会是非常方便的,假如我们现在要将修改的数据提交到服务器,我们最终只是操作User实体就好,而不需要一个个像第一种方案逐个转换,总的来说,第二种是非常方便,不容易出错的,也算是工作中遇到问题的一种解决办法。

    相关文章

      网友评论

          本文标题:DataBinding与实体双向绑定(一)

          本文链接:https://www.haomeiwen.com/subject/xiebdrtx.html