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
- Android Jetpack Architecture原理之ViewModelAndroidJetpackView
- 【譯】Android Architecture - ViewModel 與 View 的通訊AndroidView
- Architecture
- 淺談ViewModelView
- Jetpack-ViewModelJetpackView
- Guide to app architectureGUIIDEAPP
- transformer model architectureORM
- 14.4 InnoDB Architecture
- Android Jetpack之ViewModelAndroidJetpackView
- 當Koin撞上ViewModelView
- Android Jetpack 之 ViewModelAndroidJetpackView
- ViewModel 原始碼分析View原始碼
- 1 Introduction to the Multitenant ArchitectureNaN
- 13-Architecture-nodes
- The option-critic architecture
- [譯]Spring Security ArchitectureSpring
- SparkCore-Architecture-5Spark
- Clean architecture for the rest of usREST
- 前端的Clean Architecture前端
- LiveData && ViewModel使用詳解LiveDataView
- Android-ViewModel 使用指北AndroidView
- Android ViewModel 引入協程AndroidView
- WPF datagrid scrollintoview in ViewModel of MVVMViewMVVM
- Jetpack的ViewModel與LiveDataJetpackViewLiveData
- 【譯】使用 Android Architecture ComponenAndroid
- 當Dagger2撞上ViewModelView
- LiveData+ViewModel+Repository搭建MVVMLiveDataViewMVVM
- WPF ListBox scrollintoview in ViewModel via behaviorView
- 【Android Jetpack教程】ViewModel原理分析AndroidJetpackView
- Android Architecture Components 之 Room 篇AndroidOOM
- Architecture(2)Volley原始碼分析原始碼
- 14-Architecture-Master-Node CommunicationAST
- 15-Architecture-Cloud Controller ManagerCloudController
- Lecture 02 Layered Architecture of Game EngineGAM
- LiveData + ViewModel + Room (Google 官文)+DemoLiveDataViewOOMGo
- 剖析 Android 架構元件之 ViewModelAndroid架構元件View
- Android 架構元件 - Lifycycle, LiveData, ViewModelAndroid架構元件LiveDataView