日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android开发之关于MVVM架构中视图数据绑定框架dataBinding的基本用法

發(fā)布時間:2023/12/15 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android开发之关于MVVM架构中视图数据绑定框架dataBinding的基本用法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

dataBinding是Google官方開發(fā)的第三方視圖數(shù)據(jù)綁定框架。優(yōu)缺點如下:

優(yōu)點:很好用

缺點:調(diào)試bug不易,部分AS版本中不太友好

首先說下如何使用:

在gradle中的android模塊中添加 如下命令:

dataBinding {enabled = true}

如下圖

然后將你要綁定的xml布局視圖轉(zhuǎn)換成Binding視圖:

開始將bean對象放到xml布局中

然后通過dataBinding加載此布局:

DataBindingUtil.setContentView(this, R.layout.activity_home);

上面會返回一個dataBinding類型的對象ActivityHomeBinding這里說明下:這個返回的對象的名稱是根據(jù)activity_home名稱決定的。例如xml布局叫activity_page,那么返回的dataBinding類型就為ActivityPageBinding這個規(guī)則了。

然后可以拿到這個返回的對象去設(shè)置數(shù)據(jù)了

activityHomeBinding = DataBindingUtil.setContentView(this, R.layout.activity_home);people.setWork("高級移動金磚工程師");peopleMessage.setAddress("東直門");peopleMessage.setAge(21);peopleMessage.setName("李曉華");people.setPeopleMessage(peopleMessage);activityHomeBinding.setPeople(people);

當然也可以用這個返回的對象去調(diào)用xml布局中的id:例如

activityHomeBinding.tvShowPeople

上面的tvShowPeople就是xml中的tv_show_people這個id

如何更新數(shù)據(jù)呢?

我們需要在xml中這樣寫

重點代碼

<TextViewandroid:id="@+id/tv_show_people"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@{people.peopleMessage.name+':'+people.work+':'+people.peopleMessage.age+':'+people.peopleMessage.address}"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" />

數(shù)據(jù)綁定的規(guī)則:@{},這個大括號里面的就是我們設(shè)置的數(shù)據(jù),people就是上面xml布局中的data標簽中的name字段,people.work代表的是獲取com.example.databindingdemo.bean.People這個路徑下面的bean對象里面的work字段,其它字段同理

因為我們上面已經(jīng)設(shè)置了people的基本參數(shù)了

這樣數(shù)據(jù)就綁定了。項目跑起來就會顯示剛剛設(shè)置的people數(shù)據(jù)了

我們再來看下更新數(shù)據(jù):

首先在xml中定義點擊事件onClick,下面紅框里面的home.Onclick這個方法名稱隨意寫

然后在activity中寫好剛剛定義的Onclick方法:更新數(shù)據(jù)就是重新復(fù)制給people即可,在將重新復(fù)制的people賦值給binding那個對象即可

但是每次都要重新將復(fù)制的people賦值給binding那個對象太麻煩。

所以我們使用新方法,在bean對象里面使用注解:

1.首先類名要繼承BaseObservable

2.在要更新同步數(shù)據(jù)的方法上面添加@Bindable注解

3.刷新數(shù)據(jù)這個方法notifyPropertyChanged(BR.更新的字段);

可能有點亂,但是對比源碼看可能好點:GitHub源碼下載

更新了添加kotlin經(jīng)典綁定代碼示例:

package com.noboauto.module_album.adapterimport android.annotation.SuppressLint import android.view.LayoutInflater import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import com.noboauto.module_album.databinding.AlbumItemFragmentCategoryHomeListBinding import com.noboauto.xm_api.http.data.album.XmAlbum import java.text.DecimalFormat/*** 項目名稱 : Radio* 項目路徑 : com.module_album.adapter* 當前作者 : xiayiye5* 創(chuàng)建時間 : 2021/9/2 13:57*/ class RadioCategoryAdapter(private var categoryItemData: List<XmAlbum>) :RecyclerView.Adapter<RadioCategoryAdapter.RadioCategoryHolder>() {override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RadioCategoryHolder {val albumItemBinding = AlbumItemFragmentCategoryHomeListBinding.inflate(LayoutInflater.from(parent.context),parent,false)return RadioCategoryHolder(albumItemBinding)}override fun onBindViewHolder(holder: RadioCategoryHolder, position: Int) {//使用viewbinding設(shè)置數(shù)據(jù)給bean對象,在xml中拿到bean對象后就可以直接設(shè)置數(shù)據(jù)了holder.albumItemBinding.xmAlbum = categoryItemData[position]//將當前adapter實例對象傳遞給databinding布局,拿到后可以調(diào)用adapter里面的兩個方法 //countNumber和showRadioNumberholder.albumItemBinding.adapter = this@RadioCategoryAdapter}override fun getItemCount(): Int {return if (categoryItemData.isEmpty()) 0 else categoryItemData.size}fun showData(it: List<XmAlbum>) {categoryItemData = itnotifyDataSetChanged()}class RadioCategoryHolder(val albumItemBinding: AlbumItemFragmentCategoryHomeListBinding) :RecyclerView.ViewHolder(albumItemBinding.root) {}fun countNumber(number: Long): String {val format = DecimalFormat("#.##").format(number / 10000f)return if (number < 10000) number.toString() else "${format}萬"}fun showRadioNumber(itemData: XmAlbum): String = "${itemData.free_track_count}集" }

再看下xml布局

<?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><variablename="xmAlbum"type="com.noboauto.xm_api.http.data.album.XmAlbum" /><variablename="adapter"type="com.noboauto.module_album.adapter.RadioCategoryAdapter" /></data><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="15dp"android:background="@color/album_test_color_1"android:padding="15dp"><ImageViewandroid:id="@+id/iv_category_home_list_item_icon"bindingImage="@{xmAlbum.announcer.avatar_url}"android:layout_width="100dp"android:layout_height="100dp"android:src="@drawable/abc_vector_test"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tv_category_home_list_item_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="15dp"android:singleLine="true"android:text="@{xmAlbum.title}"app:layout_constraintStart_toEndOf="@+id/iv_category_home_list_item_icon"app:layout_constraintTop_toTopOf="@+id/iv_category_home_list_item_icon" /><TextViewandroid:id="@+id/tv_category_home_list_item_play_number"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="15dp"android:drawableLeft="@drawable/abc_vector_test"android:gravity="center"android:text="@{adapter.countNumber(xmAlbum.play_count)}"app:layout_constraintBottom_toTopOf="@+id/tv_category_home_list_item_author_name"app:layout_constraintStart_toEndOf="@+id/iv_category_home_list_item_icon"app:layout_constraintTop_toBottomOf="@+id/tv_category_home_list_item_title" /><TextViewandroid:id="@+id/tv_category_home_list_item_des"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="15dp"android:drawableLeft="@drawable/abc_vector_test"android:gravity="center"android:singleLine="true"android:text="@{adapter.showRadioNumber(xmAlbum)}"app:layout_constraintBottom_toBottomOf="@+id/tv_category_home_list_item_play_number"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintTop_toTopOf="@+id/tv_category_home_list_item_play_number" /><TextViewandroid:id="@+id/tv_category_home_list_item_author_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="15dp"android:drawableLeft="@drawable/abc_vector_test"android:singleLine="true"android:text="@{xmAlbum.announcer.nickname}"app:layout_constraintBottom_toBottomOf="@+id/iv_category_home_list_item_icon"app:layout_constraintStart_toEndOf="@+id/iv_category_home_list_item_icon" /></androidx.constraintlayout.widget.ConstraintLayout> </layout>

總結(jié)

以上是生活随笔為你收集整理的Android开发之关于MVVM架构中视图数据绑定框架dataBinding的基本用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。