其他分享
首页 > 其他分享> > android viewmodel 带参数

android viewmodel 带参数

作者:互联网

需要实现 ViewModelProvider.Factory

class MyModel(val id: String) : ViewModel() {

    class ViewModeFactory(private val id: String) : ViewModelProvider.Factory {

        override fun <T : ViewModel?> create(modelClass: Class<T>): T {
            return MyModel(id) as T
        }

    }
}

 使用我们创建的model

val myModel = ViewModelProvider(this,
    MyModel.ViewModeFactory(id)
 )[MyModel::class.java]

 

标签:val,MyModel,viewmodel,Factory,class,ViewModelProvider,参数,android,id
来源: https://www.cnblogs.com/rchao/p/13054969.html