DataBinding 简单使用
程序员文章站
2022-05-31 11:52:08
...
使用条件限制
Gradle 版本最低为1.5.0-alpha1
Android Studio 版本最低为1.3
使用步骤
模块下build.gradle
android {
....
dataBinding {
enabled = true
}
}
创建User对象
public class User {
private String firstName;
private String lastName;
private int age;
private String gender;
private String address;
private User(String firstName, String lastName ,int age, String gender ,String address) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age ;
this.gender = gender ;
this.address = address ;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
布局中写法
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="user"
type="com.example.databinding.User"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.firstName}"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.lastName}"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='@{"" +user.age}'/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.gender}"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.address}"/>
</LinearLayout>
</layout>
绑定数据
需要使用DataBindingUtil来绑定布局
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
User user = new User("小", "老虎",18,"男","陕西省西安市");
activityMainBinding.setUser(user);
ActivityMainBinding的由来
将布局 activity_main.xml 转换为Pascal格式并将“Binding”后缀添加到该文件中。所以 activity_main.xml 就生成了ActivityMainBinding 类 。
绑定布局也可以使用下面的方法。
ActivityMainBinding activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
然后创建对象并设置数据,效果就出来了。
此时如果我想修改用户的年龄,该怎么办呢?
使用 user.setAge(100) 吗,我们可以试一下
添加一个按钮
<Button
android:id="@+id/btn_update_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改年龄属性"
/>
findViewById(R.id.btn_update_age).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mUser.setAge(100);
}
});
然后运行程序点击修改按钮,发现年龄并没有发生变化。
这个时候就需要BaseObservable出场了。
public class User extends BaseObservable{
private String firstName;
private String lastName;
private int age;
private String gender;
private String address;
public User(String firstName, String lastName ,int age, String gender ,String address) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age ;
this.gender = gender ;
this.address = address ;
}
@Bindable
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
notifyPropertyChanged(BR.firstName);
}
@Bindable
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
notifyPropertyChanged(BR.lastName);
}
@Bindable
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
notifyPropertyChanged(BR.age);
}
@Bindable
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
notifyPropertyChanged(BR.gender);
}
@Bindable
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
notifyPropertyChanged(BR.address);
}
}
第一步 继承BaseObservable
第二步 get方法使用注解 @Bindable
第三部 set方法中调用notifyPropertyChanged() 方法中还需要填入一个参数,类似于R文件的使用
然后重新运行程序,发现数据修改了之后界面也刷新了数据。双向绑定成功。
是不是有种很麻烦的感觉。当然,下面我们就使用更简便的方法来实现数据双向绑定。
修改User类如下
public class User {
public ObservableField<String> firstName = new ObservableField<>();
public ObservableField<String> lastName = new ObservableField<>();
public ObservableInt age = new ObservableInt();
public ObservableField<String> gender = new ObservableField<>();
public ObservableField<String> address = new ObservableField<>();
public User(String firstName, String lastName ,int age, String gender ,String address) {
this.firstName.set(firstName);
this.lastName.set(lastName);
this.age.set(age);
this.gender.set(gender);
this.address.set(address);
}
}
也不需要继承BaseObservable,使用ObservableField<>
String --> ObservableField<String>
int --> ObservableInt
boolean --> ObservableBoolean
map --> ObservableArrayMap<String, String>
list --> ObservableArrayList<String>
不一样的地方是赋值操作。调用.set(value)方法即可。
然后界面不变,修改年龄的方法改一下
findViewById(R.id.btn_update_age).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mUser.age.set(100);
}
});
运行程序,发现数据双向绑定成功实现。