mvvm架构使用解析

配置

android studio目前已经集成了dataBinding,只需在build.gradle中配置,如下:

android {

    dataBinding {
        enabled = true;
    }
}

布局文件

使用 Data Binding 之后,xml的布局文件就不再单纯地展示 UI 元素,还需要定义 UI 元素用到的变量。所以,它的根节点不再是一个 ViewGroup,而是变成了 layout,并且新增了一个节点 data

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
    </data>
    <!--原先的根节点(Root Element)-->
    <RelativeLayout>
    ....
    </RelativeLayout>
</layout>

要实现 MVVM 的 ViewModel 就需要把数据与UI进行绑定,data 节点就为此提供了一个桥梁,我们先在 data 中声明一个 variable,这个变量会为 UI 元素提供数据(例如 TextView 的 android:text),然后在 Java 代码中把”后台”数据与这个 variable 进行绑定。

数据对象

添加一个entity类,NewsEntity

public class NewsEntity extends BaseObservable {

    private String name;
    private String title;

    @Bindable
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }

    @Bindable
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
        notifyPropertyChanged(BR.title);
    }
}

定义 Variable

再回到布局文件,在 data 节点中声明一个变量 news

    <data>
        <variable
            name="news"
            type="com.example.demo.mvvm.entity.NewsEntity"/>
    </data>
        <TextView
            android:id="@+id/activity_main_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{news.name}"/>

绑定 Variable

修改 Activity 的 onCreate 方法,用 DatabindingUtil.setContentView() 来替换掉 setContentView(),然后创建一个 NewsEntity对象,通过 binding.setNews(news) 与 variable 进行绑定。

高级用法

使用类方法

首先为类添加一个静态方法

public class MyStringUtils {
    public static String capitalize(final String word) {
        if (word.length() > 1) {
            return String.valueOf(word.charAt(0)).toUpperCase() + word.substring(1);
        }
        return word;
    }
}

然后在 xml 的 data 节点中导入:

<import type="com.liangfeizc.databindingsamples.utils.MyStringUtils" />

使用方法与 Java 语法一样:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{StringUtils.capitalize(user.firstName)}" />

类型别名

如果我们在 data 节点了导入了两个同名的类怎么办?

<import type="com.example.home.data.User" />
<import type="com.examle.detail.data.User" />
<variable name="user" type="User" />

这样一来出现了两个 User 类,那 user 变量要用哪一个呢?不用担心,import 还有一个 alias 属性。

<import type="com.example.home.data.User" />
<import type="com.examle.detail.data.User" alias="DetailUser" />
<variable name="user" type="DetailUser" />

Null Coalescing 运算符

android:text="@{user.displayName ?? user.lastName}"

就等价于

android:text="@{user.displayName != null ? user.displayName : user.lastName}"

属性值

通过 ${} 可以直接把 Java 中定义的属性值赋值给 xml 属性。

<TextView
   android:text="@{user.lastName}"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"/>

使用资源数据

<TextView
    android:padding="@{large? (int)@dimen/largePadding : (int)@dimen/smallPadding}"
    android:background="@android:color/black"
    android:textColor="@android:color/white"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
原文地址:https://www.cnblogs.com/huozhong/p/6114584.html