Android Data Binding实战(一)
在今年Google I/O大会上,Google推出Design Library库的同时也推出了Android Data Binding,那么什么是Data Binding?其名曰数据绑定,使用它我们可以轻松实现MVVM(模型-视图-视图模型)模式,来实现应用之间数据与视图的分离、视图与业务逻辑的分离、数据与业务逻辑的分离,从而达到低耦合、可重用性、易测试性等好处,那么我们首先先来看看什么是MVVM模式。
我自己画了一张图来帮助理解,就不多描述了,毕竟重点是Data Binding的使用
通过上图我们可以很清晰的知道这种架构模式的好处。
那么接下来就是来看看Data Binding的使用:
使用Data Binding开发环境的搭建
目前Data Binding只能在Android Studio 1.3以上版本且gradle版本也必须为1.3以上的,所以没升级的先升级装备吧。
- 环境的搭建
新建一个Project,首先在项目根目录下的build.gradle文件中配置dependencies:
dependencies {
classpath 'com.android.tools.build:gradle:1.3.+'
classpath 'com.android.databinding:dataBinder:1.0-rc1'
}
然后在app module目录下的build.gradle文件中配置:
apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'
第一次使用需要等AS下载,当配置环境成功后会提示Build SUCCESS。
使用Data Binding 写一个简单的MVVM模式的Demo
在刚刚的环境搭建完成后,使用Data Binding前要知道三点:
- 在需要绑定的xml布局中,我们需要用<layout> 作为最外层的布局
- 它是在xml布局文件中来完成数据的绑定的,所以在<layout> 布局中包含一个<data> 区域,在其中设置绑定到xml的数据Model
- 得到数据Model后,采用@{}表达式绑定数据到相应的控件上
所以在这个Demo中,我们有个用户Model类->User,需求就是使用Data Binding将User的用户名和密码通过TextView显示出来。
我们先把xml布局在最外层加上一个<layout>,然后在添加<data> 区域并设置绑定Model,最后使用@{}表达式将数据绑定到TextView控件上,那么我们来看看xml的布局:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="user"
type="com.sunzxyong.databinding.User"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.userName}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.userPassword}" />
</LinearLayout>
</layout>
再看看User类:
public class User {
private String userName;
private String userPassword;
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 User(String userName, String userPassword) {
this.userName = userName;
this.userPassword = userPassword;
}
}
一一讲解一下什么意思,上面我们使用:
<data>
<variable
name="user"
type="com.sunzxyong.databinding.User"/>
</data>
就是将com.sunzxyong.databinding包下User类的一个对象绑定到了布局中,name=“user”就是给User定义一个名为user变量(名字可以随便取),之后通过@{user.userName}获得这个对象的用户名设置给TextView(其中user.userName获取数据的根源就是它是访问user对象中的getUserName()方法来获取数据)。
接下来看看代码,我们在Activity中的onCreate()方法中使用ViewModel将数据传递给View层:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User("Sunzxyong", "12345678");
mBinding.setUser(user);
}
对,你没看错,就是通过这么几行,再也不用findViewById了,就把用户的用户名Sunzxyong和密码12345678绑定到了对应的TextView上显示出来。下面就详细解释下它的工作原理:
刚刚我们在布局文件中的<data>区域定义了一个User类型的user变量,这时候它会默认给user对象生成setUser()和getUser()方法,我们在得到它的ViewModel对象后就可以通过调用setUser()方法来设置数据,那么ViewModel类是什么呢?在默认情况下系统会根据xml布局的名字来命名自动生成ViewModel类,比如刚刚我们的布局名为activity_main,那么它就会以 _ 下划线为分隔点命名结尾再加上Binding,所以这时我们的ViewModel是ActivityMainBinding,我们通过DataBindingUtil.setContentView()来获得对应xml文件的ViewModel,当然还有常用的DataBindingUtil.inflate(),如果绑定ListView或者RecyclerView的Item我们则通过:
ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup, false);
//or
ListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false);
最后我们通过ViewModel把数据传递给View层显示。
我们来运行一下看看:
通过Data Binding我们把数据显示到界面上了,而不用通过findViewById来找到控件然后设置数据。
总结:通过它我们将数据、界面、业务逻辑这三层很好的解耦了,数据层Model得到数据后通过ViewModel对象的setUser()方法设置数据给View层供显示,界面层View获得用户输入的数据后通过ViewModel对象的getUser()方法得到View层的数据进而传递给Model层更新。
这篇我们使用了Data Binding实现了一个简单的小Demo,接下来几篇将介绍Data Binding更高级的用法!
Android Data Binding实战(一)的更多相关文章
- Android Data Binding代码实践(告别findViewById)(四)
Data Binding实战(一) Data Binding语法解析(二) Data Binding高级用法(三) 好了,继前三篇学习了Data Binding之后,我们可以发现它的强大之处有这么几点 ...
- Android Data Binding Library
Data Binding Library Data Binding Library是一个支持库,允许您使用声明格式(而不是编程)将布局中的UI组件与应用程序中的数据源绑定. 布局通常在调用UI框架方法 ...
- Android Data Binding
Android官方数据绑定框架DataBinding, 1.什么是DataBinding 2.DataBinding基础用法 3.DataBinding原理 4.表达式 5.null检查 6.incl ...
- Android Data Binding(数据绑定)用户指南
Android Data Binding(数据绑定)用户指南 http://www.jianshu.com/p/b1df61a4df77 https://github.com/LyndonChin/M ...
- android data binding jetpack VIII BindingConversion
android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...
- android data binding jetpack VII @BindingAdapter
android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...
- android data binding jetpack VI 清理一些概念。BR 运算表达式
android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...
- android data binding jetpack V 实现recyclerview 绑定
android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...
- android data binding jetpack IV 绑定一个方法另一种写法和参数传递
android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...
随机推荐
- Android 的 Sqlite基本操作
在 SQL 数据库中保存数据 使用数据库 将数据保存到数据库对于重复或结构化数据(比如契约信息)而言是理想之选. 本课程假定您基本熟悉 SQL 数据库并且可帮助您开始在 Android 中使用 SQL ...
- java异常体系结构详解
前几天在参加网易和360公司的在线考试的时候,都出了一道关于java中异常类的多项选择题.这几天翻看了相关书籍和网上一些资料,结合自己的理解与思考,将自己的一些收获记录如下: 先来看看java中异常的 ...
- UNIX网络编程——TCP 滑动窗口协议
什么是滑动窗口协议? 一图胜千言,看下面的图.简单解释下,发送和接受方都会维护一个数据帧的序列,这个序列被称作窗口.发送方的窗口大小由接受方确定,目的在于控制发送速度,以免接受方的缓存不够大, ...
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十六)
回到MainScene.m中添加selectRobot方法: -(void)selectRobot:(Robot *)robot{ LevelRestrict *lr = [LevelRestrict ...
- ios swift 实现饼状图进度条,swift环形进度条
ios swift 实现饼状图进度条 // // ProgressControl.swift // L02MyProgressControl // // Created by plter on 7/2 ...
- 小强的HTML5移动开发之路(11)——链接,图片,表格,框架
来自:http://blog.csdn.net/dawanganban/article/details/18098193 一.HTML是什么? HTML(hypertext mark-uplangua ...
- H5学习之旅-H5的格式化(4)
H5的格式设置: b代表是粗体 i斜体 big 字体变大 small变小 em强调 strong 加强和变粗差不多 sub 定义下标字 sup 定义上标字 ins 插入字 del 删除字 代码实例 & ...
- Dynamics CRM2013/2015 禁止欢迎界面(Disable the Welcome Screen)
首次打开Dynamic CRM 2013会出现一个欢迎界面如下图,要想它不出现勾选图中的复选框就行,OK下回再打开就没有了. 但是当我们打开F12开发人员工具,清除域的缓存后再次打开CRM,这个欢迎 ...
- 对嵌入式开发C语言结构体的一点总结
今天冬至居然不上班,公司的良心啊!这回有心情写博客和日志了,好了,废话不多说.直接看下文: 鉴于嵌入式开发过程中,C语言结构体的使用当然是必不可少.话说,基础什么的比你会更牛逼的算法更重要,基础不牢, ...
- 二叉树的最大深度算法面试题-leetcode学习之旅(3)
标题 Maximum Depth of Binary Tree 描述 The maximum depth is the number of nodes along the longest path f ...