DataBinding基本使用包括以下内容:
- 单纯的摆脱findviewbyid
- 绑定基本数据类型及String
- 绑定Model数据
- 绑定事件
- 通过静态方法转换数据类型
- 通过运算符操作数据
- 自定义Binding的类名
- 绑定相同Model的操作
- model变量改变自动更新数据
- 绑定List/Map等集合数据
- Observable自动更新
- Databinding与include标签的结合
- DataBinding与RecyclerView的结合
布局文件
<layout>
<data>
<import type="java.util.ArrayList" />
<import type="java.lang.String" />
<variable
name="list"
type="ArrayList<String>" />
<import type="java.util.Map" />
<variable
name="map"
type="Map<String,String>" />
<variable
name="arrays"
type="String[]" />
data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="@{list[0]}" />
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="@{list.get(1)}" />
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="@{map[`name`]}" />
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="@{map.get(`age`)}" />
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="@{arrays[0]}" />
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="@{arrays[1]}" />
LinearLayout>
layout>
泛型的支持会在编译时期报红线,是可以直接运行的,但是需要通过转义字符才行,如:\<数据类型> 或者\<数据类型\>
ThirdActivity
public class ThirdActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityThirdBinding binding = DataBindingUtil.setContentView(ThirdActivity.this, R.layout.activity_third);
ArrayList list = new ArrayList<>();
list.add("first");
list.add("second");
binding.setList(list);
Map map = new HashMap<>();
map.put("name", "zhangsan");
map.put("age", "40");
binding.setMap(map);
String[] arrays = {"lisi", "laowang"};
binding.setArrays(arrays);
}
}
Observable是一个接口,它的子类有BaseObservable,ObservableField,ObservableBoolean, ObservableByte, ObservableChar, ObservableShort, ObservableInt, ObservableLong, ObservableFloat, ObservableDouble, and ObservableParcelable,ObservableArrayList,ObservableArrayMap
<layout>
<data>
<import type="www.zhang.com.databinding.model.Animal"/>
<variable
name="animal"
type="Animal"/>
<variable
name="list"
type="android.databinding.ObservableArrayList<String>"/>
<variable
name="map"
type="android.databinding.ObservableArrayMap<String,String>"/>
data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="@{animal.field}" />
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="@{String.valueOf(animal.age)}" />
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="@{list[0]}" />
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="@{list[1]}" />
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="@{map[`name`]}" />
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="@{map[`age`]}" />
<Button
android:id="@+id/four_btn"
android:layout_width="match_parent"
android:text="改变数据"
android:layout_height="wrap_content" />
LinearLayout>
layout>
Animal类
public class Animal {
public final ObservableField field = new ObservableField<>();
public final ObservableInt age = new ObservableInt();
}
FourActivity
package www.zhang.com.databinding;
public class FourActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityFourBinding binding = DataBindingUtil.setContentView(FourActivity.this, R.layout.activity_four);
final Animal animal = new Animal();
animal.field.set("cat");
animal.age.set(2);
binding.setAnimal(animal);
final ObservableArrayList list = new ObservableArrayList<>();
list.add("dog");
list.add("mouse");
binding.setList(list);
final ObservableArrayMap map = new ObservableArrayMap<>();
map.put("name","Tom");
map.put("age","4");
binding.setMap(map);
binding.fourBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animal.field.set("dog");
animal.age.set(4);
list.set(0,"cat");
list.set(1,"dog");
map.put("name","Sam");
map.put("age","5");
}
});
}
}
当Animal属性数据改变,list/map集合数据改变,会自动更新数据,我们不需要自己手动设置,省去了一个操作,让我们更专注于业务逻辑。