Data Binding使用技巧

Data Binding

根据变量,自动赋值到各widget。

How

1.编写layout文件,这里的layout为: act_data_bind_demo.xml

这里需要先准备变量

在具体的widget上使用该变量 <TextView android:layoutwidth="matchparent" android:layoutheight="wrapcontent" android:text="@{user.name}"/>

 

2.Binding data 上面的步骤完成后,需要先编译整个Project,因为在这个过程中会生成一些类:DataBindingUtil,ActDataBindDemoBinding

 

 

注意:这里的ActDataBindDemoBinding是根据上面的布局文件的名字生成的。

在onCreate方法中:

ActDataBindDemoBinding binding = DataBindingUtil.setContentView(this, 
         R.layout.act_data_bind_demo);

customer = new Customer();
customer.setName("Andy");
customer.setMobile("13866668888");

binding.setUser(customer);

运行后,可以看到页面中的两个TextView都有值。

问题:如何获取EditText的输入内容?

failed:

<EditText
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:inputType="textEmailAddress"
   android:text="@{user.email}"/>

It's work: "

...android:text="@={user.email}"

问题:如何通过一个按钮改变当前页面中的TextView的值?

1.module类继承BaseObservable

public class Customer extends BaseObservable {
...
public void setMobile(String mobile) {
    this.mobile = mobile;
    notifyPropertyChanged(BR.mobile);
}

继承BaseObservable,同时在setter方法中调用notifyPropertyChanged。

2.更新Java中的Customer对象,例如当mobile改变,UI会制动刷新。

Custom Binding Class Names

默认情况下,Binding Class是根据layout文件名生成的。(例如,当layout文件为act_data_bind_demo.xml,生成的class为:ActDataBindDemoBinding)

可以自定义类名,以及设定包名:

<data class="com.andy.infrastructure.demos.databinding.DataBind">
    <variable ...
</data>

上面的代码中,会在包com.andy.infrastructure.demos.databinding下生成一个名为DataBind的类。注意这个package必须是存在的,当然也可以不指定package(会在默认的包下生成该类)。

Include

在layout文件中使用include时,也可以传递数据到指定的layout中。需要额外2个步骤:

  1. 确保传递的变量在两个layout中都有声明

    <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bind="http://schemas.android.com/apk/res-auto"> ... ... <include layout="@layout/user_name" bind:user="@{user}"/>

    被引入的layout: ...

    <TextView android:layoutwidth="matchparent" android:layoutheight="wrapcontent" android:padding="15dp" android:text="@{user.name}">

  2. 外层layout中传递变量

    <include layout="@layout/user_name" bind:user="@{user}"/>

Expression Language

  • 数学运算 + - / * % 
  • 字符串拼接 + 
  • 逻辑运算符 && || 
  • 位运算 & | ^ 
  • Unary + - ! ~ 
  • 位移 >> >>> << 
  • 比较运算符 == > < >= <= 
  • instanceof 
  • Grouping () 
  • Literals character, String, numeric, null 
  • Cast 
  • Method calls 
  • Field access 
  • Array access [] 
  • 三元运算符 ? : 

  • Null Coalescing Operator ??

如果??左边不为空就使用左边值,否则就使用右边的值。

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

上面的代码相当于:

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

Data Objects

三种数据更新提示机制:

  • Observable Object
  • Observable Fields
  • Observable Collections
Observable Object

这种方式,需要做三个步骤:

  • 继承BaseObservable
  • 为getter添加@Bindable
  • 在setter中添加代码notifyPropertyChanged(BR.name);

Views With IDs

A public final field will be generated for each View with an ID in the layout. The binding does a single pass on the View hierarchy, extracting the Views with IDs. This mechanism can be faster than calling findViewById for several Views.

layout文件中widget有设置id时,可以使用bind对象直接访问该控件,省去findViewById

 

文本中的代码示例,链接如下:

源码:https://github.com/ithaibo/InfrastructureAndroid/tree/master/app/src/main/java/com/andy/infrastructure/demos/databinding

转载于:https://www.cnblogs.com/ithaibo-sit/p/6256759.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值