Data Binding使用技巧
Data Binding
根据变量,自动赋值到各widget。
How
1.编写layout文件,这里的layout为: act_data_bind_demo.xml
这里需要先准备变量
在具体的widget上使用该变量 <TextView android:layoutwidth="matchparent" android:layoutheight="wrapcontent" android:text="@{user.name}"/>
2.Binding data 上面的步骤完成后,需要先编译整个Project,因为在这个过程中会生成一些类:DataBindingUtil,ActDataBindDemoBinding
注意:这里的ActDataBindDemoBinding是根据上面的布局文件的名字生成的。
在onCreate方法中:
ActDataBindDemoBinding binding = DataBindingUtil.setContentView(this,
R.layout.act_data_bind_demo);
customer = new Customer();
customer.setName("Andy");
customer.setMobile("13866668888");
binding.setUser(customer);
运行后,可以看到页面中的两个TextView都有值。
问题:如何获取EditText的输入内容?
failed:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:text="@{user.email}"/>
It's work: "
...android:text="@={user.email}"
问题:如何通过一个按钮改变当前页面中的TextView的值?
1.module类继承BaseObservable
public class Customer extends BaseObservable {
...
public void setMobile(String mobile) {
this.mobile = mobile;
notifyPropertyChanged(BR.mobile);
}
继承BaseObservable,同时在setter方法中调用notifyPropertyChanged。
2.更新Java中的Customer对象,例如当mobile改变,UI会制动刷新。
Custom Binding Class Names
默认情况下,Binding Class是根据layout文件名生成的。(例如,当layout文件为act_data_bind_demo.xml,生成的class为:ActDataBindDemoBinding)
可以自定义类名,以及设定包名:
<data class="com.andy.infrastructure.demos.databinding.DataBind">
<variable ...
</data>
上面的代码中,会在包com.andy.infrastructure.demos.databinding下生成一个名为DataBind的类。注意这个package必须是存在的,当然也可以不指定package(会在默认的包下生成该类)。
Include
在layout文件中使用include时,也可以传递数据到指定的layout中。需要额外2个步骤:
确保传递的变量在两个layout中都有声明
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bind="http://schemas.android.com/apk/res-auto"> ... ... <include layout="@layout/user_name" bind:user="@{user}"/>
被引入的layout: ...
<TextView android:layoutwidth="matchparent" android:layoutheight="wrapcontent" android:padding="15dp" android:text="@{user.name}">
外层layout中传递变量
<include layout="@layout/user_name" bind:user="@{user}"/>
Expression Language
- 数学运算 + - / * %
- 字符串拼接 +
- 逻辑运算符 && ||
- 位运算 & | ^
- Unary + - ! ~
- 位移 >> >>> <<
- 比较运算符 == > < >= <=
- instanceof
- Grouping ()
- Literals character, String, numeric, null
- Cast
- Method calls
- Field access
- Array access []
三元运算符 ? :
Null Coalescing Operator ??
如果??左边不为空就使用左边值,否则就使用右边的值。
android:text="@{user.displayName ?? user.lastName}"
上面的代码相当于:
android:text="@{user.displayName != null ? user.displayName : user.lastName}"
Data Objects
三种数据更新提示机制:
- Observable Object
- Observable Fields
- Observable Collections
Observable Object
这种方式,需要做三个步骤:
- 继承BaseObservable
- 为getter添加@Bindable
- 在setter中添加代码notifyPropertyChanged(BR.name);
Views With IDs
A public final field will be generated for each View with an ID in the layout. The binding does a single pass on the View hierarchy, extracting the Views with IDs. This mechanism can be faster than calling findViewById for several Views.
layout文件中widget有设置id时,可以使用bind对象直接访问该控件,省去findViewById
文本中的代码示例,链接如下:
Data Binding使用技巧的更多相关文章
- Data Binding和INotifyPropertyChanged是如何协调工作的?
前言 WPF的一大基础就是Data Binding.在基于MVVM架构的基础上,只有通过实现INotifyPropertyChanged接口的ViewModel才能够用于Data Binding. 要 ...
- WPF QuickStart系列之数据绑定(Data Binding)
这篇博客将展示WPF DataBinding的内容. 首先看一下WPF Data Binding的概览, Binding Source可以是任意的CLR对象,或者XML文件等,Binding Targ ...
- XAML数据绑定(Data Binding)
XAML数据绑定(Data Binding) Data Binding可以使得XAML标签属性的赋值更为灵活和方便.在绑定过程中,获取数据的标签成为目标标签:提供数据的标签成为源标签.在XAML中 ...
- Optimizing Performance: Data Binding(zz)
Optimizing Performance: Data Binding .NET Framework 4.5 Other Versions Windows Presentation Founda ...
- .NET: WPF Data Binding
WPF是分离UI和Logic的最佳工具,不同于Window Form的事件驱动原理,WPF采用的是数据驱动,让UI成为了Logic的附属,达到分离的效果. 本篇主要讲讲wpf的精华:data bind ...
- WP8.1 Study5:Data binding数据绑定
一.数据绑定 最简单的编程UI控件的方法是写自己的数据来获取和设置控件的属性,e.g. , textBox1.Text = "Hello, world"; 但在复杂的应用程序,这样 ...
- Data Binding in WPF
http://msdn.microsoft.com/en-us/magazine/cc163299.aspx#S1 Data Binding in WPF John Papa Code downl ...
- Data Binding(数据绑定)用户指南
1)介绍 这篇文章介绍了如何使用Data Binding库来写声明的layouts文件,并且用最少的代码来绑定你的app逻辑和layouts文件. Data Binding库不仅灵活而且广泛兼容- 它 ...
- 完全掌握Android Data Binding
转载:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0603/2992.html 来源 https://github.com/L ...
随机推荐
- windows go安装
1.安装git 因为golang是通过git来管理远程包的,所以我们首先要安装git,下载地址:http://www.git-scm.com/download/. git安装比较简单,直接下一步即可( ...
- Scoped CSS规范草案
原文链接:https://github.com/AlloyTeam/AlloyTouch/wiki/Scoped-CSS 写在前面 问:什么是Scoped CSS规范? Scoped CSS规范是We ...
- elasticsearch__5__java操作之FilterBuilders构建过滤器Query
FilterBuilders构建过滤器Query 代码如下: package com.elasticsearch; import org.elasticsearch.action.ActionList ...
- iOS比较常用的第三方及实例(不断更新中)
把平时看到或项目用到的一些插件进行整理,文章后面分享一些不错的实例,若你有其它的插件欢迎分享,不断的进行更新: 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com ...
- MyBatis中jdbcType和javaType的映射关系
JDBC Type Java Type CHAR String VARCHAR String LONGVARCHAR String NUMERIC java.math.BigDecimal DECIM ...
- 1-1 Linux系统安装
虚拟机下配置网络时 rhel7.2安装新建虚拟机内存2G CPU 1核2线 硬盘20G存为单个文件 使用ISO镜像 桥接网卡引导界面: Install Red Hat Enterprise L ...
- html基础起航
废话不多说,直接上例子! 工欲善其事,必先利其器 浏览器要有吧~ 比如:IE.Chrome.Firefox…… 纯文本编辑软件不可少~ 比 ...
- $x^2+y^2=c^2$
$x^2+y^2=c^2$ 每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis. 每一 ...
- [LeetCode] Course Schedule II 课程清单之二
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...