Android Data Binding

Data Binding官方文檔
Data Binding官方文檔-翻譯
Data Binding官方文檔-實例html

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);
}

以上三步完成了基本數據綁定android

2. 動態更新

當JavaBean對象的數據發生變化時,View自動更新。
正常的數據源只能作到綁定,想要動態更新,就必需要用Observable的方式存儲數據,如下是實現Observable的三種方式。git

2.1 Observable 對象

實現android.databinding.Observable或繼承其實現類github

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類segmentfault

public final ObservableField<String> firstName = new ObservableField<>();

2.3 Observable 集合

ObservableArrayMap<String, Object> mapUser = new ObservableArrayMap<>();
user.put("firstName", "Google");

2.4 引用

不管是對象、字段仍是集合,都是以Observable的方式存儲數據。接下來就是在佈局中引用了。ide

<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&lt;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);

        return holder;
    }

    @Override
    public void onBindViewHolder(final BindingHolder holder, int position) {
        final Model model = mModels.get(position);

        holder.getBinding().setVariable(BR.model, model);
        holder.getBinding().getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        holder.getBinding().executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return mModels.size();
    }

}
相關文章
相關標籤/搜索