Architecture–ViewModel
1). 簡介
ViewModel類旨在以生命週期意識的方式儲存和管理UI相關資料。 ViewModel類允許資料在配置更改(例如螢幕旋轉)後繼續存在。
Android框架管理UI控制器的生命週期,例如活動和片段。 框架可以決定銷燬或重新建立UI控制器以響應完全不受您控制的某些使用者動作或裝置事件。
2). 依賴
官方依賴
dependencies {
def lifecycle_version = "1.1.1"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
// alternatively - just ViewModel
implementation "android.arch.lifecycle:viewmodel:$lifecycle_version" // use -ktx for Kotlin
// alternatively - just LiveData
implementation "android.arch.lifecycle:livedata:$lifecycle_version"
// alternatively - Lifecycles only (no ViewModel or LiveData).
// Support library depends on this lightweight import
implementation "android.arch.lifecycle:runtime:$lifecycle_version"
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" // use kapt for Kotlin
// alternately - if using Java8, use the following instead of compiler
implementation "android.arch.lifecycle:common-java8:$lifecycle_version"
// optional - ReactiveStreams support for LiveData
implementation "android.arch.lifecycle:reactivestreams:$lifecycle_version"
// optional - Test helpers for LiveData
testImplementation "android.arch.core:core-testing:$lifecycle_version"
}
但在此處我在Android Studio中未新增任何依賴,lifecyle包在Android Studio3.0之後就預設依賴了
3). 實現
/**
* 自定義ViewModel
* Created by mazaiting on 2018/7/25.
*/
class MyViewModel : ViewModel() {
var users: MutableLiveData<List<User>>? = null
get() {
if (null == field) {
users = MutableLiveData()
loadUsers()
}
return field
}
/**
* 載入資料
*/
private fun loadUsers() {
val userList = arrayListOf<User>()
(0..10).map { userList.add(User("first$it", "last$it")) }
users?.value = userList
}
}
4). 使用
btn_model_get_data.setOnClickListener {
val model: MyViewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(MyViewModel::class.java)
model.users?.observe(this, Observer { users ->
users?.forEach {
L.e(it.toString())
}
})
}
5). 解決的問題
- 解決Activity橫豎屏切換時資料丟失的問題
- 簡化Fragment之間資料傳遞,資料冗餘的問題
6). 原文地址
7). 程式碼下載
相關文章
- Architecture -- ViewModelView
- Google Architecture ViewModelGoView
- [譯] Architecture Components 之 ViewModelView
- Android Jetpack Architecture原理之ViewModelAndroidJetpackView
- 【譯】Android Architecture - ViewModel 與 View 的通訊AndroidView
- Architecture
- Jetpack-ViewModelJetpackView
- 淺談ViewModelView
- HDFS Architecture
- Process Architecture
- Oracle ArchitectureOracle
- website architectureWeb
- The Architecture of NginxNginx
- [譯] Architecture Components 之 Guide to App ArchitectureGUIIDEAPP
- 當Koin撞上ViewModelView
- ViewModel 原始碼分析View原始碼
- SAP MDM Architecture
- Android Jetpack 之 ViewModelAndroidJetpackView
- Android Jetpack之ViewModelAndroidJetpackView
- The option-critic architecture
- 13-Architecture-nodes
- SparkCore-Architecture-5Spark
- 前端的Clean Architecture前端
- 14.4 InnoDB Architecture
- Guide to app architectureGUIIDEAPP
- Clean architecture for the rest of usREST
- Vuforia Architecture(高通元件)元件
- Optimal Flexible ArchitectureFlex
- HDFS-Architecture剖析
- Overview of Multitier Architecture (276)View
- transformer model architectureORM
- LiveData && ViewModel使用詳解LiveDataView
- Android ViewModel 引入協程AndroidView
- Android-ViewModel 使用指北AndroidView
- Jetpack的ViewModel與LiveDataJetpackViewLiveData
- 1 Introduction to the Multitenant ArchitectureNaN
- [譯]Spring Security ArchitectureSpring
- Samza文件翻譯 : Architecture