Android Data Binding

Android Data Binding

1. Basic structure

1.1 JavaBeans Objects

  1. public   class User {
  2. private   final String firstName;
  3. public User(String firstName) {
  4. this .firstName = firstName;
  5. }
  6. public String getFirstName() {
  7. return   this .firstName;
  8. }
  9. }

1.2 Layout File

  1. <?xml version= "1.0" encoding= "utf-8" ?>
  2. <layout xmlns:android= "http://schemas.android.com/apk/res/android" >
  3. <data>
  4. <variable name= "user" type= "com.example.User" />
  5. </data>
  6. <LinearLayout
  7. android:orientation= "vertical"  
  8. android:layout_width= "match_parent"  
  9. android:layout_height= "match_parent" >
  10. <TextView android:layout_width= "wrap_content"  
  11. android:layout_height= "wrap_content"  
  12. android:text= "@{user.firstName}" />
  13. </LinearLayout>
  14. </layout>

1.3 Activity

  1. @Override  
  2. protected   void onCreate(Bundle savedInstanceState) {
  3. super .onCreate(savedInstanceState);
  4. ActivityMainBinding binding = DataBindingUtil.setContentView( this , R.layout.main_activity);
  5. User user = new User( "Test" );
  6. binding.setUser(user);
  7. }

The above three steps complete the basic data binding

2. Dynamic Updates

When the data of the JavaBean object changes, the View is automatically updated.

Normal data sources can only be bound. If you want to update dynamically, you must use Observable to store data. The following are three ways to implement Observable.

2.1 Observable Object

Implement android.databinding.Observable or inherit its implementation class

  1. private   static   class User extends BaseObservable {
  2. private String firstName;
  3. @Bindable  
  4. public String getFirstName() {
  5. return   this .firstName;
  6. }
  7. public   void setFirstName(String firstName) {
  8. this .firstName = firstName;
  9. notifyPropertyChanged(BR.firstName);
  10. }
  11. }

2.2 Observable Fields

Basic data types have corresponding Observable classes

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

2.3 Observable Collections

ObservableArrayMap<String, Object> mapUser = new ObservableArrayMap<>();

user.put("firstName", "Google");

2.4 References

Whether it is an object, field or collection, data is stored in the form of Observable. The next step is to reference it in the layout.

  1. <layout xmlns:android= "http://schemas.android.com/apk/res/android" >
  2. <data>
  3. < import type= "android.databinding.ObservableMap" />
  4. <variable name= "user" type= "com.example.ObservableUser" />
  5. <variable name= "firstName" type= "android.databinding.ObservableField" />
  6. <variable name= "mapUser" type= "ObservableMap&lt;String, Object>" />
  7. </data>
  8.  
  9. <LinearLayout
  10. android:layout_width= "match_parent"  
  11. android:layout_height= "match_parent" >
  12.  
  13. <!-- Observable object -->
  14. <TextView
  15. android:text= "@{user.firstName}"  
  16. android:layout_width= "wrap_content"  
  17. android:layout_height= "wrap_content" />
  18.  
  19. <!-- Observable fields -->
  20. <TextView
  21. android:text= "@{firstName.get()}"  
  22. android:layout_width= "wrap_content"  
  23. android:layout_height= "wrap_content" />
  24.  
  25. <!-- Observable Collection -->
  26. <TextView
  27. android:text= "@{mapUser[`firstName`]}"  
  28. android:layout_width= "match_parent"  
  29. android:layout_height= "wrap_content" />
  30.  
  31. </LinearLayout>
  32. </layout>

3. RecyclerView dynamic binding

  1. private   static   class RecyclerViewAdapter
  2. extends RecyclerView.Adapter<RecyclerViewAdapter.BindingHolder> {
  3.  
  4. private List<Model> mModels;
  5.  
  6. public   static   class BindingHolder extends RecyclerView.ViewHolder {
  7. private   final ViewDataBinding binding;
  8.  
  9. public BindingHolder(ViewDataBinding binding) {
  10. super (binding.getRoot());
  11. this .binding = binding;
  12. // You can use binding.getRoot().findViewById here, and then bind the event in onBindViewHolder. Currently, no more convenient method has been found.  
  13. }
  14.  
  15. public ViewDataBinding getBinding() {
  16. return binding;
  17. }
  18. }
  19.  
  20. @Override  
  21. public BindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  22. ViewDataBinding binding = DataBindingUtil.inflate(
  23. LayoutInflater.from(parent.getContext()),
  24. R.layout.list_item,
  25. parent,
  26. false );
  27.  
  28. BindingHolder holder = new BindingHolder(binding);

<<:  Interview experience: WeChat, NetEase Games, Jinshan Xishanju, Renren

>>:  Looking for Easter eggs in the code, Google is no longer the Google you know

Recommend

iPhone 6: The pain of mobile phone design revealed

On September 9, the long-awaited iPhone 6 finally...

Get APP growth analysis report

1. Background and Purpose background Recently, I ...

That grill master may be a master of chemistry

Editor’s Note: Barbecue is nature's gift to m...

Analysis of the planning process of "Learn Together" online course activities

Figure 1-Case analysis mind map 1. User Path User...