如何在kotlin中获得价值?
我记得在kotlin语言中,有一个通过get属性获取值的选项,但找不到如何编写它 我的意思是:我的ViewModel中有一个LiveData,我需要访问post-in-LiveData的权限只有ViewModel和outside-just选项来获取订阅 我现在是如何实现它的如何在kotlin中获得价值?,kotlin,Kotlin,我记得在kotlin语言中,有一个通过get属性获取值的选项,但找不到如何编写它 我的意思是:我的ViewModel中有一个LiveData,我需要访问post-in-LiveData的权限只有ViewModel和outside-just选项来获取订阅 我现在是如何实现它的 class MyViewModel(ctx: Context) : AndroidViewModel(ctx as Application) { private val _showLoadingPB = Single
class MyViewModel(ctx: Context) : AndroidViewModel(ctx as Application)
{
private val _showLoadingPB = SingleLiveEvent<Boolean>()
fun showLoadingPB(): SingleLiveEvent<Boolean>
{
return _showLoadingPB
}
...
}
但我记得有一个选项可以这样写
class MyViewModel(ctx: Context) : AndroidViewModel(ctx as Application)
{
private val _showLoadingPB = SingleLiveEvent<Boolean>()
val showLoadingPB: SingleLiveEvent<Boolean>
get() => _showLoadingPB
}
如何使它工作?我记得它应该是怎样的
class MyViewModel(ctx: Context) : AndroidViewModel(ctx as Application)
{
private val _showLoadingPB = SingleLiveEvent<Boolean>()
val showLoadingPB: LiveData<Boolean>
get() = _showLoadingPB
}
这样,用户就不能为SingleLiveEvent分配新值,也不能在LiveData中发布新事件,他只能观察它