美文网首页
DataBinding学习(上)

DataBinding学习(上)

作者: 慎独静思 | 来源:发表于2021-09-13 23:30 被阅读0次

要使用DataBinding,需要在App module的build.gradle中配置,即使你的app module不使用data binding。

 android {
    ...
    buildFeatures {
        dataBinding true
    }
}

Data Binding的布局文件都是以<layout>为根的,layout包含两个children tag,<data>和view root。其中<data>可有可无。

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.firstName}"/>
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.lastName}"/>
   </LinearLayout>
</layout>

每一个<layout>布局文件都会生产data binding类文件。生产类文件的文件名是布局文件名去掉下划线并添加Binding后缀生成的,比如activity_main.xml生成ActivityMainBinding.class

 @Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
   User user = new User("Test", "User");
   binding.setUser(user);
}

可以通过 ActivityMainBinding或者DataBindingUtil获取目标View,它会提供set方法,来设置引入的变量。
在布局文件中,可使用如下的操作符:
Mathematical + - / * %
String concatenation +
Logical && ||
Binary & | ^
Unary + - ! ~
Shift >> >>> <<
Comparison == > < >= <= (Note that < needs to be escaped as <)
instanceof
Grouping ()
Literals - character, String, numeric, null
Cast
Method calls
Field access
Array access []
Ternary operator ?:
this, super, new和泛型调用不能在布局文件中使用。
我们可以使用 ?? 代替 ?:
data binding会进行自动的判空处理。

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

如果user为空,则会使用默认值。
在布局文件中可以引用其他view的值。

 <EditText
    android:id="@+id/example_text"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"/>
<TextView
    android:id="@+id/example_output"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{exampleText.text}"/>

其中TextView的值引用了EditText的值,exampleText是EditText的ID去掉下划线之后的结果。
可以使用[]直接访问集合类的值。

 <data>
    <import type="java.util.List"/>
    <variable name="list" type="List&lt;String>"/>
    <variable name="index" type="int"/>
</data>
…
android:text="@{list[index]}"

在布局中我们不能直接使用List<String>,需要用& l t ;(去掉空格)替换<
事件处理有两种方式:方法引用和lambda表达式

 public class MyHandlers {
    public void onClickFriend(View view) { ... }
}
 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="handlers" type="com.example.MyHandlers"/>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.firstName}"
           android:onClick="@{handlers::onClickFriend}"/>
   </LinearLayout>
</layout>

lambda表达式方式如下:

 public class Presenter {
    public void onSaveClick(Task task){}
}
 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="task" type="com.android.example.Task" />
        <variable name="presenter" type="com.android.example.Presenter" />
    </data>
    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:onClick="@{() -> presenter.onSaveClick(task)}" />
    </LinearLayout>
</layout>

布局文件会默认提供context变量。

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:bind="http://schemas.android.com/apk/res-auto">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <include layout="@layout/name"
           bind:user="@{user}"/>
       <include layout="@layout/contact"
           bind:user="@{user}"/>
   </LinearLayout>
</layout>

在include中使用。

相关文章

网友评论

      本文标题:DataBinding学习(上)

      本文链接:https://www.haomeiwen.com/subject/hdgswltx.html