上篇我们知道了Data Binding的最简单的用法,那么Data Binding其中最为重要也是最复杂的其实就是在xml布局文件中给对应的控件进行数据绑定了,接下来就一一说明Data Binding的使用各个场景的语法。

我们以User类这个Model为例:

public class User {
    private String userName;
    private String userPassword;
    private boolean isExist;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public boolean isExist() {
        return isExist;
    }

    public void setIsExist(boolean isExist) {
        this.isExist = isExist;
    }

    public User(String userName, String userPassword, boolean isExist) {
        this.userName = userName;
        this.userPassword = userPassword;
        this.isExist = isExist;
    }
}

Imports

就像Java代码一样,我们可以使用import导入我们在xml文件绑定数据时需要使用到的类,如我们需要使用到系统中的View类,可以这样:

<data>
    <import type="android.view.View"/>
</data>

然后使用它对控件进行相应属性值的绑定,如:

<TextView
   android:text="@{user.userName}"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:visibility="@{user.isExist? View.VISIBLE : View.GONE}"/>

这样我们就可以使用View类的一些值,通过@{}来绑定这些值,用三元表达式进而决定控件的可见性。

class name conflicts类名冲突

如果我们自己创建了一个类名也为View,如:

public class View {
    private int width;
    private int height;
    /*
    * getter and setter ......
    * */
}

很明显这和系统中的View类名一样,那么在xml中怎么区别这两个不同的类呢?就是通过设置别名来区分。如:

<data>
    <import type="android.view.View"/>
    <import type="com.sunzxyong.databinding.View"
        alias="MyView"/>
</data>

我设置了我自己定义的View别名为MyView,那么对控件进行数据绑定时候就使用这个别名,如:

<TextView
   android:text="@{MyView.width}"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:visibility="@{user.isExist ? View.VISIBLE : View.GONE}"/>

List集合的使用

假如我们有多位用户,我们需要显示第1位用户的数据,那么可以这样:

<data>
    <import type="com.sunzxyong.databinding.User"/>
    <import type="java.util.List"/>
    <variable name="userList" type="List&lt;User>"/>
</data>

这里我们定义了一个List<User> 这样的一个集合,名为userList,其中特别注意一点,在@{}表达式中不支持左括号,所以我们需要用转义字符代替左括号,然后在控件中获取集合的数据是通过userList[0]来获取的,这点和数组一样,而我们定义的是List<User>集合,所以最终获取第一位用户的用户名是这样的:userList[0].userName,然后绑定数据在控件上,表示显示第一位用户的数据代码如下:

<TextView
   android:text="@{userList[0].userName}"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
/>

这里打印出来的将是集合中第一个元素的userName。

如果index索引需要动态改变,那么就这样:

<data>
    <import type="java.util.List"/>
    <variable name="list" type="List&lt;String>"/>
    <variable name="index" type="int"/>
</data>
…
android:text="@{list[index]}"

Variables变量

【注意一点】java.lang.*包下的类是不需要导入的,因为这和java一样lang包下的类都是自动导入进去了,如:

<data>
    <variable
            name="number"
            type="String"/>
</data>

绑定数据:

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{number}" />

然后通过代码设置mBinding.setNumber(“888888”);那么屏幕上显示将是888888。

因为自动导入了lang包,可以直接使用String、int、boolean等类,这些变量在没有传递值的情况下和java一样,都有默认值,引用类的为null,int为0,boolean为false等。

自定义Binding Class Name,也就是自定义ViewModel类名,不是用系统默认根据xml文件名生成的

我们知道系统默认是根据xml布局文件名来生成Binding Class Name的,如:first_activity.xml,那么生成的Binding Class Name就是FirstActivityBinding,如果该app module的包名为com.sunzxyong.hello,那么生成的Bindind Class Name所处的依赖的包为com.sunzxyong.hello.databinding,在使用时候AS会自动导入该包,如:

import com.sunzxyong.hello.databinding.FirstActivityBinding

那么怎么自定义Binding Class 名呢?就是通过<data class=”“>设置<data>节点中class的名字就是我们自定义的Binding Class 名字,比如我们把Binding Class 名改为MyBinding,则:

<data class="MyBinding">
...
</data>

则该xml的Binding Class Name为MyBinding,代码中获取就变为这样了:

MyBinding mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

而它的默认依赖包名也是在module包下的databinding包中。

当然也可以自己定义Binding Class 的包名:

<data class="com.example.MyBinding">
    ...
</data>

当布局中包含另外一个子布局时,使用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.sunzxyong.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>

该布局包含了两个另外的子布局分别为name.xml和contact.xml,那么如果想要这两个子布局也共用一个User数据,那么需要在include节点中添加:

bind:user=”@{user}”

然后添加命名空间:

xmlns:bind=”http://schemas.android.com/apk/res-auto”

然后name.xml和contact布局中也需要定义数据绑定:

<?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.sunzxyong.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.userName}" />
   </LinearLayout>
</layout>

【注意】:官方文档上说the following layout is not supported <merge>标签

<?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.sunzxyong.User"/>
   </data>
   <merge>
       <include layout="@layout/name"
           bind:user="@{user}"/>
       <include layout="@layout/contact"
           bind:user="@{user}"/>
   </merge>
</layout>

这是不支持的。


@{}表达式支持的运算

基本上和java的一样:

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:text="@{String.valueOf(index + 1)}"
android:visibility="@{age &lt; 13 ? View.GONE : View.VISIBLE}"
android:transitionName='@{"image_" + id}'

不支持的语法:

this

super

new

Null Coalescing 操作符

如果firstName为null,则选择laseName,否则选择firstName:

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

它等于:

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

使用Map<Key,Value>集合

<data>
    <import type="java.util.Map"/>
    <variable name="map" type="Map&lt;String, String>"/>
    <variable name="key" type="String"/>
</data>
…
android:text="@{map[key]}"

可以看到map的数据获取也是通过map[]中括号的形式,只不过这里传入的是对应的Key,而List则是传入对应的int类型的索引。

Resources资源的访问

android:padding="@{large? @dimen/largePadding : @dimen/smallPadding}"
android:text="@{@string/nameFormat(firstName, lastName)}"
android:text="@{@plurals/banana(bananaCount)}"

可以看到和平常的访问是一样的,也是通过@访问。

好了,Data Binding的语法的使用就讲完了!下一篇将继续叙述高级的用法。。。

Android Data Binding语法解析(二)的更多相关文章

  1. Android Data Binding代码实践(告别findViewById)(四)

    Data Binding实战(一) Data Binding语法解析(二) Data Binding高级用法(三) 好了,继前三篇学习了Data Binding之后,我们可以发现它的强大之处有这么几点 ...

  2. Android Data Binding Library

    Data Binding Library Data Binding Library是一个支持库,允许您使用声明格式(而不是编程)将布局中的UI组件与应用程序中的数据源绑定. 布局通常在调用UI框架方法 ...

  3. android data binding jetpack VII @BindingAdapter

    android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...

  4. android data binding jetpack I 环境配置 model-view 简单绑定

    android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...

  5. Android Data Binding实战(一)

    在今年Google I/O大会上,Google推出Design Library库的同时也推出了Android Data Binding,那么什么是Data Binding?其名曰数据绑定,使用它我们可 ...

  6. Android Data Binding

    Android官方数据绑定框架DataBinding, 1.什么是DataBinding 2.DataBinding基础用法 3.DataBinding原理 4.表达式 5.null检查 6.incl ...

  7. Android Data Binding(数据绑定)用户指南

    Android Data Binding(数据绑定)用户指南 http://www.jianshu.com/p/b1df61a4df77 https://github.com/LyndonChin/M ...

  8. android data binding jetpack VIII BindingConversion

    android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...

  9. android data binding jetpack VI 清理一些概念。BR 运算表达式

    android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...

随机推荐

  1. Android Multimedia框架总结(八)Stagefright框架之AwesomePlayer及数据解析器

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼:http://blog.csdn.net/hejjunlin/article/details/52503057 前言:前面一篇分析了medi ...

  2. 4-sum问题

    给定一个整数数组,判断能否从中找出4个数a.b.c.d,使得他们的和为0,如果能,请找出所有满足和为0个4个数对. #define SIZE 10 void judgeAndPut(int* arr, ...

  3. MacOS的菜单状态栏App添加饼型进度

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/52075418 ...

  4. UNIX网络编程——设置套接字超时

    在涉及套接字的I/O操作上设置超时的方法有以下3种: 调用alarm,它在指定超时期时产生SIGALRM信号.这个方法涉及信号处理,而信号处理在不同的实现上存在差异,而且可能干扰进程中现有的alarm ...

  5. printk的用法

    printk的用法 内核通过 printk() 输出的信息具有日志级别,日志级别是通过在 printk() 输出的字符串前加一个带尖括号的整数来控制的,如 printk("<6> ...

  6. 网站开发进阶(四十三)html中,路径前加“/” 与不加“/”的区别

    网站开发进阶(四十三)html中,路径前加"/" 与不加"/"的区别 前言 <script src="js/downloadify.js&quo ...

  7. [Python]django使用多进程连接msyql错误

    问题 mysql 查询出现错误 error: (2014, "Commands out of sync; you can't run this command now") 查询 m ...

  8. SpriteBuilder中使用Node类型的ccb动画节点删除时崩溃的问题

    因为节点需要呈现动画效果,虽然只有两个不同帧. 在SpriteBuilder中新建Bullet.ccb文件,类型为node. 添加如上2张图片,并制作动画效果帧. 在游戏中子弹遇到障碍物会被删除,时机 ...

  9. springMVC参数的传递方式(1.通过@PathVariabl获取路径参数,2.@ModelAttribute获取数据,3.HttpServletRequest取参,4@RequestParam)

     通过@PathVariabl注解获取路径中传递参数 JAVA      @RequestMapping(value = "/{id}/{str}")      public  ...

  10. Java由先序序列和中序序列还原二叉树

    还原本来的二叉树并不是一个非常简单的事,虽然思想比较简单,但过程却是比较繁琐.下面我拿先序序列和中序序列来讲一下原理吧. 从先序序列中我们一下子就可以得到二叉树的根节点是第一个元素,然后再中序序列中我 ...