Architecture -- Data Binding Library

简介: 1). 简介Data Binding Library是一个支持库,允许在布局文件中绑定数据源。最小支持API为14,gradle插件最小为1.5.0。示例2).
1). 简介

Data Binding Library是一个支持库,允许在布局文件中绑定数据源。最小支持API为14,gradle插件最小为1.5.0。
示例

2). 配置
  • app/build.gradle
android {
    ...
    dataBinding {
        enabled = true
    }
}
3). 布局绑定表达式
<?xml version="1.0" encoding="utf-8"?>
<!--使用DataBinding时最外层包裹的内容-->
<layout xmlns:android="http://schemas.android.com/apk/res/android">
  <!--数据节点-->
  <data>
    <!--变量-->
    <variable
        name="user"
        type="com.mazaiting.jetpack.architecture.bean.User"/>
    <variable
        name="presenter"
        type="com.mazaiting.jetpack.architecture.expression.ExpressionPresenter"/>
  </data>

  <LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    <TextView
        android:text="@{user.firstName}"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:text="@{user.lastName}"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:text="@{String.valueOf(user.index + 1)}"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:text="@{user.display ?? user.lastName}"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:text="监听"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="@{(view) -> presenter.show(view, user) }"
        />
    <Button
        android:text="引用"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="@{ presenter::onClickShow }"
        />
    <Button
        android:text="修改FirstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onChangeFirstName"
        />
  </LinearLayout>

</layout>
4). Observable UI
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
  <data>
    <!--<variable-->
        <!--name="user"-->
        <!--type="com.mazaiting.jetpack.architecture.bean.ObservableUser"/>-->
    <variable
        name="user"
        type="com.mazaiting.jetpack.architecture.bean.BaseObservableUser"/>
  </data>
  <LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
    <TextView
        android:text="@{ user.firstName }"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:text="@{ user.lastName }"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:text="@{ String.valueOf(user.age)}"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:text="修改FirstName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onChangeFirstName"
        />
  </LinearLayout>
</layout>
5). 效果演示
img_75d080135951f2b9b791b44a25f6080f.gif
效果.gif

img_8a9d041039891608727047980cb89f10.png
打印结果.png
6). 代码下载
7). 原文地址
目录
相关文章
|
Shell
Detected problems with app native libraries (please consult log for detail): lib.so: text relocation
Detected problems with app native libraries (please consult log for detail): lib.so: text relocation
183 0
|
Ubuntu
Could not load dynamic library 'libcudnn.so.8'
Could not load dynamic library 'libcudnn.so.8'
931 0
|
Java Shell PHP
Guidelines for Function Compute Development - Use Fun Local for Local Running and Debugging
Preface The following key concepts are involved in this document: Function Compute: an event-driven service that allows you to focus on writing and .
1788 0
LD_LIBRARY_PATH shouldn&#39;t contain the current directory when building glibc. Please change the envir
执行# ./glibc-2.14/configure 出现以下错误: checking LD_LIBRARY_PATH variable... contains current directory configure: error: *** LD_LIBRARY_PATH ...
1977 0
|
SQL 分布式计算 分布式数据库
Big Data Application Case Study – Technical Architecture of a Big Data Platform
How should we design the architecture of a big data platform? Are there any good use cases for this architecture?
2259 0