最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在Android中如何使用DataBinding詳解(Kotlin)

 更新時間:2020年11月09日 11:59:22   作者:simon大佬  
這篇文章主要給大家介紹了關(guān)于在Android中如何使用DataBinding(Kotlin)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

本問主要介紹DataBinding在Android App中的使用方法。數(shù)據(jù)綁定是將“提供器”的數(shù)據(jù)源與“消費者”綁定并使其同步的一種通用技術(shù)。

1. Android應用程序使用數(shù)據(jù)綁定

1.1 介紹DataBinding

Android通過DataBinding提供了編寫聲明型布局的支持。這樣可以最大程度簡化布局和邏輯相關(guān)聯(lián)的代碼。
數(shù)據(jù)綁定要求修改文件,外層需要包裹一個layout布局。主要通過@{} 或 @={}語法把布局中的元素和表達式的引用寫入到屬性中。

<?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="mainModel"
   type="me.ithome.jetpack.model.MainViewModel" />①
 </data>

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

  <TextView
   android:id="@+id/tv_userinfo"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@{mainModel.userData.toString()}"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintLeft_toLeftOf="parent"
   app:layout_constraintRight_toRightOf="parent"
   app:layout_constraintTop_toTopOf="parent" />

  <Button
   android:id="@+id/button"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginTop="56dp"
   android:onClick="@{(view) -> mainModel.getClick(view)}"
   android:text="@string/btn_getUserInfo"
   app:layout_constraintBottom_toTopOf="@+id/tv_userinfo"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintHorizontal_bias="0.498"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toTopOf="parent" />

 </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

①用戶變量,定義了能在這個布局里面使用的屬性和類

②常規(guī)布局

DataBinding會基于layout創(chuàng)建一個Binding class,這個類包含了布局屬性(定義的變量)到相關(guān)視圖的所有綁定,并且會為布局中的數(shù)據(jù)元素生成setter,生成的類的名稱是基于layout的名稱(駝峰命名,加上Binding后綴)。比如布局名是activity_main.xml,生成的類就是ActivityMainBinding。你能通過這個類去inflate布局和數(shù)據(jù)模型,也可以通過DataBindingUtil類。

DataBindingUtils加載布局

val mainBindingUtil = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
mainBindingUtil.lifecycleOwner = this

inflate加載布局(此方法也能用于RecyclerView, ViewPager)

val mainBindingUtil = ActivityMainBinding.inflate(layoutInflater)
setContentView(mainBindingUtil.root)

上述兩種方法大家二選一,一般在Activity中我們都用第一種。

1.2 如何啟用DataBinding

想要在Android App工程中使用databinding,只需要在app/build.gradle文件中設置如下代碼:

android {
 ....
 dataBinding {
  enabled = true
 }
}

1.3 DataBinding點擊事件的處理

布局的處理除了數(shù)據(jù)的傳遞,還有點擊事件的處理。

使用方式和普通方法調(diào)用一樣。比如我在MainViewModel.kt中定義了getClick方法

fun getClick(v: View) {
 //TODO
}

現(xiàn)在我想在Button點擊的時候調(diào)用getClick方法,只需要在布局文件中添加下面的代碼

android:onClick="@{(view) -> mainModel.getClick(view)}"

如果不需要參數(shù),可以直接

android:onClick="@{() -> mainModel.getClick()}"

如果有其他參數(shù),對應的添加參數(shù)列表

android:onClick="@{() -> mainModel.getClick(args)}"

其他比如onLongClick之類的處理都是同理。

1.4 import的使用

可以通過import的方式導入類,直接調(diào)用類的靜態(tài)方法。

<data>
 <import type="me.ithome.jetpack.utils.StringUtils" />
 <variable
  name="mainModel"
  type="me.ithome.jetpack.model.MainViewModel" />
</data>

<TextView
 android:text="@{StringUtils.capitalize(mainModel.userData.name)}"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>

1.5 數(shù)據(jù)實時刷新

當viewmodel的數(shù)據(jù)發(fā)生變化后,我們希望布局也同時刷新,有個非常簡單的方法,不需要繼承BaseObservable,我們通過引入LiveData來實現(xiàn)。

open class MainViewModel : ViewModel() {

  var userData: MutableLiveData<UserInfo> = MutableLiveData()

  init {
    getUserInfo()
  }

  private fun getUserInfo() {
    val user = UserInfo("李四", (10..50).random())
    userData.postValue(user)  //數(shù)據(jù)發(fā)生變化后,調(diào)用postValue,無需通過observe監(jiān)聽,布局數(shù)據(jù)會自動刷新
  }

  fun getClick(v: View) {
    getUserInfo()
  }
}

1.6 使用BindingAdapter

可以通過BindingAdapter這個注解來實現(xiàn)屬性值變化的時候,控件狀態(tài)也跟著變化,比如圖片ImageView,當url變化的時候,控件會跟著顯示不同的圖片。

需要在靜態(tài)類里面定義一個靜態(tài)方法:

object StringUtils {

  @BindingAdapter("android:src")
  @JvmStatic fun loadImage(view: ImageView, url: String) {
    MyApplication._context?.let {
      Glide.with(it)
        .load(url)
        .into(view)
    }
  }
}

注意這里的android:src,這個可以直接指定控件的屬性,也可以自己定義屬性。

<ImageView
      android:id="@+id/imageView"
      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/tv_userinfo"
      android:src="@{mainModel.imageUrl}"
      tools:srcCompat="@tools:sample/avatars" />

loadImage方法綁定的是android:src這個屬性,所以當這個屬性的值變化時會把view和url傳遞到loadImage。

如果是綁定的自定義字段呢?比如我現(xiàn)在綁定了一個自定義的url。

@BindingAdapter("url")
@JvmStatic fun loadImage(view: ImageView, url: String) {
...
}

那么布局文件就這么寫

<ImageView
      android:id="@+id/imageView"
      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/tv_userinfo"
      app:url="@{mainModel.imageUrl}"
      tools:srcCompat="@tools:sample/avatars" />

總結(jié)

前面主要是寫了databinding的一些基本用法,擴展用法還比較多,我們后續(xù)再接著說。

到此這篇關(guān)于在Android中如何使用DataBinding(Kotlin)的文章就介紹到這了,更多相關(guān)Android使用DataBinding(Kotlin)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android天氣預報app改進版

    Android天氣預報app改進版

    這篇文章主要為大家詳細介紹了改進版的Android天氣預報app,內(nèi)容更加充實,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android多線程之同步鎖的使用

    Android多線程之同步鎖的使用

    本篇文章主要介紹了Android多線程之同步鎖的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Android 自定義EditText輸入框帶清空按鈕

    Android 自定義EditText輸入框帶清空按鈕

    這篇文章主要介紹了Android 自定義EditText輸入框帶清空按鈕的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Android開發(fā)筆記XML數(shù)據(jù)解析方法及優(yōu)缺點

    Android開發(fā)筆記XML數(shù)據(jù)解析方法及優(yōu)缺點

    XML數(shù)據(jù)是一種常見的數(shù)據(jù)格式,Android開發(fā)中需要對其進行解析。常用的XML解析方式有DOM、SAX、Pull和Json等,每種方式都有其優(yōu)缺點。開發(fā)者可以根據(jù)具體需求選擇合適的解析方式,提高數(shù)據(jù)解析效率和性能
    2023-05-05
  • Android自定義View實現(xiàn)水波紋引導動畫

    Android自定義View實現(xiàn)水波紋引導動畫

    這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)水波紋動畫引導,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Flutter進階之實現(xiàn)動畫效果(九)

    Flutter進階之實現(xiàn)動畫效果(九)

    這篇文章主要為大家詳細介紹了Flutter進階之實現(xiàn)動畫效果的第九篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android 中 ActivityLifecycleCallbacks的實例詳解

    Android 中 ActivityLifecycleCallbacks的實例詳解

    這篇文章主要介紹了Android 中 ActivityLifecycleCallbacks的實例詳解的相關(guān)資料,希望通過本文大家能掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • Android檢測手機多點觸摸點數(shù)的方法

    Android檢測手機多點觸摸點數(shù)的方法

    這篇文章主要為大家詳細介紹了Android檢測手機多點觸摸點數(shù)的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android Selector獲取焦點后文本背景修改的實現(xiàn)代碼

    Android Selector獲取焦點后文本背景修改的實現(xiàn)代碼

    這篇文章主要介紹了Android Selector獲取焦點后文本背景修改的實現(xiàn)代碼,本文通過demo展示和實現(xiàn)代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-11-11
  • Android中TextView局部變色功能實現(xiàn)

    Android中TextView局部變色功能實現(xiàn)

    這篇文章給大家詳細講解了一下Android中TextView實現(xiàn)部分文字不同顏色的功能實現(xiàn)過程,有這方面需要的朋友們一起學習下吧。
    2017-12-12

最新評論

西乡县| 福贡县| 江津市| 新蔡县| 杂多县| 金湖县| 新晃| 雷山县| 集安市| 东方市| 武隆县| 三门县| 二连浩特市| 桃园县| 蓝山县| 谢通门县| 涪陵区| 通州市| 扶绥县| 论坛| 怀化市| 重庆市| 昌邑市| 昌吉市| 嘉兴市| 淮阳县| 新兴县| 绵阳市| 阳江市| 商都县| 田林县| 溧水县| 太仆寺旗| 芜湖县| 黑水县| 新田县| 尚志市| 乌苏市| 灌南县| 元江| 吕梁市|