JetPack學(xué)習(xí)筆記之DataBinding(一)
布局文件通常只負(fù)責(zé)UI控件的布局工作混狠,頁(yè)面通過(guò)setContentView()方法關(guān)聯(lián)布局文件,然后通過(guò)View的id找到控件柴底,然后才能在代碼中對(duì)View進(jìn)行操作笼踩。可以說(shuō)頁(yè)面(Activity或者Fragment)承擔(dān)了大部分的工作量绑蔫。DataBinding 的出現(xiàn)讓布局文件承擔(dān)了一部分原本屬于頁(yè)面的工作,也使得頁(yè)面和布局文件的耦合度進(jìn)一步降低了泵额。
DataBinding的優(yōu)勢(shì):
- 項(xiàng)目更簡(jiǎn)潔配深,可讀性更高。部分與UI控件相關(guān)的工作可以在布局文件中完成嫁盲。
- 不再需要findViewById()方法篓叶。
- 布局文件可以包含簡(jiǎn)單的業(yè)務(wù)邏輯。View可以直接與數(shù)據(jù)模型中的字段綁定,甚至能夠響應(yīng)用戶的交互缸托。
DataBinding與MVVM架構(gòu)是分不開(kāi)的左敌,實(shí)際上,DataBinding是谷歌為了Android能更好地實(shí)現(xiàn)MVVM架構(gòu)而設(shè)計(jì)的俐镐。
采用DataBinding完成簡(jiǎn)單的數(shù)據(jù)綁定
1矫限、在app的build.gradle文件中啟用數(shù)據(jù)綁定
android {
...
dataBinding {
enabled = true
}
}
2、將布局文件轉(zhuǎn)換為data bind 布局佩抹,具體操作可以參照下圖叼风。
image.png
修改后的布局文件為
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".databinding.DataBindingActivity">
</LinearLayout>
</layout>
可以看到,在布局文件最外層添加了一個(gè)layout標(biāo)簽匹摇,而且將命名空間移到了layout標(biāo)簽上咬扇,data標(biāo)簽稍后會(huì)介紹甲葬。
此時(shí)rebuild項(xiàng)目廊勃,DataBinding庫(kù)會(huì)自動(dòng)生成為綁定布局文件所需要的類(lèi)
image.png
3、建立數(shù)據(jù)對(duì)象
/**
* 項(xiàng)目名稱(chēng) JetPackPro
* 創(chuàng)建人 xiaojinli
* 創(chuàng)建時(shí)間 2020/8/6 9:36 PM
**/
public class Book {
private String name;
private String title;
private String money;
public Book(String name, String title, String money) {
this.name = name;
this.title = title;
this.money = money;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
}
4经窖、在布局文件中引入對(duì)象坡垫,綁定布局變量與數(shù)據(jù)模型中的成員變量
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="book"
type="com.example.jetpackpro.databinding.Book" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".databinding.DataBindingActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{book.name}"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{book.title}"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{book.money}"/>
</LinearLayout>
</layout>
5、 在頁(yè)面中實(shí)例化布局文件
public class DataBindingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_data_binding);
ActivityDataBindingBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_data_binding);
Book book = new Book("這是書(shū)名","這是作者","20元");
binding.setBook(book);
}
}
運(yùn)行后頁(yè)面顯示為:
image.png
6画侣、在布局文件中對(duì)數(shù)據(jù)模型中的字段進(jìn)行簡(jiǎn)單的映射冰悠。
布局文件中View顯示值不只能簡(jiǎn)單的顯示數(shù)據(jù)模型中的值,還可以借助外部類(lèi)進(jìn)行一些處理配乱。比如我們生成一個(gè)處理金錢(qián)的類(lèi)溉卓。
/**
* 項(xiàng)目名稱(chēng) JetPackPro
* 創(chuàng)建人 xiaojinli
* 創(chuàng)建時(shí)間 2020/8/6 9:47 PM
**/
public class MoneyHelp {
public static String formatMoney(String money){
return money + "$";
}
}
在布局文件中引入該類(lèi),并修改顯示金錢(qián)的View修改為:
<data>
...
<import type="com.example.jetpackpro.databinding.MoneyHelp"/>
</data>
...
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{MoneyHelp.formatMoney(book.money)}"/>
將Activity中初始化Book對(duì)象時(shí)的money修改為20搬泥,重新運(yùn)行該項(xiàng)目:
image.png