android 数据绑定(2)绑定表达式
1.官方文档
https://developer.android.com/topic/libraries/data-binding/expressions.html
2.绑定表达式的约束
2.1 允许的运算符
| 符号运算符 | + - | |
| 括号运算符 | () | |
| 成员访问运算符 | obj.成员 | array[indx] |
| 类型运算符 | instanceof | 类型强转运算符,如(int)data.pi |
| 算数运算符 | + - / * % | 包括字符串的+ ,如 '@{"image_" + id}' |
| 位运算符 | & | ^ ! ~ | >> >>> << |
| 关系运算符 | == > < >= <= | 其中的< 要用html转移字符< |
| 逻辑运算符 |
|
|
| 三目运算符 |
|
|
| 非空选择运算符 |
|
??左边非空就返回左边,否则返回右边,如 |
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<String>"/>
<variable name="sparse" type="SparseArray<String>"/>
<variable name="map" type="Map<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<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)绑定表达式的更多相关文章
- android 数据绑定(1)Ativity、Fragment、Item绑定数据源
1.简介 官方文档: https://developer.android.com/topic/libraries/data-binding 官方示例: https://github.com/andr ...
- Android数据绑定DataBinding(二)入门篇
前言 之前写了Android数据绑定DataBinding(一)入门篇,很简单的记录了如何使用DataBinding,其初衷是想要代码中的数据发生改变,不需要繁琐的setText等操作,在最后说到了只 ...
- Android数据绑定技术一,企业级开发
PS:数据绑定,顾名思义是数据与一些控件或者用户账号等绑定,这样用的好处是便于管理.代码清晰,量少. 首先要了解什么是数据绑定? 为什么要用数据绑定? 怎么用数据绑定? 语法的使用 简单例子,数据绑定 ...
- Android应用程序绑定服务(bindService)的过程源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6745181 Android应用程序组件Serv ...
- Android应用程序绑定服务(bindService)的过程源码分析
Android应用程序组件Service与Activity一样,既能够在新的进程中启动,也能够在应用程序进程内部启动:前面我们已经分析了在新的进程中启动Service的过程,本文将要介绍在应用程序内部 ...
- salesforce lightning零基础学习(三) 表达式的!(绑定表达式)与 #(非绑定表达式)
在salesforce的classic中,我们使用{!expresion}在前台页面展示信息,在lightning中,上一篇我们也提及了,如果展示attribute的值,可以使用{!v.expresi ...
- 2018-12-22-WPF-在绑定表达式添加计算
title author date CreateTime categories WPF 在绑定表达式添加计算 lindexi 2018-12-22 16:12:56 +0800 2018-12-22 ...
- Android中使用lambda表达式
lambda 语法简介 视频为本篇播客知识点讲解,建议采用超清模式观看, 欢迎点击订阅我的优酷 如果刚学Android,不知道怎么写点击事件可以跳转,传送门 要想在Android中使用lambda语法 ...
- android 数据绑定(6)自定义绑定方法、双向数据绑定
1.官方文档 https://developer.android.com/topic/libraries/data-binding/binding-adapters https://developer ...
随机推荐
- 【期望DP】[poj2096]Collecting Bugs
偷一波翻译: 工程师可以花费一天去找出一个漏洞——这个漏洞可以是以前出现过的种类,也可能是未曾出现过的种类,同时,这个漏洞出现在每个系统的概率相同.要求得出找到n种漏洞,并且在每个系统中均发现漏洞的期 ...
- LOJ#6075. 「2017 山东一轮集训 Day6」重建
题目描述: 给定一个 n个点m 条边的带权无向连通图 ,以及一个大小为k 的关键点集合S .有个人要从点s走到点t,现在可以对所有边加上一个非负整数a,问最大的a,使得加上a后,满足:s到t的最短路长 ...
- MyBatis与Hibernate
Mybatis和hibernate不同,它不完全是一个ORM框架,因为MyBatis需要程序员自己编写Sql语句.mybatis可以通过XML或注解方式灵活配置要运行的sql语句,并将java对象和s ...
- DMZ在虚拟化环境中的部署
常见的方法有三种: 1.分别部署 2.部分虚拟化 3.全部虚拟化 传统DMZ部署结构: 分别部署: 想要保持DMZ区域物理隔离采用这种方法,每个区域分别部署进入不同的服务器集群,区域之间的连接采用物理 ...
- Java 生成pdf表格文档
最近在工作做一个泰国的项目,应供应商要求,需要将每天的交易生成pdf格式的报表上传到供应商的服务器,特此记录实现方法.废话不多说,直接上代码: THSarabunNew.ttf该文件是泰国字体自行网上 ...
- 【JZOJ3238】【BZOJ3482】超空间旅行
description 在遥远的未来,行星之间的食品运输将依靠单向的贸易路线.每条路径直接连接两个行星,且其运输时间是已知的. 贸易商协会打算利用一项最近发现的新技术--超空间旅行,以增加一些新的航线 ...
- C++Builder 常用String
关于AnsiSting的使用大全(1) arrow: Ansistring 转 char 代码: void __fastcall TForm1::Button1Click(TObject *Sende ...
- 深入浅出 Java Concurrency (38): 并发总结 part 2 常见的并发场景[转]
常见的并发场景 线程池 并发最常见用于线程池,显然使用线程池可以有效的提高吞吐量. 最常见.比较复杂一个场景是Web容器的线程池.Web容器使用线程池同步或者异步处理HTTP请求,同时这也可以有效的复 ...
- Android之GridLayout网格布局
1.相关属性 GridLayout网格布局是4.0之后引入的布局方式! android:columnCount="4" //设置列数(4列) android:rowCount=&q ...
- C#控件的闪烁问题解决方法总结
最近对代码作了一些优化,试验后效果还可以,但是发现界面会闪烁,具体是TreeView控件会闪烁,语言为C#,IDE为VS2005.在查阅一些资料,使用了一些基本技术后(如开启双缓冲),发现没什么效果. ...