运算符


一、Android DataBinding 基础使用
二、Android DataBinding单向绑定
三、Android DataBinding 双向数据绑定、事件绑定、使用类方法
四、Android DataBinding 运算符、BindingAdapter、 BindingConversion


基础运算符

DataBinding 支持在布局文件中使用以下运算符、表达式和关键字

  • 算术 + - / * %
  • 字符串合并+
  • 逻辑&& ||
  • 二元& | ^
  • 一元 + - ! ~
  • 移位>> >>> <<
  • 比较== > < >= <=
  • Instanceof
  • Grouping ()
  • character, String, numeric, null
  • Cast
  • 方法调用
  • Field 访问
  • Array访问 []
  • 三元?:

目前不支持以下操作

  • this
  • super
  • new
  • 显示泛型调用

此外,DataBinding 还支持以下几种形式的调用:

Null Coalescing

空合并运算符 ?? 会取第一个不为 null 的值作为返回值

<TextView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="@{user.name ?? user.password}" />

等价于

android:text="@{user.name != null ? user.name : user.password}"

属性控制

可以通过变量值来控制 View 的属性

<TextView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="可见性变化"    android:visibility="@{user.male  ? View.VISIBLE : View.GONE}" />

避免空指针异常

DataBinding 也会自动帮助我们避免空指针异常
例如,如果 “@{userInfo.password}”userInfonull 的话,userInfo.password 会被赋值为默认值 null,而不会抛出空指针异常

BindingAdapter

dataBinding 提供了 BindingAdapter 这个注解用于支持自定义属性,或者是修改原有属性。注解值可以是已有的 xml 属性,例如 android:srcandroid:text等,也可以自定义属性然后在 xml 中使用

例如,对于一个 ImageView ,我们希望在某个变量值发生变化时,可以动态改变显示的图片,此时就可以通过 BindingAdapter 来实现

需要先定义一个静态方法,为之添加 BindingAdapter 注解,注解值是为 ImageView 控件自定义的属性名,而该静态方法的两个参数可以这样来理解:当 ImageView 控件的 url 属性值发生变化时,dataBinding 就会将 ImageView 实例以及新的 url 值传递给 loadImage() 方法,从而可以在此动态改变 ImageView 的相关属性

@BindingAdapter({"url"})public static void loadImage(ImageView view, String url) {Log.d(TAG, "loadImage url : " + url);}

在 xml 文件中关联变量值,当中,bind 这个名称可以自定义

<?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"    xmlns:tools="http://schemas.android.com/tools">        <data>        <import type="com.github.ixiaow.databindingsample.model.Image" />        <variable            name="image"            type="Image" />    data>        <android.support.constraint.ConstraintLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <ImageView            android:id="@+id/image"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/ic_launcher_background"            bind:url="@{image.url}" />    android.support.constraint.ConstraintLayout>layout>

BindingAdapter 更为强大的一点是可以覆盖 Android 原先的控件属性。例如,可以设定每一个 Button 的文本都要加上后缀:“-Button”

@BindingAdapter("android:text")public static void setText(Button view, String text) {view.setText(text + "-Button");}
<Button    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:onClick="@{()->handler.onClick(image)}"    android:text='@{"改变图片Url"}'/>

这样,整个工程中使用到了 “android:text” 这个属性的控件,其显示的文本就会多出一个后缀

BindingConversion

dataBinding 还支持对数据进行转换,或者进行类型转换

与 BindingAdapter 类似,以下方法会将布局文件中所有以@{String}方式引用到的String类型变量加上后缀-conversionString

@BindingConversionpublic static String conversionString(String text) {return text + "-conversionString";}

xml 文件

<TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text='@{"xxx"}'android:textAllCaps="false"/>

可以看到,对于 Button 来说,BindingAdapter 和 BindingConversion 同时生效了,而 BindingConversion 的优先级要高些, 此外,BindingConversion 也可以用于转换属性值的类型

看以下布局,此处在向 backgroundtextColor 两个属性赋值时,直接就使用了字符串,按正常情况来说这自然是会报错的,但有了 BindingConversion 后就可以自动将字符串类型的值转为需要的 DrawableColor

<TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background='@{"红色"}'android:padding="20dp"android:text="红色背景蓝色字"android:textColor='@{"蓝色"}'/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:background='@{"蓝色"}'android:padding="20dp"android:text="蓝色背景红色字"android:textColor='@{"红色"}'/>
@BindingConversionpublic static Drawable convertStringToDrawable(String str) {    if (str.equals("红色")) {        return new ColorDrawable(Color.parseColor("#FF4081"));    }    if (str.equals("蓝色")) {        return new ColorDrawable(Color.parseColor("#3F51B5"));    }    return new ColorDrawable(Color.parseColor("#344567"));}@BindingConversionpublic static int convertStringToColor(String str) {    if (str.equals("红色")) {        return Color.parseColor("#FF4081");    }    if (str.equals("蓝色")) {        return Color.parseColor("#3F51B5");    }    return Color.parseColor("#344567");}

更多相关文章

  1. Android 属性动画详解
  2. Android之Bean属性通知类
  3. View的XML 属性大全(官方文档)
  4. Android ImageView 的scaleType属性

随机推荐

  1. mysql 8.0.25 安装配置方法图文教程
  2. mysql定时自动备份数据库的方法步骤
  3. MYSQL数据库基础之Join操作原理
  4. MySQL高可用架构之MHA架构全解
  5. MySQL一些常用高级SQL语句
  6. MySQL系列之十二 备份与恢复
  7. MySQL定时全库备份数据库
  8. MySQL系列之十 MySQL事务隔离实现并发控
  9. MySQL系列之十一 日志记录
  10. MySQL系列之四 SQL语法