转载请注明链接: https://blog.csdn.net/feather_wch/article/details/88597266
DataBinding的基本使用
版本号:2019-03-23(13:20)
文章目录
集成DataBinding
SDK Manager中进行下载
1、SDK Manager的Tools中下载Android Support Repository
build.gradle中开启
2、根目录build.gradle中增加能下载的仓库
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
3、app的build.gradle中开启databinding
dataBinding{
enabled = true
}
4、AS 3.1开始使用新的Databinding 增加编译器
gradle.properties
android.databinding.enableV2=true
布局中的使用和绑定表达式
基础知识
import
1、import用于导入需要用的类,避免在xml布局中报错
<data>
<import type="android.view.View"/>
</data>
就可以使用View了
<TextView
android:text="@{user.lastName}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"/>
类型别名
2、如果导入的两个类名称一致,避免混乱,应该使用别名
// View
<import type="android.view.View"/>
// 还是View
<import type="com.example.real.estate.View"
alias="Vista"/>
用Vista就能代指
com.example.real.estate.View
导入其它类
3、导入其它类
<data>
<import type="com.example.User"/>
<import type="java.util.List"/>
<variable name="user" type="User"/>
<variable name="userList" type="List<User>"/>
</data>
4、也可以进行类型转换
<TextView
android:text="@{((User)(object)).lastName}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
context
5、布局中有个特殊的变量为context,是根View的getContext()获得的对象
该名
context
通过显式的variable,可以被重写掉。
include
1、databinding支持从父布局将变量传递到内部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.example.User"/>
</data>
<!-- WORK! -->
<LinearLayout
android:orientation="vertical">
<include layout="@layout/name"
bind:user="@{user}"/>
<include layout="@layout/contact"
bind:user="@{user}"/>
</LinearLayout>
</layout>
但是不支持
merge标签
<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.example.User"/>
</data>
<!-- Doesn't work -->
<merge>
<include layout="@layout/name"
bind:user="@{user}"/>
<include layout="@layout/contact"
bind:user="@{user}"/>
</merge>
</layout>
基本使用
1、Databinding的使用是在布局中通过标签layout、data、variable
实现
1-使用如下: 将用户类User,作为别名user。将账号和密码和对应控件绑定。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="user"
type="com.hao.architecture.User"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/account_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{user.account}"/>
<Button
android:id="@+id/password_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{user.password}"/>
</LinearLayout>
</layout>
2-User类
package com.hao.architecture;
public class User {
String account;
String password;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword()