欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂Android使用LiveData替換ObservableField做Data Binding

2018-01-18 13:46:16 4103瀏覽

AndroidStudio3.1Canary6開始支持使用LiveData做DataBinding。今天扣丁學(xué)堂給大家簡單介紹下使用LiveData替換ObservableField做DataBinding(開發(fā)語言為kotlin)吧。



環(huán)境準(zhǔn)備

1、安裝AndroidStudio3.1Canary6或以上版本。

2、升級AndroidGradle插件

buildscript{

dependencies{

classpath'com.android.tools.build:gradle:3.1.0-alpha06'

}

}

3、更新databinding編譯器的版本

kapt'com.android.databinding:compiler:3.1.0-alpha06'

app的build.gradle如下:

applyplugin:'com.android.application'

applyplugin:'kotlin-android'

applyplugin:'kotlin-kapt'

android{

compileSdkVersion26

defaultConfig{

applicationId"com.example.livedatabinding"

minSdkVersion21

targetSdkVersion26

versionCode1

versionName"1.0"

testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner"

}

buildTypes{

release{

minifyEnabledfalse

proguardFilesgetDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'

}

}

dataBinding{

enabled=true

}

}

dependencies{

implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation'com.android.support:appcompat-v7:26.1.0'

implementation'android.arch.lifecycle:extensions:1.0.0'

kapt'com.android.databinding:compiler:3.1.0-alpha06'

kapt'android.arch.lifecycle:compiler:1.0.0'

testImplementation'junit:junit:4.12'

androidTestImplementation'com.android.support.test:runner:1.0.1'

androidTestImplementation'com.android.support.test.espresso:espresso-core:3.0.1'

}

使用

原來使用ObserverableField的ViewModel

classUsersViewModel:ViewModel(){

privatevaluserRepository=UserRepository()

valname=ObservableField()

valage=ObservableInt()

init{

userRepository.addUser{

name.set(it.name)

age.set(it.age)

}

}

}

改用LiveData

classUserViewModel:ViewModel(){

privatevaluserRepository=UserRepository()

valname=MutableLiveData()

valage=MutableLiveData()

init{

userRepository.updateUserInfo{

name.postValue(it.name)

age.postValue(it.age)

}

}

}

注意:LiveData暴露公開兩個方法用于設(shè)置值

postValue:允許后臺線程向主進(jìn)程推送數(shù)據(jù)

setValue:只允許在主線程調(diào)用,如果在其他線程調(diào)用會報錯:Thismethodmustbecalledfromthemainthread

綁定LiveData并對Binding設(shè)置LifecycleOwer:

classUserActivity:AppCompatActivity(){

overridefunonCreate(savedInstanceState:Bundle?){

super.onCreate(savedInstanceState)

valbinding:ActivityUserBinding?=setContentView(this,R.layout.activity_user)

valviewModel=ViewModelProviders.of(this).get(UserViewModel::class.java)

binding?.let{

it.viewModel=viewModel

it.setLifecycleOwner(this)

}

}

}

由于LiveData會遵從其他應(yīng)用組件(如activity,fragment)的生命周期,它只會在UI組件處在active狀態(tài)(如activity處在started和resumed)時才會推送數(shù)據(jù)。這樣避免了我們UI展示數(shù)據(jù)時,需要檢查下組件是否存在。

以上就是關(guān)于Android使用LiveData替換ObservableField做DataBinding的詳細(xì)介紹,最后想向大家強(qiáng)調(diào)一點,學(xué)編程基礎(chǔ)一定要打牢,所有的概念和知識點必須理解透徹,解更多請關(guān)注扣丁學(xué)堂Android培訓(xùn)官網(wǎng)、微信公眾號平臺,扣丁學(xué)堂Android視頻教程從零基礎(chǔ)到精通免費試聽試學(xué),就業(yè)終端服務(wù)系統(tǒng)隨時監(jiān)控學(xué)員的學(xué)習(xí)情況和效果,順利畢業(yè)后根據(jù)學(xué)員自身風(fēng)格和學(xué)習(xí)效果推薦就業(yè)。如果你想要學(xué)習(xí)Android開發(fā)工程師技術(shù)就不要再猶豫了,喜歡就付諸行動吧。






【關(guān)注微信公眾號獲取更多的學(xué)習(xí)資料】



查看更多關(guān)于“Android開發(fā)技術(shù)的相關(guān)資訊>>

標(biāo)簽: Android開發(fā)從入門到精通 Android學(xué)習(xí)路線圖 Android培訓(xùn) Android開發(fā)工程師 Android視頻教程

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部