DataBinding的意義:
- 讓布局文件承擔(dān)了部分原本屬于頁面的工作次员,使頁面與布局耦合度進(jìn)一步降低
DataBinding使用:
build.gradle中配置
buildFeatures {
dataBinding true
}
布局文件:
可使用快捷生成databinding模板代碼
鼠標(biāo)放到布局文件內(nèi)容首位alt +enter
image.png
<?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="user"
type="com.example.composedemo.User" />
<variable
name="eventhandlelistener"
type="com.example.composedemo.EventHandleListener" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Test2Activity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.name}"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:text="點(diǎn)贊"
android:onClick="@{eventhandlelistener.onButtonClick}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
<include
layout="@layout/sub"
app:user = "@{user}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
include 二級布局:
<?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">
<data>
<variable
name="user"
type="com.example.composedemo.User" />
<import type="com.example.composedemo.ScoreUtil"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/scoreView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{ ScoreUtil.getScoreName(user.score) }"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
該布局包含二級頁面引用败许,
<variable> 標(biāo)簽 作用引入對象
name="user" 對象名稱
type="com.example.composedemo.User" 對象地址
<import>標(biāo)簽 作用導(dǎo)入工具類可直接使用的靜態(tài)方法
activity 中使用
class Test2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var dataBinding: ActivityTest2Binding =
DataBindingUtil.setContentView(this, R.layout.activity_test2)
var user = User("小美",3)
// <variable
// name="user"
// type="com.example.composedemo.User" />
dataBinding.user = user //對應(yīng)xml定義的對象名
//button 點(diǎn)擊事件的處理類和User類似
dataBinding.eventhandlelistener = EventHandleListener(this)
}
}
點(diǎn)擊事件處理類:
class EventHandleListener(var context: Context) {
fun onButtonClick(view:View){
Toast.makeText(context,"點(diǎn)贊",Toast.LENGTH_LONG).show()
}
}
在xml中的使用就是: android:onClick="@{eventhandlelistener.onButtonClick}"
評分顯示處理類:
object ScoreUtil {
@JvmStatic
fun getScoreName(score: Int): String {
return when (score) {
1 -> "一星"
2 -> "二星"
3 -> "三星"
4 -> "四星"
5 -> "五星"
else -> {
"未評級"
}
}
}
}
在xml中的使用就是: android:text="@{ ScoreUtil.getScoreName(user.score) }"
針對二級頁面的對象數(shù)據(jù)傳遞定義:
在二級頁面定義
<variable
name="user"
type="com.example.composedemo.User" />
父頁面使用: app:user = "@{user}"
此處的app:user 命名對應(yīng)二級頁面對象的命名( <variable>中的name值)
二級頁面的使用就可以和父頁面的使用方法一樣
image.png