Google I/O大会 DataBinding数据绑定 MVVM设计模式
来源:互联网 发布:apache beam 快速入门 编辑:程序博客网 时间:2024/05/22 18:56
DataBinding 目前处于测试阶段,有Bug。环境要求:AndroidStudio 1.3以上版本;Gradle 1.3.0以上版本;在Android SDK MAnager更新最新的Android Support Repository(包含DataBinding支持库)
配置开发环境:
①打开build.gradle 修改gradle版本
dependencies { classpath 'com.android.tools.build:gradle:1.3.0'}
网上有的教程说是1.3.0 -beta1,但是我开始使用beta1, 重新配置后会报错,提示我使用1.3.0,后来过段时间再次尝试beta1时自动下载,正确配置上了。所以这里使用1.3.0 --> sync now②接着添加databinding版本
dependencies { classpath 'com.android.tools.build:gradle:1.3.0' classpath 'com.android.databinding:dataBinder:1.0-rc1'}这里的dataBinder后面的版本号并不能随意填写,开始我也按照网上的复制过来(1.0-rc0),sync后一直报错。后来打开sdk目录下的extras/android/m2repository/com/android/databinding/library下查看到版本号为1.0-rc1。所以修改为dataBinder:1.0-rc1 --> sync now,没有问题,成功。
③打开另一个build.gradle文件头部添加
apply plugin: 'com.android.databinding'sync now。 环境配置成功
写代码:
main.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bind="http://schemas.android.com/apk/res-auto"> <data class="UserBinding"> <import type="android.view.View" /> <variable name="user" type="com.ancx.databind.User" /> <variable name="sex" type="String" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" 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="@string/isMan" android:visibility="@{user.isMan?View.VISIBLE:View.GONE}" /> <include layout="@layout/layout" bind:sex="@{sex}" bind:user="@{user}" /> </LinearLayout></layout>
layout.xml
<?xml version="1.0" encoding="utf-8"?><layout> <data> <variable name="user" type="com.ancx.databind.User" /> <variable name="sex" type="String" /> </data> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{sex}" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.phone}" /> </LinearLayout></layout>这是布局代码,需要注意的就是最外层标签要是<layout>包裹起来,里面再分<data>和页面布局标签部分。
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);// setContentView(R.layout.activity_main); UserBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); User user = new User(); user.name = "abc"; user.phone = "123"; user.isMan = true; binding.setSex("男"); binding.setUser(user); }}User.java
public class User { public String name; public String phone; public boolean isMan;}java代码部分需要注意的是不能用Activity的setContentView方式来关联布局了。需要用DatabindingUtil.setContentView()绑定。最后在<data>标签里设置了几个variable就需要绑定几个相同类型的类。binding.setXxx()。其余的代码就很简单了。
写代码时遇到了几个bug:
①在xml布局中写的java代码,三元表达式提示错误。不过可以编译通过,应该是一处bug
②在如果xml里使用了<data>标签,那么布局里的<TextView>就不能写中文字符(其他控件可能也会有这个问题,只测试了TextView)。编译时会提示没有结束标签。应该不是编码问题,已设置成utf-8,网上有的可以写中文字符,可能是版本问题,所以这里写的中文字符是在资源文件中写的,再引用。如果是正常的布局不存在这个问题。
这种设计模式很爽,很期待Google继续完善,以后可以用到。感觉代码很简洁,很方便。MVVM的优点就不用说了。very good!
0 0
- Google I/O大会 DataBinding数据绑定 MVVM设计模式
- Android DataBinding(MVVM设计模式)
- Google I/O 大会将近
- Google 2016 i/o 大会
- Google I/O大会相关
- Android DataBinding库(MVVM设计模式)
- 【Android】DataBinding库(MVVM设计模式)
- Android DataBinding库(MVVM设计模式)
- Android DataBinding库(MVVM设计模式)
- Android DataBinding库(MVVM设计模式)
- android MVVM DataBinding 数据绑定框架 注意问题
- Android学习笔记之MVVM----DataBinding(数据双向绑定)
- 浅谈 2017 Google I/O 开发者大会
- Android设计模式MVVM之DataBinding简单使用
- Google推出Developers Live为I/O开发者大会预热
- Google I/O 2014 大会总结 Android开发新方向
- Google I/O开发者大会之Android L平台框架
- Google 2015 I/O大会为我们带来了什么?
- maven配置
- Unsupported major.minor version 51.0解决办法
- 《XMPP高级编程 使用JavaScript和jQuery》
- pdf如何修改错误
- A*算法
- Google I/O大会 DataBinding数据绑定 MVVM设计模式
- 使用ps查看进程的资源占用
- svn修改新url地址方法(linux,windows,centos)
- 腾讯、阿里、华为的岗位薪资情况概述
- MySQL查看sql语句运行时间
- Java反射的一个小问题
- mongoDB 数据导入
- STL中的set容器的一点总结
- 使用FilenameFilter来找出目录下指定后缀的文件