引入依赖
def lifecycle_version ="2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
创建实体类继承ViewModel
class KlineViewModel: ViewModel() {
var name:String?
var age:Int?
}
实例化viewModel
val klineViewModel by lazy { ViewModelProviders.of(this).get(KlineViewModel::class.java) }
操作ViewModel的数据
klineViewModel.name
网友评论