Introduction
DataBinding is a data binding support library launched by Google in Jetpack. It can directly bind the data source of applications in the page components. Make it more convenient to maintain and a clearer and more brief introduction to the architecture.
The only function of DataBinding is also its mission, which is to bind data and all support libraries, essentially to support this function. There are two explanations for the term binding. The first is to bind data to UI elements; the second is to bind data on UI to the corresponding data model, and also supports observation of data and UI changes. If one of them changes, it needs to be synchronized to the other.
Enable databinding
First, set up Databinding and add the following code to the app module:
android {
...
dataBinding {
enabled = true
}
}
Layout xml
The root node must be <layout>, and there can only be one <data> and one direct child View node.
variable (variable tag)
The attribute name of the variable cannot contain _ underscore, otherwise the variable will not be found in the kt file. Sometimes you may need to specify a custom type. You can use the import syntax to import the class, and use alias to set the alias
<data> <import type=""/> <import type="" alias="member"/> <variable name="user" type="User" /> <variable name="member" type="member" /> </data>
When you need to use two classes with the same name but different package names, you can use the alias alias attribute
data (data tag)
It has a property class that can customize the class name and path generated by DataBinding
<data class="ObservableDataBinding"> </data>
@{}expression
Operators
Operation type | Operators |
---|---|
Arithmetic operators | + - / * % |
String concatenation operator | + |
Logical operators | && |
Binary operator | & |
unary operator | + - ! ~ |
Shift operator | >> >>> << |
Triple operator | == > < >= <= |
Grouping operator | () |
Keywords
instanceof
Characters, strings, numbers, null
Type conversion
Method call
Field access
Array access []
Bind normal data
DataBinding can bind normal data objects (not Observable/LiveData)
<data> <import type=""/> <variable name="content" type="String" /> </data>
Bind observable data
Bind observable data means that when the data changes, the UI will change along with it. There are three ways to bind observable data:objects
、fields
andcollections
.
Binding to a single variable - fields
For some data classes, only a few fields are required to support observable, so you can use this method to create observable data
data class User( val name: ObservableField<String>, val likes: ObservableInt )
The basic data type uses the corresponding packaging class directly
Basic data types | Packaging |
---|---|
boolean | ObservableBoolean |
byte | ObservableByte |
char | ObservableChar |
short | ObservableShort |
int | ObservableInt |
long | ObservableLong |
float | ObservableFloat |
double | ObservableDouble |
Reference types are created using the ObservableField class with generic parameters
val name: ObservableField<String>
Binding to collections - collections
<data> <import type=""/> <variable name="user" type="ObservableList<Object>"/> </data> <TextView android:text="@{user[index]}" ... />
You can directly use the [] operator ( list[0] ) to get the elements at the corresponding position
Bind Objects - objects
Data entity class inheritance that needs to be boundBaseObservable
class Person : BaseObservable() { @get:Bindable var country: String = "" set(value) { field = value notifyPropertyChanged() } @get:Bindable var sex: String = "" set(value) { field = value notifyPropertyChanged() } }
You need to add the @get:Bindable annotation on observable data, and then rewrite the set method, and call the notifyPropertyChanged method to indicate the update of the data. BR is automatically generated. The package name is the same as the current package name. The corresponding value will be generated based on the variables annotated by Bindable; you can also call the notifyChange() method to update all data.
Bind LiveData
LiveData also supports data binding
<?xml version="1.0" encoding="utf-8"?> <layout> <data> <variable name="desc" type="<String>" /> </data> ... <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="@{desc}" /> </layout>
We can directly assign LiveData to text and bind the data
val desc = MutableLiveData<String>() = desc
Two-way binding
One-way binding refers to the update of the UI after data changes, while two-way binding refers to the update of any of the changes to the other.
Bidirectional binding is implemented using the @={} expression:
<data> ... <variable name="input" type="<String>" /> </data> ... <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@={input}"/>
This is the end of this article about the detailed explanation of the Android JetPack component support library Databinding. For more related contents of Android JetPack component, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!