前言
剛開始接觸數(shù)據(jù)雙向綁定的時候删掀,是記得可以與實體進行雙向綁定的殃姓,可能實際業(yè)務(wù)業(yè)務(wù)中用的非常少袁波,所以也就沒認(rèn)真去了解相關(guān)方面的知識點瓦阐。也是因為可以使用其它的方法代替它铺纽,在今天發(fā)現(xiàn)與實體進行雙向綁定很方便搀暑,又或者我的知識點存在盲區(qū),或者說有更好的辦法處理此類相關(guān)問題控硼。
ViewMode驅(qū)動的方案
這個辦法枷颊,應(yīng)該是使用最多的戳杀,以實際業(yè)務(wù)舉例,比如現(xiàn)在有一個設(shè)置個人信息的頁面夭苗,將xml和ViewModel進行數(shù)據(jù)雙向綁定:
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("計算機技術(shù)")
school.postValue("武漢大學(xué)")
}
}
2 ,xml實現(xiàn):
<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>
基本上在實際業(yè)務(wù)中我都是這么做,如果單單像這兩個控件一樣綁定题造,毫無疑問坐求,是沒有多大問題,假如現(xiàn)在突然來了20個這樣的雙向綁定晌梨,如果再來點需求數(shù)據(jù)還需要二次編輯或者從服務(wù)端拉取展示,當(dāng)然我們使用這種也是可行须妻,當(dāng)數(shù)據(jù)獲取成功后仔蝌,手動處理,做成這種雙向綁定荒吏,我想說是真的很繁瑣敛惊,且容易出錯,那有沒有簡單的方法呢绰更?
與實體雙向綁定
1瞧挤,假如我們現(xiàn)在有一個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 = "武漢大學(xué)"
data.postValue(user)
}
fun onChangeMajor() = View.OnClickListener {
data.value?.major = "土木工程"
}
fun onChangeSchool() = View.OnClickListener {
data.value?.school = "深圳大學(xué)"
}
}
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>
總結(jié)
從上面的例子,由于數(shù)據(jù)量小徐钠,沒法看出優(yōu)勢癌刽,在實際問題中,第二種將會是非常方便的尝丐,假如我們現(xiàn)在要將修改的數(shù)據(jù)提交到服務(wù)器显拜,我們最終只是操作User實體就好,而不需要一個個像第一種方案逐個轉(zhuǎn)換爹袁,總的來說远荠,第二種是非常方便,不容易出錯的失息,也算是工作中遇到問題的一種解決辦法譬淳。