Google 在 2015 年公佈了一個 Library 很方便使用,而且還向下支援到 Android 2.2 版,用起來還算簡單,而且還可以精簡程式碼,減少出錯,非常值得推薦。
官網網址:https://developer.android.com/topic/libraries/data-binding/index.html
Data Binding Library 其實蠻強大的,打算寫三篇來解釋如何使用:
- 基本概念
- 進階概念
- 雙向溝通
要講到 Data Binding Library 就必須提到 MVVM 架構,MVVM 是指三大部分:
- Module
- View
- View Module
而這種架構並非是這篇重點,有興趣的朋友請在網路上面搜尋。
環境架構
Data Binding Library 不支援 eclipse ,僅支援 Android Studio 且 Android Studio 版本要 1.3以上,另外 Android Studio Library for Gradle 之版本至少為 1.5.0 alpha-1。
Gradle 設定
要啟動 Data Binding Library 必須在 build.gradle 裡面,找到 android 的設定,加上 dataBinding 的設定
android {
....
dataBinding {
enabled = true
}
}
這樣就是開啟了 DataBinding 的功能。
開始使用
在文章一開始,有提到 MVVM 之架構,我們寫 Data Binding Library 也就要像這種順序來寫。
1.建立 Module
public class User {
private String id;
private String name;
private int age;
}
接著要設定 Getter 和 Setter
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
如果要設定 constructor 也是可以,但是就不限定一定要做,我們這裡會寫,因為可以減少寫 setter 的 code。
public User(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
這樣就是設定好了 module。
2.設定 View
如何設定 View ?其實很簡單就是設定 layout,我們必須使用 layout(注意為小寫)來作為整個 layout 的最外層,並將上一步驟的 module 放在 data 裡面。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="user"
type="com.example.databindingsample.User" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@{user.id}"
android:textColor="#000"
android:layout_margin="3dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="@{user.name}"
android:textColor="#000"
android:layout_margin="3dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@{String.valueOf(user.age)}"
android:textColor="#000"
android:layout_margin="3dp" />
</LinearLayout>
</layout>
3.製作 View Module
製作這部份的 Code ,是目前為止最簡單的動作,我們不必寫任何一行 code ,就可完成,看到這裡,是不是很神奇?只要我們在 android studio 裡面按下 Build -> Build APK ,這就完成了。是不是很簡單?(我相信看倌看到這裡一定一頭霧水,請繼續看下去)
4.開始寫 Code
假設我們在第 2 步驟的 layout 檔案名稱為 activity_main.xml,我們在 MainActivity 裡面的程式碼就像下面:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
setData();
}
}
大家一定會覺得很奇怪 ActivityMainBinding 這是打哪裡來的?其實就是從
第 3 步驟 android studio 幫我們自動建立好的,很神奇吧,這個 databinding class 的命名方式和 layout 的名稱有相關連性,不用說大家也應該看出來了。接著我們來寫 setData function。
private void setData(){
binding.setUser(new User("1", "Marry", 11));
}
接下來執行 APP,理論上看起來應該是一片空白,但是實際上卻是
這樣我們不使用 findIdByView 及 setText 就完成了一個 APP ,有沒有很神奇?只用了短短的兩行 code 就可以把所有的 TextView 都把資料給填上,這樣會不會覺得太精簡了一點?