DataBinding
databinding是Google官方發布的一個框架,用于降低布局和邏輯的耦合性,使代碼邏輯更加清晰,可以直接系結資料到xml中,并實作自動重繪,databinding能夠省去findViewById(),減少大量的模板代碼,資料能夠單向或雙向系結到layout檔案中,有助于防止記憶體泄漏,而且能自動進行空檢測以避免空指標例外,
簡單使用
1. 啟動dataBinding
在Module的build.gradle中加上如下配置
apply plugin: 'kotlin-kapt'
android{
//AS 4.0以下,
dataBinding{
enabled true
}
//AS 4.1之后
bindingFeature{
dataBinding = true
//for view binding :
//viewBinding = true)
}
}
2. 修改布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="test"
type="String" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{test}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
- 根節點變成了layout
- layout里面包含了一個data節點和傳統的視圖
- data標簽記憶體放用于xml的資料變數 ,使用variable來定義
- variable里面的name屬性表示變數屬性,type表示變數的型別,這里定義的是一個String類,由于java.lang.*包會被自動匯入,所以基本的資料型別可以直接使用,這里的資料型別也可以是自己定義的物體類(資料型別需要寫上物體類完整的位置,也可以使用import引入)
- 最后使用 @{test} 將test與AppCompatTextView的text進行系結,這里是單向系結,意思是dataBinding的test發生改變時text會接收它的改變,但是text發生改變時test不會改變,如需要雙向系結可使用 @={test},這樣text發生改變時test也會接收改變(一般用于)
3. 在Activiy或Fragment中系結
Activity中
//進行系結,并回傳一個Binding實體 ActivityMainBinding是系統根據布局名稱所生成的
val mBinding =DataBindingUtil.setContentView<ActivityMainBinding> (this,
R.layout.activity_main)
//呼叫mBinding來給布局中的控制元件賦值
mBinding.test="abc"
Fragment中
//第一種
xxxxxxxBinding.bind(view)
//第二種
xxxxxxxBinding.inflate()
使用技巧
1.支持運算式
- 數學運算式
- 字串拼接:+ - (使用``反引號)
- 邏輯運算式、位運算子、比較運算子
- 三目運算子 ?:
- 等等…
下面舉例使用拼接符
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test+test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
2. 處理點擊事件
<layout>
<data>
<variable
name="OnClickListener"
type="android.view.View.OnClickListener" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{OnClickListener}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
動態更新
至此,如果我們在databinding中定義了基本的資料型別類或者物體類的話,當它們的內容發生變化時,UI界面的資料是不會動態更新的,DataBinding也為我們提供了3種動態更新的機制,分別為Observable、ObservableField和Observable容器,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/290311.html
標籤:其他
下一篇:自動搶票之 12306 登錄篇
