DataBinding 使用方式

来源:互联网 发布:sql的count 1 编辑:程序博客网 时间:2024/06/07 04:59

就说是自己安逸了两年,这么长时间了,我一直在逃避现实,总觉得有些东西没错,直到今天。。。。就关于android查找组件等的注解时,一直以为findViewById() 是最好的选择,也看过其他相关的注解的东西,观其内部实现原理是发现用的大多数是反射之类的,在组件多的时候难免会在程序运行的时候带来难点,今天看了15年I/O大会推荐的DataBinding 这个与其说是注解,倒不如说是将数据层和View层沟通的一个桥梁,其使用简单,速度和findViewById()相差无几,以至于可以视为相同,当然,组件超多的时候它还是和findViewById()一样存在性能上的问题,下面就说DataBinding的使用方法

1、DataBinding的使用不需要添加任何依赖和jar包,这就是简单的一个地方

在Model的gradle中添加如下代码

/*允许使用dataBinding*/dataBinding{    enabled = true}
2、OK,搞定了,这就将dataBinding添加到项目中了

接下来要做的就是介绍dataBinding的最基本的使用方法

布局发生变化,将以往的布局中的根布局为XXXlayout 改为layout* 添加<data></data> 标签* 使用variable 标签定义 属性 name 和type 在使用的时候直接将其用@{变量名.属性名}* 也可以用import 导入 其用法同上 假如导入两个相同的类 可以使用alias 设置别名* 引用的时候使用@{别名.属性}

a>现在项目中新建一个给text属性赋值的bean类

e.g.: 

public class User {    private String name;    private String age;    public User(String name, String age) {        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }}
b>在Activity中进行初始化

/** * 实体类和布局文件进行绑定 将传统的setContentView() 代替为下面的格式 */ActivityMainBinding viewDataBinding =        DataBindingUtil.setContentView(this, R.layout.activity_main);User databinding = new User("databinding", "1"); //实例化类viewDataBinding.setUser(databinding); //于布局中的定义variable 进行绑定

c>先介绍在布局中text属性上的使用

<layout xmlns:android="http://schemas.android.com/apk/res/android" //注意布局的变化 有之前的XXXLayout变成layout >
    <data>        <variable            name="user" //属性名 也就是下面TextView中使用的            type="com.databinding.bean.User"/> //注意新添加的代码     </data>    <!--data 好比就是View和Model的桥梁-->    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@{user.name}" //注意使用的地方及使用的格式             />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@{user.age}"            />    </LinearLayout></layout>

3 高级使用

、高级使用* 在布局中如果出现集合等集合变量* 定义的方法是* 在<data></data>标签中先用<import 要用到的类(全包名)/>* 再使用* <variable* name "集合变量名"* type="集合全称<类型>"/>* 为集合定义相应的索引* <variable* name="集合变量名Key"* type="类型"* />

4、事件绑定

、事件的绑定* 在xml中引入android.view.View.OnClickListener** @{定义的事件名称} 代码中 holder.binding.setClickListener(new View.OnClickListener(* private void onClick(View v){* //do something* }* ));

5、

在xml 中支持表达式* 比如:* <TextView* android:layout_width="wrap_content"* android:layout_height="wrap_content"* android:text='@{error ? "error" : "ok"}'/>* <p>* 支持的表达式:* Mathematical + - / * %* String concatenation +* Logical && ||* Binary & | ^* Unary + - ! ~* Shift >> >>> <<* Comparison == > < >= <=* instanceof* Grouping ()* Literals - character, String, numeric, null* Cast* Method calls* Field access* Array access []* Ternary operator ?:* <p>* 不支持的表达式:* this* super* new* Explicit generic invocation

以上就是dataBinding 的较为简单的使用方法,以后在项目中逐渐使用,将会替代传统的写法,使代码更加易于重构,参考http://www.jianshu.com/p/2d3227d9707d 感谢
























原创粉丝点击