一、使用 include 導(dǎo)入二級界面布局
如果在 DataBinding 布局 中 , 通過 include 引入了其它布局 , 數(shù)據(jù)模型對象 需要 綁定到 通過 include 引入的二級頁面 布局文件中 ;
在上一篇博客 【Jetpack】DataBinding 架構(gòu)組件 ② ( 字符串拼接函數(shù) | 綁定點(diǎn)擊事件函數(shù) | DataBinding 布局中使用 import 標(biāo)簽導(dǎo)入 Java、Kotlin 類 ) 的示例中 , 有兩個(gè) TextView 組件 , 將其中顯示年齡的 TextView 組件設(shè)置到單獨(dú)的 XML 布局文件中 , 使用 include 標(biāo)簽引入該布局文件 , 這里就需要 將綁定的數(shù)據(jù)對象 , 傳遞到二級頁面 ;
設(shè)置一個(gè)子布局 activity_sub.xml , 內(nèi)容如下 :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{student.onClick}"
android:text="@{student.ageText()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2"
tools:text="18" />
</androidx.constraintlayout.widget.ConstraintLayout>
在 activity_main 的 Design 模式下 , 拖動(dòng)一個(gè) include 容器到布局中 , include 在 Containers 類別下 ;
拖入后 , 選擇之前創(chuàng)建的子布局 ;
為 include 設(shè)置四個(gè)方向上的約束 ;
將 include 寬高設(shè)置為 0dp , 也就是 match_parent ;
當(dāng)前布局代碼如下 :
<?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="student"
type="kim.hsl.databinding_demo.Student" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Tom"
android:text="@{student.nameText()}"
android:onClick="@{student.onClick}"
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.4" />
<include
layout="@layout/activity_sub"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
二、二級頁面綁定數(shù)據(jù)模型
1、將二級界面布局轉(zhuǎn)為 DataBinding 布局
將導(dǎo)入的 activity_sub.xml 也轉(zhuǎn)為 DataBinding 布局 , 將光標(biāo)放在第一個(gè)字符位置 , 使用 Alt + 回車 快捷鍵 ,
彈出如下對話框 , 選擇 " Convert to data binding layout " 選項(xiàng) ,
轉(zhuǎn)換為 DataBinding 布局后 , 設(shè)置如下數(shù)據(jù)模型 :
<data>
<variable
name="student"
type="kim.hsl.databinding_demo.Student" />
</data>
完整的布局文件如下 :
<?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="student"
type="kim.hsl.databinding_demo.Student" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{student.onClick}"
android:text="@{student.ageText()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2"
tools:text="18" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
2、在主布局中為二級界面布局傳遞數(shù)據(jù)模型
首先 , 在布局根目錄 , 聲明如下命名空間 ;
xmlns:app="http://schemas.android.com/apk/res-auto"
然后 , 在 include 標(biāo)簽中 , 設(shè)置 app:student 屬性標(biāo)簽 , 屬性值為 variable 標(biāo)簽中的 name 對象名稱 ;
-
屬性名稱 : 該屬性的名稱 , 也是不固定的 , 屬性名稱是
app:對象名稱
;
<include
layout="@layout/activity_sub"
app:student="@{student}"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
最后 , 在 二級頁面 布局中 , 使用數(shù)據(jù)模型 ;
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{student.onClick}"
android:text="@{student.ageText()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2"
tools:text="年齡 : 18" />
三、核心代碼示例
1、主布局
在主布局中使用 include 導(dǎo)入二級頁面 , 在 include 標(biāo)簽中 , 設(shè)置
app:student="@{student}"
屬性 , 該屬性名稱是 app:數(shù)據(jù)模型對象名稱
, 屬性值是 數(shù)據(jù)模型對象 ;
在
<data>
<variable
name="student"
type="kim.hsl.databinding_demo.Student" />
</data>
中 , 設(shè)置的 DataBinding 數(shù)據(jù)模型對象名稱是 student , 在 include 中傳遞的 數(shù)據(jù)模型 屬性的屬性名稱就是 app:student
;
布局代碼 :
<?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="student"
type="kim.hsl.databinding_demo.Student" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Tom"
android:text="@{student.nameText()}"
android:onClick="@{student.onClick}"
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.4" />
<include
layout="@layout/activity_sub"
app:student="@{student}"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
2、子布局
在子布局中 , 也需要轉(zhuǎn)為 DataBinding 布局 , 配置的 數(shù)據(jù)模型
<data>
<variable
name="student"
type="kim.hsl.databinding_demo.Student" />
</data>
與 主布局 一樣 , 在主布局中的 include 中使用 app:student="@{student}"
配置導(dǎo)入數(shù)據(jù)模型 ,
即可在子布局中使用該 數(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="student"
type="kim.hsl.databinding_demo.Student" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{student.onClick}"
android:text="@{student.ageText()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2"
tools:text="年齡 : 18" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
3、Java 代碼
數(shù)據(jù)模型代碼 : 該數(shù)據(jù)模型 , 封裝了 name 和 age 兩個(gè)屬性 , 以及對應(yīng)的 字符串拼接函數(shù) ;
package kim.hsl.databinding_demo
import android.util.Log
import android.view.View
class Student(var name: String, var age: Int) {
fun nameText(): String {
return "姓名 : ${name}"
}
fun ageText(): String {
return "年齡 : ${age}"
}
fun onClick(view: View): Unit {
Log.i("", "${nameText()} ${ageText()} 觸發(fā)點(diǎn)擊事件")
}
}
Activity 組件代碼 : 在 Activity 組件中 , 為布局設(shè)置 Student 數(shù)據(jù)模型對象 ;文章來源:http://www.zghlxwxcb.cn/news/detail-401448.html
package kim.hsl.databinding_demo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import kim.hsl.databinding_demo.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 設(shè)置布局文件
// 布局文件是 activity_main.xml
// 該類名稱生成規(guī)則是 布局文件名稱 + Binding
var activityMainBinding: ActivityMainBinding =
DataBindingUtil.setContentView(this, R.layout.activity_main)
// 為布局 設(shè)置 數(shù)據(jù)
activityMainBinding.student = Student("Jerry", 13)
}
}
4、執(zhí)行結(jié)果
文章來源地址http://www.zghlxwxcb.cn/news/detail-401448.html
到了這里,關(guān)于【Jetpack】DataBinding 架構(gòu)組件 ③ ( 使用 include 導(dǎo)入二級界面布局 | 二級頁面綁定數(shù)據(jù)模型 )的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!