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

【Jetpack】DataBinding 架構(gòu)組件 ⑤ ( 數(shù)據(jù)模型與視圖雙向綁定 | BaseObservable 實(shí)現(xiàn)雙向綁定 | ObservableField 實(shí)現(xiàn)雙向綁定 )

這篇具有很好參考價(jià)值的文章主要介紹了【Jetpack】DataBinding 架構(gòu)組件 ⑤ ( 數(shù)據(jù)模型與視圖雙向綁定 | BaseObservable 實(shí)現(xiàn)雙向綁定 | ObservableField 實(shí)現(xiàn)雙向綁定 )。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。





一、數(shù)據(jù)模型 Model 與視圖 View 雙向綁定




1、數(shù)據(jù)模型 Model 與視圖 View 的單向綁定


在之前的博客中 ,

數(shù)據(jù)模型 Model 中的 指定 Field 字段 綁定到 View 視圖中的組件 ,

在實(shí)際案例中 , 將 Student 類中的 String 類型的 name 字段綁定到了 布局文件中的 TextView 組件中 ,

當(dāng) Student#name 字段發(fā)生了改變 ,

對(duì)應(yīng)的 TextView 組件中顯示的內(nèi)容也發(fā)生了相應(yīng)的修改 ;


上述綁定方式可以理解為 單向綁定 ,

因?yàn)?TextView 組件不能修改 , 只能顯示 ,

數(shù)據(jù)模型中的字段修改 , 可以改變 TextView 顯示的內(nèi)容 ;

TextView 組件不能發(fā)起對(duì)數(shù)據(jù)模型的修改 ;

【Jetpack】DataBinding 架構(gòu)組件 ⑤ ( 數(shù)據(jù)模型與視圖雙向綁定 | BaseObservable 實(shí)現(xiàn)雙向綁定 | ObservableField 實(shí)現(xiàn)雙向綁定 )


2、由單向綁定引出雙向綁定


如果 綁定的 數(shù)據(jù)模型 對(duì)應(yīng)的組件是 EditText 文本框 ,

EditText 組件的內(nèi)容可以自行進(jìn)行修改 ,

數(shù)據(jù)模型 可以發(fā)起對(duì) EditText 組件的修改 ,

同時(shí) EditText 也可以發(fā)起對(duì)數(shù)據(jù)模型的修改 ,

那么就會(huì)出現(xiàn)一個(gè) 雙向綁定 的問(wèn)題 ;

【Jetpack】DataBinding 架構(gòu)組件 ⑤ ( 數(shù)據(jù)模型與視圖雙向綁定 | BaseObservable 實(shí)現(xiàn)雙向綁定 | ObservableField 實(shí)現(xiàn)雙向綁定 )





二、BaseObservable 實(shí)現(xiàn)數(shù)據(jù)模型 Model 與視圖 View 雙向綁定



示例代碼 : https://download.csdn.net/download/han1202012/87702558


1、啟用 DataBinding


使用 DataBinding 前 , 必須啟用數(shù)據(jù)綁定 ,

Module 下的 build.gradle 構(gòu)建腳本 中 , 在 " android / defaultConfig " 層級(jí) , 配置

        // 啟用 DataBinding
        dataBinding {
            enabled = true
        }

內(nèi)容 , 即可啟用 數(shù)據(jù)綁定 ;


完整層級(jí)的代碼如下 :

android {
    namespace 'kim.hsl.databinding_demo'
    compileSdk 32

    defaultConfig {
        applicationId "kim.hsl.databinding_demo"
        minSdk 21
        targetSdk 32

        // 啟用 DataBinding
        dataBinding {
            enabled = true
        }
    }
}

2、導(dǎo)入 kotlin-kapt 插件


凡是 在 Kotlin 中使用到注解的情況下 , 都需要導(dǎo)入 kotlin-kapt 插件 ;

在 Module 下的 build.gradle 構(gòu)建腳本中 , 導(dǎo)入 kotlin-kapt 插件 ;

plugins {
    id 'kotlin-kapt'
}

3、數(shù)據(jù)模型類


數(shù)據(jù)類中 , 主要 封裝 數(shù)據(jù)模型 ;

package kim.hsl.databinding_demo

class Student(var name: String, var age: Int) {
}

4、BaseObservable 實(shí)現(xiàn)雙向綁定 ( 本博客的核心重點(diǎn) ) ★


實(shí)現(xiàn) 數(shù)據(jù) 與 視圖 的雙向綁定類 , 需要繼承 BaseObservable 類 ;


class StudentViewModel: BaseObservable {
}

在該類中 , 需要 維護(hù)一個(gè) 數(shù)據(jù)類對(duì)象 , 如下在 次構(gòu)造函數(shù) 中傳入 ;

    lateinit var student: Student

    constructor() {
        this.student = Student("Tom", 18)
    }

實(shí)現(xiàn)一個(gè) getXxx 函數(shù) , 使用 @Bindable 注解修飾該函數(shù) ,

同時(shí) 在 DataBinding 布局中 , 為 EditText 組件設(shè)置值時(shí) , 也使用該函數(shù)設(shè)置值 ;

設(shè)置了 @Bindable 注解 , 只要 student 對(duì)象中的 name 發(fā)生了變化 , 綁定的組件中的內(nèi)容就會(huì)發(fā)生變化 ;

    /**
     * 只要 student 對(duì)象中的 name 發(fā)生了變化
     * 綁定的組件中的內(nèi)容就會(huì)發(fā)生變化
     */
    @Bindable
    fun getStudentName(): String {
        return student.name
    }

如果要實(shí)現(xiàn) 通過(guò) EditText 修改 數(shù)據(jù)模型 的效果 , 需要再實(shí)現(xiàn)一個(gè) setXxx 函數(shù) ,

該函數(shù)需要與之前的 使用 @Bindable 注解修飾的 getXxx 函數(shù)對(duì)應(yīng) , Xxx 必須是一樣的 ;

修改后需要調(diào)用 notifyPropertyChanged(BR.xxx) 通知數(shù)據(jù)模型進(jìn)行變更 ;

    /**
     * 只要綁定的 EditText 組件內(nèi)容發(fā)生變化
     * 就會(huì)自動(dòng)調(diào)用該函數(shù) 修改 student 對(duì)象中的 name 字段
     */
    fun setStudentName(name: String): Unit {
        // 修改后的字符串不為空 且與之前的值不同 才更新數(shù)據(jù)模型數(shù)據(jù)
        if (name != null && !(name == student.name)) {
            student.name = name
            Log.i("StudentViewModel", "setStudentName : ${name}")

            // BR 是編譯時(shí)自動(dòng)生成的字段
            // studentName 對(duì)應(yīng)的是 上面 被 @Bindable 修飾的 getStudentName 函數(shù)
            notifyPropertyChanged(BR.studentName)
        }
    }

BR 類是 BaseObservable 子類中由 @Bindable 注解修飾的函數(shù)生成 ;

BR 類生成位置在 app\build\generated\source\kapt\debug\kim\hsl\databinding_demo\BR.java ;

【Jetpack】DataBinding 架構(gòu)組件 ⑤ ( 數(shù)據(jù)模型與視圖雙向綁定 | BaseObservable 實(shí)現(xiàn)雙向綁定 | ObservableField 實(shí)現(xiàn)雙向綁定 )


BaseObservable 類源碼如下 :

package kim.hsl.databinding_demo

import android.util.Log
import android.view.View
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable

class StudentViewModel: BaseObservable {

    lateinit var student: Student

    constructor() {
        this.student = Student("Tom", 18)
    }

    /**
     * 只要 student 對(duì)象中的 name 發(fā)生了變化
     * 綁定的組件中的內(nèi)容就會(huì)發(fā)生變化
     */
    @Bindable
    fun getStudentName(): String {
        return student.name
    }

    /**
     * 只要綁定的 EditText 組件內(nèi)容發(fā)生變化
     * 就會(huì)自動(dòng)調(diào)用該函數(shù) 修改 student 對(duì)象中的 name 字段
     */
    fun setStudentName(name: String): Unit {
        // 修改后的字符串不為空 且與之前的值不同 才更新數(shù)據(jù)模型數(shù)據(jù)
        if (name != null && !(name == student.name)) {
            student.name = name
            Log.i("StudentViewModel", "setStudentName : ${name}")

            // BR 是編譯時(shí)自動(dòng)生成的字段
            // studentName 對(duì)應(yīng)的是 上面 被 @Bindable 修飾的 getStudentName 函數(shù)
            notifyPropertyChanged(BR.studentName)
        }
    }
}

5、布局文件設(shè)置 ( 重點(diǎn) )


在 DataBinding 布局文件中 , 需要 在 " data / variable " 標(biāo)簽中 , 引入 StudentViewModel 類型的對(duì)象 ;

在位 EditText 組件賦值時(shí) , 需要使用 android:text="@={student.studentName}" 進(jìn)行賦值 , 注意值為 @={student.studentName} , 比之前的數(shù)據(jù)綁定多了一個(gè)等號(hào) ;


布局代碼示例 :

<?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.StudentViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <EditText
            android:id="@+id/imageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@={student.studentName}"
            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>

6、Activity 組件代碼 ( 重點(diǎn) )


在 Activity 組件中 , 向布局中設(shè)置的對(duì)象類型是 StudentViewModel 類型的 , 不是 Student 類型的 ;

package kim.hsl.databinding_demo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
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 = StudentViewModel()
    }
}

7、執(zhí)行結(jié)果


執(zhí)行后顯示 :

【Jetpack】DataBinding 架構(gòu)組件 ⑤ ( 數(shù)據(jù)模型與視圖雙向綁定 | BaseObservable 實(shí)現(xiàn)雙向綁定 | ObservableField 實(shí)現(xiàn)雙向綁定 )

逐個(gè)字母刪除 Tom , 然后輸入 Jack ;

最終打印如下日志 :

setStudentName : To
setStudentName : T
setStudentName : 
setStudentName : Jack

【Jetpack】DataBinding 架構(gòu)組件 ⑤ ( 數(shù)據(jù)模型與視圖雙向綁定 | BaseObservable 實(shí)現(xiàn)雙向綁定 | ObservableField 實(shí)現(xiàn)雙向綁定 )

【Jetpack】DataBinding 架構(gòu)組件 ⑤ ( 數(shù)據(jù)模型與視圖雙向綁定 | BaseObservable 實(shí)現(xiàn)雙向綁定 | ObservableField 實(shí)現(xiàn)雙向綁定 )





三、ObservableField 實(shí)現(xiàn)數(shù)據(jù)模型 Model 與視圖 View 雙向綁定 ( 本博客的核心重點(diǎn) ) ★



示例代碼 :


ObservableField 實(shí)現(xiàn)數(shù)據(jù)模型 Model 與視圖 View 雙向綁定

BaseObservable 實(shí)現(xiàn)數(shù)據(jù)模型 Model 與視圖 View 雙向綁定

進(jìn)行對(duì)比 ,

除了 StudentViewModel 之外 , 其它代碼都一樣 ;

重點(diǎn)介紹 StudentViewModel 類 ;


將數(shù)據(jù)模型類 Student , 定義為 ObservableField 的泛型類 ;

    lateinit var studentObservableField: ObservableField<Student>

在構(gòu)造函數(shù)中 , 創(chuàng)建 Student 對(duì)象 , 將其設(shè)置到 ObservableField<Student> 對(duì)象中 ;

    constructor() {
        var student: Student = Student("Tom", 18)
        studentObservableField = ObservableField()
        studentObservableField.set(student)
    }

定義 getStudentName() 函數(shù) , 獲取 ObservableField<Student> 對(duì)象中的 Student 對(duì)象的 name 屬性 ;

    fun getStudentName(): String? {
        return studentObservableField.get()?.name
    }

定義 setStudentName() 函數(shù) , 設(shè)置 ObservableField<Student> 對(duì)象中的 Student 對(duì)象的 name 屬性 ;

    fun setStudentName(name: String): Unit {
        studentObservableField.get()?.name = name
        Log.i("StudentViewModel", "setStudentName : ${name}")
    }

完整代碼如下 :

package kim.hsl.databinding_demo

import android.util.Log
import androidx.databinding.ObservableField

class StudentViewModel {
    lateinit var studentObservableField: ObservableField<Student>

    constructor() {
        var student: Student = Student("Tom", 18)
        studentObservableField = ObservableField()
        studentObservableField.set(student)
    }

    fun getStudentName(): String? {
        return studentObservableField.get()?.name
    }

    fun setStudentName(name: String): Unit {
        studentObservableField.get()?.name = name
        Log.i("StudentViewModel", "setStudentName : ${name}")
    }
}

執(zhí)行上述代碼 , 也能實(shí)現(xiàn)與 BaseObservable 雙向綁定相同的效果 ;
【Jetpack】DataBinding 架構(gòu)組件 ⑤ ( 數(shù)據(jù)模型與視圖雙向綁定 | BaseObservable 實(shí)現(xiàn)雙向綁定 | ObservableField 實(shí)現(xiàn)雙向綁定 )文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-420395.html

到了這里,關(guān)于【Jetpack】DataBinding 架構(gòu)組件 ⑤ ( 數(shù)據(jù)模型與視圖雙向綁定 | BaseObservable 實(shí)現(xiàn)雙向綁定 | ObservableField 實(shí)現(xiàn)雙向綁定 )的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

  • 【Jetpack】DataBinding 架構(gòu)組件 ② ( 字符串拼接函數(shù) | 綁定點(diǎn)擊事件函數(shù) | DataBinding 布局中使用 import 標(biāo)簽導(dǎo)入 Java、Kotlin 類 )

    【Jetpack】DataBinding 架構(gòu)組件 ② ( 字符串拼接函數(shù) | 綁定點(diǎn)擊事件函數(shù) | DataBinding 布局中使用 import 標(biāo)簽導(dǎo)入 Java、Kotlin 類 )

    在上一篇博客中 , 遇到 在 DataBinding 布局 中 , 向 TextView 組件設(shè)置 int 類型數(shù)據(jù)的情況會(huì)報(bào)錯(cuò) , 最終的處理方式是 將 int 類型的變量 student.age 通過(guò) String.valueOf 函數(shù)轉(zhuǎn)為 字符串 類型 , 設(shè)置到 TextView 組件中 ; 此外 , 還可以 在 數(shù)據(jù)類 中定義 字符串拼接函數(shù) , 直接在 DataBinding 布局

    2023年04月08日
    瀏覽(28)
  • Jetpack業(yè)務(wù)架構(gòu)—四件套(Lifecycle、ViewModel、LiveData、DataBinding)

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

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

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

    android jetpack databinding的基本使用(java)

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

    2024年02月10日
    瀏覽(22)
  • Android Jetpack組件架構(gòu):ViewModel的原理

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

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

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

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

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

    2024年02月01日
    瀏覽(25)
  • 【Qt之模型視圖】1. 模型和視圖架構(gòu)

    【Qt之模型視圖】1. 模型和視圖架構(gòu)

    MVC(Model-View-Control)是一種源自 Smalltalk 的設(shè)計(jì)模式,通常用于構(gòu)建用戶界面。 MVC由三種類型的對(duì)象組成。模型是應(yīng)用對(duì)象,用來(lái)表示數(shù)據(jù);視圖是模型的用戶界面,用來(lái)顯示數(shù)據(jù);控制器定義了用戶界面對(duì)用戶輸入的反應(yīng)方式。在MVC之前,用戶界面設(shè)計(jì)往往將這些對(duì)象混為

    2024年01月18日
    瀏覽(25)
  • 大型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客戶端

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

    2024年02月09日
    瀏覽(27)
  • SpringMVC的架構(gòu)有什么優(yōu)勢(shì)?——視圖與模型(二)

    SpringMVC的架構(gòu)有什么優(yōu)勢(shì)?——視圖與模型(二)

    「作者主頁(yè)」 :雪碧有白泡泡 「?jìng)€(gè)人網(wǎng)站」 :雪碧的個(gè)人網(wǎng)站 「推薦專欄」 : ★ java一站式服務(wù) ★ ★ React從入門(mén)到精通 ★ ★ 前端炫酷代碼分享 ★ ★ 從0到英雄,vue成神之路★ ★ uniapp-從構(gòu)建到提升 ★ ★ 從0到英雄,vue成神之路 ★ ★ 解決算法,一個(gè)專欄就夠了 ★ ★

    2024年02月13日
    瀏覽(22)
  • 《Jetpack Compose從入門(mén)到實(shí)戰(zhàn)》第三章 定制 UI 視圖

    《Jetpack Compose從入門(mén)到實(shí)戰(zhàn)》第三章 定制 UI 視圖

    -ui.theme.Color.kt ui.theme.Type.kt 先將Nunito Sans字體家族放入 res/font,再根據(jù)設(shè)計(jì)稿寫(xiě)代碼 ui.theme/Shape.kt CompositionLocal 是 Jetpack Compose 中的一種數(shù)據(jù)傳遞方式。它可以在組合組件之間傳遞可變數(shù)據(jù),而無(wú)需通過(guò) props 或 state 管理器來(lái)傳遞數(shù)據(jù)。這個(gè)特性比傳統(tǒng)的數(shù)據(jù)傳遞方式更為高效

    2024年02月07日
    瀏覽(19)
  • Vue父子組件間數(shù)據(jù)的雙向綁定

    Vue父子組件間數(shù)據(jù)的雙向綁定

    在vue中數(shù)據(jù)的流向通常是單向的,但是實(shí)際開(kāi)發(fā)中,存在子組件對(duì)父組件值進(jìn)行更新的情況,例如對(duì)表單組件進(jìn)行二次封裝等,父組件需要響應(yīng)子組件的變化。雙向綁定呼之欲出,vue提供了兩種方法進(jìn)行雙向綁定: 在父組件上 v-model 會(huì)利用子組件名為 value 的 prop 和名為 inp

    2024年02月06日
    瀏覽(25)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包