万众瞩目,期待已久的MVVM,马上就要和大家见面啦。
那么在这之前,首先要介绍的就是DataBinding这一部分,该部分内容将会作为View和ViewModel之间的桥梁,用于连接这两个模块,所以在学习MVVM之前把DataBinding学会了是非常有必要的。
MVVM已经是Android直接支持的框架了,意味着我们可以直接去使用它。不用改啥子结构,就是直接用,就这么酷,( ̄ε(# ̄)☆╰╮( ̄▽ ̄///)收住。
基础使用
引入DataBinding support包
事实上称为引入并不合适,DataBinding已经由Google官方整合完成在Android中了,要使用时只需要在gradle中开启就行了(需要确保gradle在1.5及以上版本)。
//设置DataBinding状态为可用
dataBinding{
enabled = true
}
完成这一配置就能够在项目中正常的使用DataBinding了。官方完成整合后,使用起来非常方便,只需要以上的代码就能直接使用这一功能。
Model
是的,又是它,哪都有它。其实这个世界就是由它构成的(ノ`Д)ノ,好了,不说段子了。上我们的经典栗子,还是熟悉的味道:
class Person{
private String name;
private int age;
private boolean isCool;
public Person(String name, int age, boolean isCool){
this.name = name;
this.age = age;
this.isCool = isCool;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public boolean isCool(){
return isCool;
}
public void setCool(boolean isCool){
this.isCool = isCool;
}
}
布局
使用DataBinding之后,和原先的布局有不小的区别,先贴上一个例子:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>......</data>
<!--原布局内容-->
......
......
<!--原布局内容-->
</layout>
在data节点中使用variable用于View与数据之间的绑定。在新的布局中,控件不再申请id,而是使用@{}来直接绑定数据。
data节点
<data>
<variable name = "person" type = "xxx.xxx.xxx.Person"/>
</data>
<!--以上内容表示,data中存在变量user,类型为xxx.xxx.xxx.User类的实例-->
data节点中除variable外,还能够添加import节点,该import与.java中的用法相同。
<data>
<import type = "xxx.xxx.xxx.Person"/>
<variable name = "person" type = "Person"/>
</data>
绑定variable
<TextView
android:width = "wrap_content"
android:height = "wrap_content"
android:text = "@{person.getName()}" />
通过这一动作就能够通过绑定的person这一variable获取name数据。
绑定数据
在Activity的onCreate()中进行数据绑定,使用DataBindingUtil.setContentView()替换原先的setContentView()。
ActivityBasicBinding binding = DataBindingUtil.setContentView(this, R.layout.xxx);
Person person = new Person("maye", "17", true);
binding.setPerson(person);
进阶使用
工具类导入
import除引入Model外,还能够引入数据绑定过程中需要用到的类,用作辅助。
//java
public class BindUtil {
public static String beCool(String name, boolean isCool){
return isCool ? name + "is Cool" : name + "is not Cool";
}
}
<!--使用工具类处理数据后显示-->
<TextView
android:width = "wrap_content"
android:height = "wrap_content"
android:text = "@{BindUtil.beCool(person.getName(), person.getAge())}" />
引入类别名
<!--import能够对引入类进行重命名-->
<import name = "xxx.xxx.xxx.Example" alias = "Emp" >
??运算符
"根据数据 != null 选择数据结果"
<TextView
android:width = "wrap_content"
android:height = "wrap_content"
android:text="@{person.getName ?? BindUtil.beCool(person.name(), person.isCool)}" />
属性设定
"调用系统本身属性"
<TextView
android:width = "wrap_content"
android:height = "wrap_content"
android:marginTop = "@{ person.isCool ? @string/Cool : @string/Not Cool }" />
存在id值设置
<TextView
android:id = "@+id/name"
android:width = "wrap_content"
android:height = "wrap_content" />
ActivityBaseBinding binding = DataBindingUtil.setContentView(this, R.layout.xxx);
binding.name.setText("TextView文本内容");
单向绑定
之前所涉及到的数据设置都是单次的设置,拿出之前数据绑定的例子来看:
//这一次的数据绑定是单次的设置。person的数据变动还不会动态的影响View的显示。
ActivityBaseBinding binding = DataBindingUtil.setContentView(this, R.layout.xxx);
Person person = new Person("maye", 17, true);
binding.setPerson(person);
需要使用到动态的绑定时,我们就需要提到一个新朋友了。BaseObservable,就是这个酷酷的东西,已经封装在Android内部。需要修改的内容就是之前所编写的Model。
//继承BaseObservable
public class Person extends BaseObservable{
private String name;
private int age;
private boolean isCool;
@Bindable
public String getName(){
return name;
}
@Bindable
public int getAge(){
return age;
}
@Bindable
public boolean isCool(){
return isCool;
}
public void setName(String name){
this.name = name;
notifyPropertyChanged(BR.name);
}
public void setAge(int age){
this.age = age;
notifyPropertyChanged(BR.age);
}
public void setIsCool(boolean isCool){
this.isCool = isCool;
notifyPropertyChanged(BR.isCool);
}
"BR是自动生成的类,在编译过程中完成,@Bindable会将FIELD作为ENTRY添加至BR中"
"notifyPropertyChanged()会在数据改变的同时对View进行修改"
"以上的内容只能在Data发生改变时修改View的显示。而相互修改则需要使用双向绑定"
}
DataBinding目前已经支持双向绑定了,也就是说可以通过修改View的内容而控制改变Data内容。可惜的是,我还没研究透彻。本着认真负责的原则,我会继续研究尽快把双向绑定加上的。本篇内容就到以上了,感谢读到这里。