android-[Transformation]

FangShiRui 2020-06-04 PM 2470℃ 0条

简介

用于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()方法

标签: none

非特殊说明,本博所有文章均为博主原创。

评论已关闭