- XML layout file
- Dynamically setting variables
- BindingAdapter
Ready to begin the second installment in the DataBinding series, review the first installment with a primer and a brief introduction to DataBindig for some development scenarios. By the end of this chapter you will be able to deal with most of the scenarios in development.
XML layout file
Resources in the RES folder can be used in XML, and can be used in binding expressions in almost any general way, such as:
<TextView
android:text="@ {@ string/app_name}" />
Copy the code
Some, however, require display (different from regular reference) calls:
1.1. Use of String
There are several ways to use strings in expressions:
- Use literals directly
- Use string resources, (Format effect)
- Using string resources, (rich text effect)
There are two ways to use literals directly in expressions:
Android: text = "@ {` hello `}" / / ` this symbol above the Tab key to android: test = '@ {} "hello"' / / this is the single quotation marksCopy the code
When we use converters in string resources, we get the same effect as format when we use expressions:
< string name = "test" > hello % s < / string > -- -- -- -- -- -- -- -- -- -- -- -- -- segmentation android: text = "@ {@ string/test (` wrold `)}" / / hello wroldCopy the code
Use the same format as String.
Finally, implement some rich text operations, such as displaying different font colors:
<string name="test">hello<Data><! [CDATA [< font color = "# F92F2F" > wrold < / font >]] > < / Data > < / string > -- -- -- -- -- -- -- -- -- -- -- -- -- segmentation Android :text= "@{html.fromhtml (@string/test)}" //wrold will display redCopy the code
The HTML used here needs to be imported with the
tag.
1.2. Operators in XML
There are a number of operators that can be used in expressions, plus the above to conclude:
- Arithmetic operator
+ - / * %
- String concatenation operator
+
- Logical operator
&& | |
- Binary operator
& | ^
- Unary operator
+ -! ~
- Shift operator
>> >>> <<
- Comparison operator
== > < >= <=
(Note that < needs to be escaped to <) - instanceof
- Grouping operator ()
- Literal operators – characters, strings, numbers, NULL
- Type conversion
- The method call
- Field access
- Array access
[]
- Ternary operator
? :
- Null merge operator
??
Note that the Null merge operator is a binary operator:
Android: text = "@ {demo?? 'def'} 'is equivalent to android:text= "@{demo == null? Def: demo}"Copy the code
1.3. Hidden resources in XML
You can make
equivalent to the Java/Kotlin imort keyword
The import keyword is known to be used to import packages in both Java and Kotlin, and one package that can be used without an import is the Long package. The same is true for the
tag.
So in DataBinding’s XML layout, you can feel free to use static methods of all classes of the Long package, such as String.format, math.abs, and so on. So what are the total hidden things in XML?
- Context
- Java.long.*
- Controls in a layout
How do I use views in a layout?You can see that the Viewpager object has been obtained by referring directly to the name of the ID (if the name is underlined, it needs to be converted to camel form).
1.4, tags,
DataBinding is also available in the include tag:
<include
item="@{item}"
android:id="@+id/stockDetail"
layout="@layout/view_search_stock_item_detail"/>
Copy the code
view_search_stock_item_detail.xml
<data>
<!——include的属性名与variable的name一致——>
<variable
name="item"
type="com.jiucaigongshe.model.Stock" />
</data>
Copy the code
Data binding does not support include as a direct child of the Merge element
2. Dynamically set variables
Sometimes, a particular binding class is not known, but you still need to set a particular value (the variable corresponding to the BR value) to the binding class, which requires the setVariable method to set the variable dynamically. It goes something like this:
With polymorphism, you don’t need to know the specific binding class, you just need to be the ViewDataBinding object, and second, you don’t need to know who the variable is, you can specify the variable through the BR value. The setVariable method is used to set different types of variables for different binding classes, as long as the name of the variable is the same.
Take a simple example without an application scenario:
When a mutable or observable changes, the binding is scheduled to change before the next frame. But sometimes the binding must be performed immediately. You need to use executePendingBindings().
valBindingA =... ; Val bindingB =... ; BindData (bindingA, "data")12)
Copy the code
It has application scenarios, such as RecyclerView and DataBinding.
But when you define the method, you’ll notice that when I don’t have the layout, the BR value doesn’t exist, so data gets an error.
At this point we can pass@Bindable
Generate a BR value:
3. BindingAdapter
The BindingAdapter is the most important annotation in dataBinding. This annotation creates properties that can use binding expressions. This annotation takes two parameters:
The first is a string variable parameter, which represents a list of attribute names. The second parameter, requireAll, indicates whether the preceding attributes must exist at the same time (the default is true)
For Java, this tag works only on static methods, whereas for Kotlin, it can be either static or top-level.
Here’s an example:
@bindingAdapter (" app:deleteLine ", "app:underLine", requireAll = false)
fun void setDelAndUnderLine(view:TextView,delLine:Boolean? ,undLine:Boolean?).{
// This method is used to set underscores and strikeouts
//code..
}
Copy the code
You can see that both the delLine and undLine parameters are nullable because requireAll is false and they do not have to exist together. If requireAll is true, both must be used in the layout file or an error will be reported.
<TextView
app:deleteLine="@ {true}"
app:underLine="@ {true}" />
Copy the code
This is the simple use of a BindingAdapter that allows you to create properties that would otherwise not exist.
Note: Namespaces are not required.
Note again: If you want to use static methods in Kotlin as you do in Java, you need to annotate the methods with @jVMStatic.
summary
DataBinding has regular uses in XML (using literals, resources in resource folders), and so on. There are also resources that are not visible (context, controls in XML layout, etc.)
Finally, the basic usage of BindingAdapter annotations is introduced.
This is the second part of the DataBinding series, and the third part will focus on bidirectional binding and DataBinding annotations. Stay tuned!!