Android DataBinding手把手入門教程
1、在build.gradle(Module)里引入依賴,然后重構(gòu)(sync Now):
android { ...... dataBinding{ enabled true } }
2、找到想要改為dataBinding視圖的頁面,alt+enter彈出如下:
并選擇Convert to data binding layout自動轉(zhuǎn)換。
轉(zhuǎn)換之后效果應(yīng)和下圖類似:
可以看到頁面出現(xiàn)了新的Layout和data的空標(biāo)簽。(data 就是用來存放數(shù)據(jù)的)
3、接下來到對應(yīng)的Activity里,聲明全局變量為視圖去掉下劃線改大寫+binding:
如視圖為activity_main,則對應(yīng)聲明的databinding類型變量就是ActivityMainBinding類型,如下:
此時在Oncreate()里輸入如下代碼:
//binding的對象實(shí)例化,第二個參數(shù)為對應(yīng)的視圖id //binding=DataBindingUtil.setContentView(this,視圖id); binding= DataBindingUtil.setContentView(this,R.layout.activity_data_banding_test); //你的ViewModel實(shí)例化,ViewModel部分就不再詳細(xì)講了,也可以用其他類型數(shù)據(jù) viewModelWithDataBanding=new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(ViewModelWithDataBanding.class); //將你準(zhǔn)備好的數(shù)據(jù),可以是ViewModel,通過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ù)類型"/>
然后,綁定變量如:
android:text="@{你自定義變量名.變量下的數(shù)據(jù)}";
綁定方法是注意:如:
android:onClick="@{()->data.add()}",需在方法前添加()->表示這是方法。
5、在視圖對應(yīng)的activity中可通過你聲明的綁定對象來訪問對應(yīng)的視圖控件,如:
需要設(shè)置id為button12的點(diǎn)擊事件,可直接用綁定對象.button12.setOnclickListener(this);來操作,減少了代碼的重復(fù)性,簡潔美觀。
操作控件案例截圖:
以上為DataBinding的簡單使用教程,希望大家支持!
到此這篇關(guān)于Android DataBinding手把手入門教程的文章就介紹到這了,更多相關(guān)Android DataBinding內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ViewPager頂部導(dǎo)航欄聯(lián)動效果(標(biāo)題欄條目多)
這篇文章主要介紹了ViewPager頂部導(dǎo)航欄聯(lián)動效果,代碼簡單易懂,感興趣的朋友參考下吧2016-08-08Android自定義RecyclerView實(shí)現(xiàn)不固定刻度的刻度尺
這篇文章主要為大家詳細(xì)介紹了Android自定義RecyclerView實(shí)現(xiàn)不固定刻度的刻度尺,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-07-07android studio 使用adb 命令傳遞文件到android 設(shè)備的方法
這篇文章主要介紹了android studio 使用adb 命令傳遞文件到android 設(shè)備的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽
這篇文章主要為大家詳細(xì)介紹了Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Android SDK Manager解決更新時的問題 :Failed to fetch URL...
本文主要介紹解決安裝使用SDK Manager更新時的問題:Failed to fetch URL...,這里提供了詳細(xì)的資料及解決問題辦法,有需要的小伙伴可以參考下2016-09-09Android中WebView實(shí)現(xiàn)點(diǎn)擊超鏈接啟動QQ的方法
這篇文章主要給大家介紹了在Android中WebView如何實(shí)現(xiàn)點(diǎn)擊超鏈接啟動QQ的方法,文中給出了詳細(xì)的示例代碼,相信對大家的學(xué)習(xí)或者工作具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04Android?8.0實(shí)現(xiàn)藍(lán)牙遙控器自動配對
這篇文章主要為大家詳細(xì)介紹了Android?8.0實(shí)現(xiàn)藍(lán)牙遙控器自動配對,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08