1. 基本结构
1.1 JavaBeans对象
- public class User {
- private final String firstName;
- public User(String firstName) {
- this.firstName = firstName;
- }
- public String getFirstName() {
- return this.firstName;
- }
- }
1.2 布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <layout xmlns:android="http://schemas.android.com/apk/res/android">
- <data>
- <variable name="user" type="com.example.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}"/>
- </LinearLayout>
- </layout>
1.3 Activity
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
- User user = new User("Test");
- binding.setUser(user);
- }
以上三步完成了基本数据绑定
2. 动态更新
当JavaBean对象的数据发生变化时,View自动更新。
正常的数据源只能做到绑定,想要动态更新,就必须要用Observable的方式存储数据,以下是实现Observable的三种方式。
2.1 Observable 对象
实现android.databinding.Observable或继承其实现类
- private static class User extends BaseObservable {
- private String firstName;
- @Bindable
- public String getFirstName() {
- return this.firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- notifyPropertyChanged(BR.firstName);
- }
- }
2.2 Observable 字段
基本的数据类型都有相应的Observable类
public final ObservableField<String> firstName = new ObservableField<>();
2.3 Observable 集合
ObservableArrayMap<String, Object> mapUser = new ObservableArrayMap<>();
user.put("firstName", "Google");
2.4 引用
无论是对象、字段还是集合,都是以Observable的方式存储数据。接下来就是在布局中引用了。
- <layout xmlns:android="http://schemas.android.com/apk/res/android">
- <data>
- <import type="android.databinding.ObservableMap" />
- <variable name="user" type="com.example.ObservableUser" />
- <variable name="firstName" type="android.databinding.ObservableField" />
- <variable name="mapUser" type="ObservableMap<String, Object>" />
- </data>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <!-- Observable 对象 -->
- <TextView
- android:text="@{user.firstName}"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <!-- Observable 字段 -->
- <TextView
- android:text="@{firstName.get()}"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <!-- Observable 集合 -->
- <TextView
- android:text="@{mapUser[`firstName`]}"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- </layout>
3. RecyclerView动态绑定
- private static class RecyclerViewAdapter
- extends RecyclerView.Adapter<RecyclerViewAdapter.BindingHolder> {
- private List<Model> mModels;
- public static class BindingHolder extends RecyclerView.ViewHolder {
- private final ViewDataBinding binding;
- public BindingHolder(ViewDataBinding binding) {
- super(binding.getRoot());
- this.binding = binding;
- // 这里可以binding.getRoot().findViewById,然后再onBindViewHolder中绑定事件,目前没有找到更方便的方法
- }
- public ViewDataBinding getBinding() {
- return binding;
- }
- }
- @Override
- public BindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- ViewDataBinding binding = DataBindingUtil.inflate(
- LayoutInflater.from(parent.getContext()),
- R.layout.list_item,
- parent,
- false);
- BindingHolder holder = new BindingHolder(binding);