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). 原文地址
目录
相关文章
|
6月前
target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target S
target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target S
98 0
《Data infrastructure architecture for a medium size organization tips for collecting, storing and analysis》电子版地址
Data infrastructure architecture for a medium size organization: tips for collecting, storing and analysis
86 0
《Data infrastructure architecture for a medium size organization tips for collecting, storing and analysis》电子版地址
|
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
163 0
|
Ubuntu
Could not load dynamic library 'libcudnn.so.8'
Could not load dynamic library 'libcudnn.so.8'
918 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?
2249 0