Android Data Binding(More Xml, Less Java)

背景

Google在2015年的I/O大会上发布了三个重要的支持库:

  • Material Design支持库:Android Support Design
  • 百分比布局支持库:Percent support lib
  • 数据绑定支持库:Data Binding Library

Data Binding有什么作用

  • 是官方支持的MVVM模式框架

  • 可以直接在静态布局XML中绑定数据,无需再findViewById,然后再手动设置数据

    Question:是不是RoboGuice、AndroidAnotations...这样的依赖注入框架会慢慢淡出市场

  • 可以提高解析XML的速度

  • UI与功能解耦

Data Binding的需求与支持

需求

  • Android Studio 1.3.0-beta1 或更高版本
  • Gradle plugin 1.3.0以上

支持

  • Android 2.1(API level 7+)以上

如何使用Data Binding

配置

Gradle 插件为1.3的配置

在工程的根build.gradle文件中配置如下:

dependencies {
    classpath 'com.android.tools.build:gradle:1.3.1'
    classpath 'com.android.databinding:dataBinder:1.+'
}

在app的build.gradle文件中配置如下:

apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'

Gradle 插件为1.5.+的配置:

只需要在app的build.gradle文件中如下配置就ok:

android {
    ....
    dataBinding {
        enabled = true
    }
}

使用流程

第一步:创建XML

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.firstName}"/>
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.lastName}"/>
   </LinearLayout>
</layout>

第二步:定义数据

public class User {
   public final String firstName;
   public final String lastName;
   public User(String firstName, String lastName) {
       this.firstName = firstName;
       this.lastName = lastName;
   }
}

第三步:绑定数据

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
   User user = new User("Test", "User");
   binding.setUser(user);

Data Binding使用之详细介绍

使用类方法

<data>
    <import type="com.example.MyStringUtils"/>
    <variable name="user" type="com.example.User"/>
</data>
<TextView
   android:text="@{MyStringUtils.capitalize(user.lastName)}"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>

类型别名

    <data>
        <import type="archmages.github.databindingsamples.model.User"/>
        <import type="archmages.github.databindingsamples.model.real.User" alias="RealUser"/>
        <variable
            name="user"
            type="RealUser"/>
    </data>
    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text='@{user.firstName}'/>

Null Coalescing运算符

空聚合运算操作(??), 如果左边的运算对象不为空的话选择左边,否则选择右边

android:text="@{user.displayName ?? user.lastName}"

这个运算等价于:

android:text="@{user.displayName != null ? user.displayName : user.lastName}"

使用属性值

如下,我们要使用View的Visible属性:

<data>
    <import type="android.view.View"/>
</data>
<TextView
   android:text="@{user.lastName}"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"/>

使用资源数据

Data Binding也是支持直接访问资源数据的

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

带ID的View

Data Binding 有效降低了代码的冗余性,甚至完全没有必要再去获取一个 View 实例,万一我们真的就需要的时候,只要给 View 定义一个 ID,Data Binding 就会为我们生成一个对应的 final 变量。

<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.firstName}"
           android:id="@+id/firstName"/>
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.lastName}"
           android:id="@+id/lastName"/>
   </LinearLayout>
</layout>

Data Binding会自动生成成员变量 :

public final TextView firstName;
public final TextView lastName;

这样我们通过binding对象,可以直接访问这些View, 如此之方便

List View Item

如果我们在ListView中或者在RecyclerView中使用Data Binding, 我们需要在Adapter中这样写(以ListView为例):

@Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()),
                R.layout.user_list_item_view, viewGroup, false);
        binding.setVariable(BR.user, users.get(i));
        binding.executePendingBindings();
        return binding.getRoot();
    }

Includes

有时候我们为了避免layout的xml写得过长,有一些共用的View会抽出来,通过include的方式加入xml中,Data Binding也支持include binding, 我们如何传递binding给include layout呢? 通过Application命名空间 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">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <include layout="@layout/name"
           bind:user="@{user}"/>
       <include layout="@layout/contact"
           bind:user="@{user}"/>
   </LinearLayout>
</layout>

空指针安全

Data Binding自动会检查空对象并且避免空指针异常,例如:在表达式:@{user.name}中,如果user是null, user.name会被指定一个默认值null, 如果我们用@{user.age}, 这个地方age是int型,那么当user为null的时候,user.age默认是0

Collections

常用集合类型,Data Binding都支持:arrays, lists, sparse lists, and maps

<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]}"

[备注]
这边大家注意:在xml中<是特殊字符,当引用List, Map...时,左尖括号需要用转义字符<替换, 同时,引号"要用"替换

表达式

Data Binding支持常用表达式:
  • 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 Databinding还只支持单向绑定,试想未来是否能像Angular.Js一样支持双向绑定呢?
  • 由于目前Android Data Binding还是测试版,buck官方说等Android Databinding升级到正式版本后才考虑对其支持

我谷歌大法一直在与时俱进,每年的I/O大会都会给出重量级的产品,期待上述两个Features的实现,如果真是这样的话,Android不管是开发速度还是编译速度都飞速提高😀

Demo地址,猛戳这里

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容