1.官方文档

  https://developer.android.com/topic/libraries/data-binding/expressions.html

2.绑定表达式的约束

2.1 允许的运算符

符号运算符 + -  
括号运算符 ()  
成员访问运算符 obj.成员 array[indx]
类型运算符 instanceof 类型强转运算符,如(int)data.pi
算数运算符 + - / * % 包括字符串的+ ,如  '@{"image_" + id}'
位运算符 & | ^ ! ~ >>  >>>  <<
关系运算符 == > < >= <= 其中的< 要用html转移字符&lt;
逻辑运算符

&& ||

 
三目运算符

?:

 
非空选择运算符

??

??左边非空就返回左边,否则返回右边,如 @{user.displayName ?? user.lastName}

2.2 不允许的运算符及关键字

  new、super、this、泛型调用

2.3 允许函数调用以及lamda表达式

如:
    android:text="@{String.valueOf(index + 1)}"
    android:onClick="@{() -> presenter.onSaveClick(task)}"

2.4 允许使用集合

声明:

 <data>
<import type="android.util.SparseArray"/>
<import type="java.util.Map"/>
<import type="java.util.List"/>
<variable name="list" type="List&lt;String>"/>
<variable name="sparse" type="SparseArray&lt;String>"/>
<variable name="map" type="Map&lt;String, String>"/>
<variable name="index" type="int"/>
<variable name="key" type="String"/>
</data>

使用:


android:text="@{list[index]}"

android:text="@{sparse[index]}"

android:text="@{map[key]}"

2.5 允许字符常量

两种方式:

单引号内使用双引号

    android:text='@{map["firstName"]}'

或者双引号内使用单引号

    android:text="@{map[`firstName`]}"

2.6 允许使用资源

    android:padding="@{large? @dimen/largePadding : @dimen/smallPadding}"

2.7 import类型

 <data>
<import type="com.example.User"/>
<import type="com.example.MyStringUtils"/>
<import type="java.util.List"/>
<import type="android.view.View"/>
<variable name="user" type="User"/>
<variable name="userList" type="List&lt;User>"/>
</data>

使用:

 <TextView
android:text="@{MyStringUtils.capitalize(user.lastName)}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"/>

2.8 类型别名

 <import type="android.view.View"/>
<import type="com.example.real.estate.View"
alias="Vista"/>

2.9 Include

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable name="data" type="com.example.databind.Data" />
<variable name="click" type="com.example.databind.Click" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="@{click::onStartClicked}"
android:clickable="true"> <include
android:id="@+id/include"
layout="@layout/include"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="@+id/frgmt2"
app:layout_constraintStart_toStartOf="@+id/frgmt2"
app:layout_constraintTop_toBottomOf="@+id/frgmt2"
bind:data="@{data}" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout>

其中

  • xmlns:bind="http://schemas.android.com/apk/res-auto" 声明 bind
  • layout指定的布局是一个完整的数据绑定布局文件。
  • 数据绑定不支持 <merge> 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>
    <variable name="user" type="com.example.User"/>
    </data>
    <merge><!-- Doesn't work -->
    <include layout="@layout/name" bind:user="@{user}"/>
    </merge>
    </layout>

3.绑定事件

3.1 两种方法

方法 指定事件处理函数  绑定事件监听器

参数要求

参数与返回值必须与listener对应的事件函数一致,如View.OnClickListener(View v)

可任意参数,返回值要与对应事件处理函数一致。

listener创建时机

指定的方法不为空时,才创建。

绑定数据对象时就创建,始终存在。

3.2 指定处理函数示例

第一步:定义处理事件的方法,它的参数要与对应的listener一样

 public class Click {
public void onBackGroundClicked(View view){ }
}

第二步:在layout定义指定处理事件的方法

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="data" type="com.example.databind.Data" />
<variable name="click" type="com.example.databind.Click" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="@{click::onStartClicked}"
android:clickable="true">
... </androidx.constraintlayout.widget.ConstraintLayout> </layout>

第三步:关联方法

     private FrgmtMainBinding    binding;

     private void init(View view){
binding = DataBindingUtil.bind(view); MainActivity main = (MainActivity) getActivity(); binding.setData(main.data);
binding.setClick(new Click());
}

3.2 绑定事件监听器示例

监听器代码:

 public class Click {
public void onResetClicked(Data data){
data.key = "time";
data.value = ;
}
}

在layout中绑定:

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="data" type="com.example.databind.Data" />
<variable name="click" type="com.example.databind.Click" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="@{click::onStartClicked}"
android:clickable="true"> <Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:text="reset"
android:onClick="@{() -> click.onResetClicked(data)}"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="@+id/start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/stop"
app:layout_constraintTop_toTopOf="@+id/start"
app:layout_constraintVertical_bias="1.0" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout>

其中:

  第22行的是lamda表达式,也可以把指定参数。

    android:onClick="@{(view) -> click.onResetClicked(data)}"
												

android 数据绑定(2)绑定表达式的更多相关文章

  1. android 数据绑定(1)Ativity、Fragment、Item绑定数据源

    1.简介 官方文档:  https://developer.android.com/topic/libraries/data-binding 官方示例: https://github.com/andr ...

  2. Android数据绑定DataBinding(二)入门篇

    前言 之前写了Android数据绑定DataBinding(一)入门篇,很简单的记录了如何使用DataBinding,其初衷是想要代码中的数据发生改变,不需要繁琐的setText等操作,在最后说到了只 ...

  3. Android数据绑定技术一,企业级开发

    PS:数据绑定,顾名思义是数据与一些控件或者用户账号等绑定,这样用的好处是便于管理.代码清晰,量少. 首先要了解什么是数据绑定? 为什么要用数据绑定? 怎么用数据绑定? 语法的使用 简单例子,数据绑定 ...

  4. Android应用程序绑定服务(bindService)的过程源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6745181 Android应用程序组件Serv ...

  5. Android应用程序绑定服务(bindService)的过程源码分析

    Android应用程序组件Service与Activity一样,既能够在新的进程中启动,也能够在应用程序进程内部启动:前面我们已经分析了在新的进程中启动Service的过程,本文将要介绍在应用程序内部 ...

  6. salesforce lightning零基础学习(三) 表达式的!(绑定表达式)与 #(非绑定表达式)

    在salesforce的classic中,我们使用{!expresion}在前台页面展示信息,在lightning中,上一篇我们也提及了,如果展示attribute的值,可以使用{!v.expresi ...

  7. 2018-12-22-WPF-在绑定表达式添加计算

    title author date CreateTime categories WPF 在绑定表达式添加计算 lindexi 2018-12-22 16:12:56 +0800 2018-12-22 ...

  8. Android中使用lambda表达式

    lambda 语法简介 视频为本篇播客知识点讲解,建议采用超清模式观看, 欢迎点击订阅我的优酷 如果刚学Android,不知道怎么写点击事件可以跳转,传送门 要想在Android中使用lambda语法 ...

  9. android 数据绑定(6)自定义绑定方法、双向数据绑定

    1.官方文档 https://developer.android.com/topic/libraries/data-binding/binding-adapters https://developer ...

随机推荐

  1. WPF drag过程中显示ToolTip.

    原文:WPF drag过程中显示ToolTip. 在drag/drop过程中,我们在判断出over的元素上是否可以接受drag的东西之后,通常是通过鼠标的样式简单告诉用户这个元素不接受现在drag的内 ...

  2. wpf 让正执行的程序暂停几秒钟

    public static class DispatcherHelper     {         [SecurityPermissionAttribute(SecurityAction.Deman ...

  3. 提问(prompt 消息对话框)用于询问一些需要与用户交互的信息。弹出消息对话框(包含一个确定按钮、取消按钮与一个文本输入框)

    提问(prompt 消息对话框) prompt弹出消息对话框,通常用于询问一些需要与用户交互的信息.弹出消息对话框(包含一个确定按钮.取消按钮与一个文本输入框). 语法: prompt(str1, s ...

  4. linux /bin/find 报错:paths must precede expression 及find应用

    1.问题描述,运行下面的命令,清楚日志 [resin@xx ~]$ ssh xxx  "/usr/bin/find /data/logs/`dirname st_qu/stdout.log` ...

  5. c# 中反射里的invoke方法的参数

    一个最简单的C#反射实例,首先编写类库如下: namespace ReflectionTest { public class WriteTest { //带参数的公共方法 public void Wr ...

  6. JAVA-第一课 环境的配置

    首先 我们需要 下载java的开发工具包 jdk  jdk 的下载地址::http://www.oracle.com/technetwork/java/javase/downloads/index.h ...

  7. Linux时间和时区设定

    一.时区设定 由于安装系统时采用了UTC,那么什么是UTC呢,简单的说UTC就是0时区的时间,是国际标准,而中国处于UTC+8时区. 使用tzselect命令,过程如下: 可以看到此环境变量已设置,将 ...

  8. day2-元组、字典、文件操作

    学习内容: 1. 元组操作 2. 字典操作 3. 文件操作 4. 深浅copy 1. 元组操作: 元组和列表非常相似,只不过元组不能在原处修改(它是不可变的),并且通常写成圆括号中的一系列项. # 元 ...

  9. css正則匹配、模糊匹配

    //所有class包含font-red的p标签 p[class~="font-red"] {color: red;} [abc^="def"] 选择 abc 属 ...

  10. 通过Amazon AWS 十分钟搭建私人主机 自由的不要不要的

      首先承认有点标题党了,当时自己搞的时候可不止十分钟,好吧,我承认是坑太多了,所以特意开了一篇博文,就是要准备尝试的和我一样的菜鸟们,可以真正的十分钟搞定.  当然高手可能用不上十分钟. 首先,就是 ...