Android 學習日記----jetpack---androidx.dataBinding

Databinding概述:

數(shù)據(jù)綁定庫是一種支持庫,借助該庫,您可以使用聲明性格式(而非程序化地)將布局中的界面組件綁定到應用中的數(shù)據(jù)源。

布局通常是使用調(diào)用界面框架方法的代碼在 Activity 中定義的。例如,以下代碼調(diào)用 findViewById() 來查找 TextView 微件并將其綁定到 viewModel 變量的 userName 屬性:

? ? findViewById<TextView>(R.id.sample_text).apply {

text

= viewModel.userName

}

以下示例展示了如何在布局文件中使用數(shù)據(jù)綁定庫將文本直接分配到微件。這樣就無需調(diào)用上述任何 Java 代碼。請注意賦值表達式中 @{} 語法的使用:

<TextViewandroid:text="@{viewmodel.userName}" />

借助布局文件中的綁定組件,您可以移除 Activity 中的許多界面框架調(diào)用,使其維護起來更簡單、方便。還可以提高應用性能,并且有助于防止內(nèi)存泄漏以及避免發(fā)生 Null 指針異常。

使用數(shù)據(jù)綁定庫

要了解如何在 Android 應用中使用數(shù)據(jù)綁定庫,請參閱以下頁面。

開始使用

了解如何準備開發(fā)環(huán)境以使用數(shù)據(jù)綁定庫,包括在 Android Studio 中支持數(shù)據(jù)綁定代碼。

布局和綁定表達式

借助表達式語言,您可以編寫將變量關聯(lián)到布局中的視圖的表達式。數(shù)據(jù)綁定庫會自動生成將布局中的視圖與您的數(shù)據(jù)對象綁定所需的類。該庫提供了可在布局中使用的導入、變量和包含等功能。

該庫的這些功能可與您的現(xiàn)有布局無縫地共存。例如,可以在表達式中使用的綁定變量在 data 元素(界面布局根元素的同級)內(nèi)定義。這兩個元素都封裝在 layout 標記中,如以下示例所示:

<layout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><data><variablename="viewmodel"type="com.myapp.data.ViewModel" /></data><ConstraintLayout... /> <!-- UI layout's root element --></layout>

使用可觀察的數(shù)據(jù)對象

數(shù)據(jù)綁定庫提供了可讓您輕松地觀察數(shù)據(jù)更改情況的類和方法。您不必操心在底層數(shù)據(jù)源發(fā)生更改時刷新界面。您可以將變量或其屬性設為可觀察。借助該庫,您可以將對象、字段或集合設為可觀察。

生成的綁定類

數(shù)據(jù)綁定庫可以生成用于訪問布局變量和視圖的綁定類。此頁面展示了如何使用和自定義所生成的綁定類。

綁定適配器

每一個布局表達式都有一個對應的綁定適配器,要求必須進行框架調(diào)用來設置相應的屬性或監(jiān)聽器。例如,綁定適配器負責調(diào)用 setText() 方法來設置文本屬性,或者調(diào)用 setOnClickListener() 方法向點擊事件添加監(jiān)聽器。最常擁的綁定適配器(例如針對本頁面的示例中使用的 android:text 屬性)可供您在 android.databinding.adapters 軟件包中使用。如需常用綁定適配器列表,請參閱適配器。您也可以按照以下示例所示創(chuàng)建自定義適配器:

? ? @BindingAdapter("app:goneUnless")fun goneUnless(view: View, visible: Boolean) {

view

.visibility = if (visible) View.VISIBLE else View.GONE

}

將布局視圖綁定到架構組件

Android 支持庫包含架構組件,您可以使用這些組件設計穩(wěn)健、可測試且易維護的應用。您可以將架構組件與數(shù)據(jù)綁定庫一起使用,以進一步簡化界面開發(fā)。

雙向數(shù)據(jù)綁定

數(shù)據(jù)綁定庫支持雙向數(shù)據(jù)綁定。此類綁定使用的表示法支持以下操作:接收對屬性的數(shù)據(jù)更改,同時監(jiān)聽用戶對此屬性的更新。

當然以上都是在谷歌開發(fā)者官方網(wǎng)站都可以找到,需要的同學可以到官網(wǎng)查看谷歌官方傳送門

下面就開始我門今天的示例操作:

首先我門的前期工作就是創(chuàng)建相應的activity和viewModel以及布局文件,當然這個和前面的將Livedata時候的文件基本相同,這里就不在做過多的說明了,想要的同學可以看前面的文章,或者在文章結束的時候,我會給大家我的項目示例的GitHub地址,需要的同學可以下載。

首先來的我先將databinding在我門的項目中啟用,這很簡單,只需要在我門的gradle中添加以下代碼就可以了

dataBinding{

? ? enabled=true

}

設置完成之后,我們同步gradle就可以使用dataBinding了。

首先來到布局文件,在布局文件中的最上面的有一個小黃色的燈泡,點擊燈泡,就可以看到一個轉(zhuǎn)換為databinding布局文件的提示;

轉(zhuǎn)換完成之后的布局文件樣式如下:


就會發(fā)現(xiàn)根布局文件變成了layout了,并多了一個data包裹的區(qū)域。

完成布局文件的轉(zhuǎn)換之后,來到activity中進行dataBinding的綁定。綁定如下

public class DataBindActivity extends AppCompatActivity {

private DataBindingViewModel dataBindingViewModel;

//設置dataBinding

private ActivityDataBindBinding dataBindBinding;

? ? @Override

? ? protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//綁定不布局文件

dataBindBinding=DataBindingUtil.setContentView(this,R.layout.activity_data_bind);

? ? ? ? dataBindingViewModel=new ViewModelProvider(this).get(DataBindingViewModel.class);

//綁定viewModel數(shù)據(jù)層,setData跟布局文件中的name=data相同

? ? dataBindBinding.setData(dataBindingViewModel);

//生命周期監(jiān)控

dataBindBinding.setLifecycleOwner(this);

? ? }

}

經(jīng)過綁定之后我門能夠發(fā)現(xiàn),比起之前activity中的代碼量少了很多,看起來更加的舒服了。

然后我到布局文件中進行數(shù)據(jù)和點擊事件的綁定代碼如下:

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"

? ? xmlns:app="http://schemas.android.com/apk/res-auto"

? ? xmlns:tools="http://schemas.android.com/tools">

? ? ? ? name="data"

? ? ? ? type="com.example.jetpackdemo.dataBinding.DataBindingViewModel" />

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="match_parent"

? ? ? ? tools:context=".dataBinding.DataBindActivity">

? ? ? ? ? ? android:id="@+id/guideline"

? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:orientation="vertical"

? ? ? ? ? ? app:layout_constraintGuide_begin="205dp" />

? ? ? ? ? ? android:id="@+id/btn1"

? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:text="按鈕1"

? ? ? ? ? ? android:onClick="@{()->data.addTickadd1()}"

? ? ? ? ? ? app:layout_constraintBottom_toBottomOf="parent"

? ? ? ? ? ? app:layout_constraintEnd_toStartOf="@+id/guideline"

? ? ? ? ? ? app:layout_constraintStart_toStartOf="parent"

? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent" />

? ? ? ? ? ? android:id="@+id/btn2"

? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:text="按鈕2"

? ? ? ? ? ? android:onClick="@{()->data.addTickadd2()}"

? ? ? ? ? ? app:layout_constraintBottom_toBottomOf="parent"

? ? ? ? ? ? app:layout_constraintEnd_toEndOf="parent"

? ? ? ? ? ? app:layout_constraintStart_toStartOf="@+id/guideline"

? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent" />

? ? ? ? ? ? android:id="@+id/tv1"

? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:text="@{String.valueOf(data.tickadd1)}"

? ? ? ? ? ? app:layout_constraintBottom_toTopOf="@+id/btn1"

? ? ? ? ? ? app:layout_constraintEnd_toStartOf="@+id/guideline"

? ? ? ? ? ? app:layout_constraintStart_toStartOf="parent"

? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent" />

? ? ? ? ? ? android:id="@+id/tv2"

? ? ? ? ? ? android:layout_width="47dp"

? ? ? ? ? ? android:layout_height="19dp"

? ? ? ? ? ? android:text="@{String.valueOf(data.tickadd2)}"

? ? ? ? ? ? app:layout_constraintBottom_toTopOf="@+id/btn2"

? ? ? ? ? ? app:layout_constraintEnd_toEndOf="parent"

? ? ? ? ? ? app:layout_constraintStart_toStartOf="@+id/guideline"

? ? ? ? ? ? app:layout_constraintTop_toTopOf="parent" />

</layout>

到這里基本就完成了數(shù)據(jù)的雙向綁定了,接下來看看效果吧!


可以看到實現(xiàn)了我們的目的。

下面是本項目的GitHub地址

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容