Android DataBinding手把手入門(mén)教程
1、在build.gradle(Module)里引入依賴(lài),然后重構(gòu)(sync Now):
android {
......
dataBinding{
enabled true
}
}
2、找到想要改為dataBinding視圖的頁(yè)面,alt+enter彈出如下:

并選擇Convert to data binding layout自動(dòng)轉(zhuǎn)換。
轉(zhuǎn)換之后效果應(yīng)和下圖類(lèi)似:

可以看到頁(yè)面出現(xiàn)了新的Layout和data的空標(biāo)簽。(data 就是用來(lái)存放數(shù)據(jù)的)
3、接下來(lái)到對(duì)應(yīng)的Activity里,聲明全局變量為視圖去掉下劃線改大寫(xiě)+binding:
如視圖為activity_main,則對(duì)應(yīng)聲明的databinding類(lèi)型變量就是ActivityMainBinding類(lèi)型,如下:

此時(shí)在Oncreate()里輸入如下代碼:
//binding的對(duì)象實(shí)例化,第二個(gè)參數(shù)為對(duì)應(yīng)的視圖id //binding=DataBindingUtil.setContentView(this,視圖id); binding= DataBindingUtil.setContentView(this,R.layout.activity_data_banding_test); //你的ViewModel實(shí)例化,ViewModel部分就不再詳細(xì)講了,也可以用其他類(lèi)型數(shù)據(jù) viewModelWithDataBanding=new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(ViewModelWithDataBanding.class); //將你準(zhǔn)備好的數(shù)據(jù),可以是ViewModel,通過(guò)binding.setData();綁定到視圖 binding.setData(viewModelWithDataBanding); //設(shè)置綁定的生命周期,這句很必要?。?! binding.setLifecycleOwner(this);
4、再次返回你的視圖,綁定你的數(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">
<data>
<variable
name="data"
type="com.example.mytests.viewModels.ViewModelWithDataBanding"
/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.DataBandingTest">
<TextView
android:id="@+id/textView2_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(data.number)}"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.257" />
<Button
android:id="@+id/button2_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="@{()->data.add()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2_1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
首先,聲明變量<variable name="你自定義變量名,data" type="你之前.setData()的數(shù)據(jù)的數(shù)據(jù)類(lèi)型"/>
然后,綁定變量如:
android:text="@{你自定義變量名.變量下的數(shù)據(jù)}";
綁定方法是注意:如:
android:onClick="@{()->data.add()}",需在方法前添加()->表示這是方法。
5、在視圖對(duì)應(yīng)的activity中可通過(guò)你聲明的綁定對(duì)象來(lái)訪問(wèn)對(duì)應(yīng)的視圖控件,如:
需要設(shè)置id為button12的點(diǎn)擊事件,可直接用綁定對(duì)象.button12.setOnclickListener(this);來(lái)操作,減少了代碼的重復(fù)性,簡(jiǎn)潔美觀。
操作控件案例截圖:

以上為DataBinding的簡(jiǎn)單使用教程,希望大家支持!
到此這篇關(guān)于Android DataBinding手把手入門(mén)教程的文章就介紹到這了,更多相關(guān)Android DataBinding內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ViewPager頂部導(dǎo)航欄聯(lián)動(dòng)效果(標(biāo)題欄條目多)
這篇文章主要介紹了ViewPager頂部導(dǎo)航欄聯(lián)動(dòng)效果,代碼簡(jiǎn)單易懂,感興趣的朋友參考下吧2016-08-08
Android自定義RecyclerView實(shí)現(xiàn)不固定刻度的刻度尺
這篇文章主要為大家詳細(xì)介紹了Android自定義RecyclerView實(shí)現(xiàn)不固定刻度的刻度尺,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
android studio 使用adb 命令傳遞文件到android 設(shè)備的方法
這篇文章主要介紹了android studio 使用adb 命令傳遞文件到android 設(shè)備的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽(tīng)
這篇文章主要為大家詳細(xì)介紹了Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽(tīng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android購(gòu)物分類(lèi)效果實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android購(gòu)物分類(lèi)效果的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android SDK Manager解決更新時(shí)的問(wèn)題 :Failed to fetch URL...
本文主要介紹解決安裝使用SDK Manager更新時(shí)的問(wèn)題:Failed to fetch URL...,這里提供了詳細(xì)的資料及解決問(wèn)題辦法,有需要的小伙伴可以參考下2016-09-09
Android中WebView實(shí)現(xiàn)點(diǎn)擊超鏈接啟動(dòng)QQ的方法
這篇文章主要給大家介紹了在Android中WebView如何實(shí)現(xiàn)點(diǎn)擊超鏈接啟動(dòng)QQ的方法,文中給出了詳細(xì)的示例代碼,相信對(duì)大家的學(xué)習(xí)或者工作具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-04-04
Android?8.0實(shí)現(xiàn)藍(lán)牙遙控器自動(dòng)配對(duì)
這篇文章主要為大家詳細(xì)介紹了Android?8.0實(shí)現(xiàn)藍(lán)牙遙控器自動(dòng)配對(duì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08

