简介

用于LiveData的数据转换。它是实时,自动执行的,当LiveData中的value变化时,它便自动转换。
它的最大作用是,实时变换LiveData中的数据为方便展示的数据,例如将数字转化为指定格式的字符串。


map()方法

在viewModel中

private val _currentTime = MutableLiveData<Long>()
init{
    _currentTime.value = 90L
}
val currentTimeString = Transformations.map(_currentTime) { time ->
    DateUtils.formatElapsedTime(time)
}

// currentTimeString 为 "01:30"

switchMap()方法

最后修改:2020 年 06 月 04 日
如果觉得我的文章对你有用,请随意赞赏