Android Data Binding ——入门
已经介绍了Data Binding的基本使用,包括简单的数据绑定和事件绑定,接下来我们介绍Data Binding的进阶使用。
文中的例子可前往DataBindingDemo查看。
导入
在布局的data
标签内可以声明多个import
标签,import
标签内type
的值是类的全名。布局文件里面可以像Java一样使用import进来的类。
- 导入的类型用于表达式
<data>
<import type="android.view.View"/>
</data>
然后你可以在View里面这样使用:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.lastName}"
android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}" />
不用再在Java里面获取User的值,不用写findViewById,就可以直接根据数据的变化改变View的属性,是不是觉得很方便?
那如果我有两个类的类名相同,只是包名不同呢?或者我想用一个别名来代替类名呢?data binding帮我们想到了,这时候可以用别名alias
:
<import
alias="UserModel" type="com.dragonjiang.databindingdemo.model.User" />
<!--这里使用UserModel代替com.dragonjiang.databindingdemo.model.User-->
<variable name="user" type="UserModel" />
- 使用静态方法或属性
<data>
<import type="com.example.MyStringUtils"/>
<import alias="UserModel" type="com.dragonjiang.databindingdemo.model.User" />
<variable name="user" type="UserModel" />
</data>
…
<TextView
android:text="@{`static method: ` + StringUtil.capitalize(user.lastName)}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
注意:与Java一样,java.lang.*
是自动导入的。
表达式语言
通用的特性
data binding支持的表达式语言有很多与Java表达式一样:
- Mathematical
+ - / * %
- String concatenation
+
- Logical
&& ||
- Binary
& | ^
- Unary
+ - ! ~
- Shift
>> >>> <<
- Comparison
== > < >= <=
instanceof
- Grouping
()
- Literals - character, String, numeric,
null
- Cast
- Method calls
- Field access
- Array access
[]
- Ternary operator
?:
例如:
android:text="@{String.valueOf(index + 1)}"
android:visibility="@{age < 13 ? View.GONE : View.VISIBLE}"
android:transitionName='@{"image_" + id}'
不支持的运算符
一些java的运算符data binding不支持:
this
super
new
- 显式泛型调用 <T>
Null合并运算符
data binding支持null合并运算符(??),在非null时选择左边,null时选择右边:
android:text="@{user.displayName ?? user.lastName}"
等价于:
android:text="@{user.displayName != null ? user.displayName : user.lastName}"
自动避免空指针异常
自动生成的data binding代码会自动检测空指针和避免空指针异常。例如在表达式@{user.name}
中,如果user是null,user.name就会被赋值为null。如果引用的是user.age(int),user.age就会被赋值为0。
集合类
通用的集合类:arrays,lists,sparse lists和maps,可以用[]
操作符来操作。
<data>
<import type="java.util.List" />
<import type="android.util.SparseArray" />
<import type="java.util.Map" />
<import
alias="UserModel"
type="com.dragonjiang.databindingdemo.model.User" />
<variable
name="user"
type="UserModel" />
<variable
name="userList"
type="List<UserModel>" />
<variable
name="sparse"
type="SparseArray<String>" />
<variable
name="map"
type="Map<String, String>" />
<variable
name="key"
type="String" />
<variable
name="index"
type="int" />
</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="@{`userList: ` + userList[index].toString()}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{`sparse: ` + sparse[index]}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!--可以在""里面用``连接字符串,也可以用''-->
android:text="@{`map: ` + map[key] + ' ' +map[`key2`]}" />
</LinearLayout>
注意List<UserModel>
的<
和>
要进行转义:List<UserModel>
资源
可以在表达式里面使用android资源:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!--字符串格式化-->
android:text="@{`stringFormat: ` + @string/nameFormat(user.firstName, user.lastName)}"
android:textSize="@{large ? @dimen/large_text : @dimen/small_text}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!--使用复数-->
android:text="@{`plural: `+@plurals/orange(index)}" />
在string.xml里面:
<resources>
<string name="app_name">DataBindingDemo</string>
<string name="nameFormat">fullname: %1$s %2$s</string>
<plurals name="orange">
<item quantity="one">Have an orange</item>
<item quantity="other">Have %d oranges</item>
</plurals>
</resources>
对于android:text="@{`plural: `+@plurals/orange(index)}" />
,根据index的值,可以格式化为:plural: Have an orange
或者plural: Have %d oranges
。
在dimens.xml里面:
<resources>
<dimen name="large_text">20sp</dimen>
<dimen name="small_text">12sp</dimen>
</resources>
Includes
在布局中可能用到include
标签,variable
可以传递到布局中使用的的任何include
布局中:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<import
alias="UserModel"
type="com.dragonjiang.databindingdemo.model.User" />
<variable
name="user"
type="UserModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/include"
bind:user="@{user}" />
</LinearLayout>
</layout>
在include.xml中:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="user"
type="com.dragonjiang.databindingdemo.model.User" />
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvInclude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{`include layout: ` + user.toString()}"
android:textColor="@color/colorAccent" />
</LinearLayout>
</layout>
对比可以发现,在两个布局中都需要声明user
variable。
Data binding不支持包含merge
标签的布局,举个不能运行的例子:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable name="user" type="com.example.User"/>
</data>
<!--data binding 不支持merge标签-->
<merge>
<include layout="@layout/name"
bind:user="@{user}"/>
<include layout="@layout/contact"
bind:user="@{user}"/>
</merge>
</layout>
至此,Android Data Binding的进阶就介绍完了。我们将在下一篇文章介绍data binding的数据绑定:
Android Data Binding——高级