DataBinding的基本使用

本文详细介绍了Android DataBinding库的集成步骤、基本使用、数据绑定、表达式、集合和资源使用、事件处理、可观察对象、绑定类、BindingAdapters、数据双向绑定等内容,帮助开发者深入理解并掌握DataBinding在实际开发中的应用。
摘要由CSDN通过智能技术生成

转载请注明链接: 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() 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值