TheData Binding solves a pain point in Android UI Programming, and the official natively supports MVVM The model allows us to use these new features very easily without changing the existing code framework. Before that, some third-party frameworks (Roboandroid) could support the MVVM model, but the framework was too intrusive to be popular.
1. Prepare:
Android Studio Update to 1.3 or higher
2. Note:
The Data Binding is a support Package
3. Create a new Project:
U modify Project 's build.gradleto add a dependencyto the Build script .
Classpath "Com.android.databinding:databinder:1.0-rc1"
You add a plugin to a module that uses the Data Binding and modify the corresponding build.gradle
Applyplugin: ' com.android.databinding '
Once the project has been created, we use a simple example to illustrate the basic usage of Data Binding.
- Layout file
With Data Binding , The layout file for XML no longer simply shows UI elements. Therefore, its root node is no longer a viewgroup, but instead becomes layout, and a new node data , as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<layout xmlns:android= "Http://schemas.android.com/apk/res/android" >
<data>
<import type= "Com.example.siyan.sample.Person"/>
<variable
Name= "User"
Type= "Person"/>
</data>
<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@{user.name}"
Android:textsize= "20sp"/>
</LinearLayout>
InLayoutinDatanode, declare aVariableachild Nodes,then you canLayoutused in the. Typeproperty can beJavaBean,POJO (is actuallyJavaclass), note:Typeshould be the full path. nameproperty is an alias for the reference to this class. as follows:
<data>
<variable
Name= "User"
Type= "Com.example.siyan.sample.Person"/>
</data>
Of course, you can also use one of the following notation. As shown below:
<data>
<import type= "Com.example.siyan.sample.Person"/>
<variable
Name= "User"
Type= "Person"/>
</data>
The layout uses the expression "@{}" to set the valueof object . As shown below:
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@{user.name}"/>
Android:textsize= "20sp"/>
public class Person {
Public final String name;
Public person (String name) {
THIS.name = name;
}
Public String GetName () {
return name;
}
}
- Binding Data
Replace Setcontentview ()with Databindingutil.setcontentview ( ), and then create a user object, by binding.setuser (user) with the variable The binding is as follows:
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Note that amainbinding is generated based on the layout name.
Amainbinding dataBinding = Databindingutil.setcontentview (this, r.layout.a_main);
Person p = new person ("Preach Intelligence Podcast");
Databinding.setuser (P);
}
The Amainbinding class is automatically generated, and all set methods are generated based on the Name property of the variable node .
At this point, a simple data binding is complete. Run the above program, effect:
Intelligence Podcast Secrets Android 6.0 Data Binding Guide