国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Jetpack】DataBinding 架構(gòu)組件 ③ ( 使用 include 導(dǎo)入二級界面布局 | 二級頁面綁定數(shù)據(jù)模型 )

這篇具有很好參考價(jià)值的文章主要介紹了【Jetpack】DataBinding 架構(gòu)組件 ③ ( 使用 include 導(dǎo)入二級界面布局 | 二級頁面綁定數(shù)據(jù)模型 )。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。





一、使用 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 類別下 ;

【Jetpack】DataBinding 架構(gòu)組件 ③ ( 使用 include 導(dǎo)入二級界面布局 | 二級頁面綁定數(shù)據(jù)模型 )

拖入后 , 選擇之前創(chuàng)建的子布局 ;
【Jetpack】DataBinding 架構(gòu)組件 ③ ( 使用 include 導(dǎo)入二級界面布局 | 二級頁面綁定數(shù)據(jù)模型 )

為 include 設(shè)置四個(gè)方向上的約束 ;
【Jetpack】DataBinding 架構(gòu)組件 ③ ( 使用 include 導(dǎo)入二級界面布局 | 二級頁面綁定數(shù)據(jù)模型 )

將 include 寬高設(shè)置為 0dp , 也就是 match_parent ;

【Jetpack】DataBinding 架構(gòu)組件 ③ ( 使用 include 導(dǎo)入二級界面布局 | 二級頁面綁定數(shù)據(jù)模型 )

當(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) ,

【Jetpack】DataBinding 架構(gòu)組件 ③ ( 使用 include 導(dǎo)入二級界面布局 | 二級頁面綁定數(shù)據(jù)模型 )
轉(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ù)模型對象 ;

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é)果


【Jetpack】DataBinding 架構(gòu)組件 ③ ( 使用 include 導(dǎo)入二級界面布局 | 二級頁面綁定數(shù)據(jù)模型 )文章來源地址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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Jetpack業(yè)務(wù)架構(gòu)—四件套(Lifecycle、ViewModel、LiveData、DataBinding)

    Jetpack業(yè)務(wù)架構(gòu)—四件套(Lifecycle、ViewModel、LiveData、DataBinding)

    Jetpack 是一個(gè)由多個(gè)庫組成的套件,可幫助開發(fā)者遵循最佳做法、減少樣板代碼并編寫可在各種 Android 版本和設(shè)備中一致運(yùn)行的代碼,讓開發(fā)者可將精力集中于真正重要的編碼工作。 Android Jetpack組件的優(yōu)勢: Jetpack推出的主要目的是為了能夠讓開發(fā)者更加快速、方便以及高質(zhì)

    2024年02月09日
    瀏覽(28)
  • android jetpack databinding的基本使用(java)

    android jetpack databinding的基本使用(java)

    開啟databing 修改布局文件 為布局文件添加layout標(biāo)簽。 實(shí)例化布局文件 向布局文件傳遞數(shù)據(jù) 創(chuàng)建一個(gè)Sentence 類,實(shí)例化。傳給布局并顯示。 5. 在布局中引用靜態(tài)類 在sentence類中添加屬性collect ,collect 等于1表示已收藏,0表示收藏。 建立工具類CollectUtil 通過import導(dǎo)入到布局文

    2024年02月10日
    瀏覽(22)
  • 【Jetpack】ViewModel + LiveData + DataBinding 綜合使用 ( 核心要點(diǎn)說明 | 組合方式 | 代碼示例 )

    【Jetpack】ViewModel + LiveData + DataBinding 綜合使用 ( 核心要點(diǎn)說明 | 組合方式 | 代碼示例 )

    ViewModel 架構(gòu)組件 是 視圖 View 與 數(shù)據(jù)模型 Model 之間 數(shù)據(jù)交互的 橋梁 ; 傳統(tǒng) Android 開發(fā)中 , 視圖 View 與 數(shù)據(jù)模型 Model 都在 Activity 中維護(hù) , 導(dǎo)致 二者有很高的耦合度 , 不利于代碼維護(hù) ; 引入了 ViewModel 架構(gòu)組件后 , 視圖 View 與 數(shù)據(jù)模型 Model 之間實(shí)現(xiàn)了解耦 , 同時(shí)也能 保證二

    2024年02月01日
    瀏覽(24)
  • 【Jetpack】ViewModel 架構(gòu)組件 ( 視圖 View 和 數(shù)據(jù)模型 Model | ViewModel 作用 | ViewModel 生命周期 | 代碼示例 | 使用注意事項(xiàng) )

    【Jetpack】ViewModel 架構(gòu)組件 ( 視圖 View 和 數(shù)據(jù)模型 Model | ViewModel 作用 | ViewModel 生命周期 | 代碼示例 | 使用注意事項(xiàng) )

    Activity 遇到的問題 : 瞬態(tài)數(shù)據(jù)丟失 : 操作 Activity 時(shí) , 如果 屏幕 自動(dòng)旋轉(zhuǎn) , 當(dāng)前 Activity 組件會(huì) 執(zhí)行銷毀操作 , 并重新創(chuàng)建新的 Activity 組件 , 該操作會(huì) 導(dǎo)致 Activity 的 瞬態(tài)數(shù)據(jù) 丟失 ; 內(nèi)存泄漏 : 在 系統(tǒng)組件 如 Activity 中 , 啟動(dòng)了一個(gè)線程 , 在線程中執(zhí)行一系列操作 , 如果 A

    2024年01月25日
    瀏覽(19)
  • Android Jetpack組件架構(gòu):ViewModel的原理

    Android Jetpack組件架構(gòu):ViewModel的原理

    本篇文章是關(guān)于介紹ViewModel的,由于ViewModel的使用還是挺簡單的,這里就不再介紹其的基本應(yīng)用,我們主要來分析ViewModel的原理。 眾所周知,一般使用ViewModel是用來解決兩個(gè)問題的,第一個(gè)就是關(guān)于設(shè)備配置發(fā)生改變時(shí)Activity先前狀態(tài)的保存,在ViewModel出來之前我們一般會(huì)使

    2024年02月07日
    瀏覽(18)
  • 大型Android項(xiàng)目架構(gòu):基于組件化+模塊化+Kotlin+協(xié)程+Flow+Retrofit+Jetpack+MVVM架構(gòu)實(shí)現(xiàn)WanAndroid客戶端

    大型Android項(xiàng)目架構(gòu):基于組件化+模塊化+Kotlin+協(xié)程+Flow+Retrofit+Jetpack+MVVM架構(gòu)實(shí)現(xiàn)WanAndroid客戶端

    前言:茍有恒,何必三更眠五更起;最無益,莫過一日曝十日寒。 之前一直想寫個(gè) WanAndroid 項(xiàng)目來鞏固自己對 Kotlin+Jetpack+協(xié)程 等知識的學(xué)習(xí),但是一直沒有時(shí)間。這里重新行動(dòng)起來,從項(xiàng)目搭建到完成前前后后用了兩個(gè)月時(shí)間,平常時(shí)間比較少,基本上都是只能利用零碎的

    2024年02月09日
    瀏覽(27)
  • 安卓DataBinding問題:ActivityMainBinding、FragmentHomeBinding類不存在且無法自動(dòng)導(dǎo)入

    安卓DataBinding問題:ActivityMainBinding、FragmentHomeBinding類不存在且無法自動(dòng)導(dǎo)入

    找不到 ActivityMainBinding 類和 FragmentHomeBinding 類, 主要表現(xiàn)為 private 處標(biāo)紅, import 也無法自動(dòng)導(dǎo)入,強(qiáng)行手動(dòng)導(dǎo)入也是不行的。 在build.gradle(:app)中添加: 位置 詳情如下: sync一下,即可解決上述問題,類也有了,API也有了,也不標(biāo)紅了。如下圖。 ? 分別在對應(yīng)的.xml文件的根

    2024年02月06日
    瀏覽(32)
  • Jetpack Compose 中組件使用教程(比較全面)

    在本文中,我們將學(xué)習(xí) Jetpack Compose,這是一個(gè)用于構(gòu)建原生 UI 的現(xiàn)代工具包。 通過這個(gè)完整的教程,我們將學(xué)習(xí)如何使用 Text、TextField、Preview、Column、Row、Button、Card、AlertDialog、MaterialDesign 元素等。因此,事不宜遲,讓我們開始創(chuàng)建一個(gè) Jetpack Compose 項(xiàng)目。因此,本章節(jié)是

    2024年02月10日
    瀏覽(23)
  • 如何合理使用 Jetpack 組件開發(fā) Android 項(xiàng)目?

    如何合理使用 Jetpack 組件開發(fā) Android 項(xiàng)目?

    Jetpack 是 Android 官方推出的一套開發(fā)庫,其中包含眾多的組件,可以讓 Android 開發(fā)者更快更高效地開發(fā)應(yīng)用程序。Jetpack 組件分為四大部分:架構(gòu)、行為、UI 和基礎(chǔ)組件。 下面詳細(xì)闡述如何合理使用 Jetpack 組件開發(fā) Android 項(xiàng)目。 在使用 Jetpack 組件之前,首先應(yīng)熟悉幾個(gè)常用的

    2024年02月02日
    瀏覽(36)
  • 【Jetpack】Navigation 導(dǎo)航組件 ⑤ ( NavigationUI 類使用 )

    【Jetpack】Navigation 導(dǎo)航組件 ⑤ ( NavigationUI 類使用 )

    代碼地址 : CSDN ( 本博客代碼快照 | 推薦下載 0 積分 ) : https://download.csdn.net/download/han1202012/88251933 GitHub ( 可能已經(jīng)覆蓋 ) : https://github.com/han1202012/Navigation NavigationUI 類支持一些系統(tǒng)自帶的控件 , 配置后 , 自動(dòng)跳轉(zhuǎn) Fragment 界面的功能 , 使用起來非常簡潔 , 支持的可配置 Navigation

    2024年02月10日
    瀏覽(24)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包