Data Binding官方文檔
Data Binding官方文檔-翻譯
Data Binding官方文檔-實例html
public class User { private final String firstName; public User(String firstName) { this.firstName = firstName; } public String getFirstName() { return this.firstName; } }
<?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>
@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
當JavaBean對象的數據發生變化時,View自動更新。
正常的數據源只能作到綁定,想要動態更新,就必需要用Observable的方式存儲數據,如下是實現Observable的三種方式。git
實現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); } }
基本的數據類型都有相應的Observable類segmentfault
public final ObservableField<String> firstName = new ObservableField<>();
ObservableArrayMap<String, Object> mapUser = new ObservableArrayMap<>(); user.put("firstName", "Google");
不管是對象、字段仍是集合,都是以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<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>
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(); } }