DataBinding

万众瞩目,期待已久的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内容。可惜的是,我还没研究透彻。本着认真负责的原则,我会继续研究尽快把双向绑定加上的。本篇内容就到以上了,感谢读到这里。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dinding是一种通过机制,将代码中的数据和XML(UI)进行绑定的技术。它允许双方都能对数据进行操作,并在数据发生变化时自动刷新数据。 在Android中,我们可以使用DataBindingUtil类的方法来创建Databinding实例,即通过DataBindingUtil.inflate(LayoutInflater, layoutId, parent, attachToParent)或DataBindingUtil.bindTo(viewRoot, layoutId)方法来得到绑定对象。 Databinding主要解决了Model层与View交互的问题,并且现在也支持双向绑定。它的优势在于可以简化代码,使得数据与UI更紧密地结合。我们可以通过一个登录的例子来进一步感受Databinding的优势。你可以下载并查看一个登录的Demo,或者自己编写相关的例子来深入理解Databinding的用法和好处。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [DataBinding详解](https://blog.csdn.net/peiyuWang_2015/article/details/76945081)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [DataBinding使用教程详解](https://blog.csdn.net/zhangwude2301/article/details/80069972)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值