当前位置:网站首页>LiveData : Transformations.map and Transformations.switchMap usage
LiveData : Transformations.map and Transformations.switchMap usage
2022-08-10 00:32:00 【Ape Xiaoshuai 01】
项目背景 : 某个int值,在APage needs to be obtained it showsint数值,在BPage need toint值转换为String字符串
1. Transformations.map
基于此背景,This article introduceTransformations.map和Transformations.switchMap,数据源A监听数据源B的内容,Changes will beB
The content of the other contents into corresponding and notice toAData source of the observer.
案例
class CarViewModel : ViewModel() {
// 数据源 B
private var initialData = MutableLiveData<Int>()
/** * @describe 数据源A */
private var transformLiveData = Transformations.map(getInitialData()) {
when (it) {
0 -> "on"
else -> "off"
}
}
fun getInitialData(): MutableLiveData<Int> {
return initialData
}
}
2.Transformations.switchMap
数据源A监听数据源B的内容的变化, Change fromBContent to obtain the corresponding data sourceC, 添加到A的监听列表
即 A 同时监听B和C ,BThe change of the content will only(switch)更换AListen to the list ofC,CThe change of the content will informA监听器
class CarViewModel : ViewModel() {
// 数据源B
private val userLivedata = MutableLiveData<User>()
// 数据源C1
private val cFirstLiveData = MutableLiveData<Int>()
// 数据源C2
private val cSecondLiveData = MutableLiveData<Int>()
/** * 数据源A */
val mapLiveData = Transformations.switchMap(userLivedata) {
when (it.name) {
"test" -> cFirstLiveData
else -> cSecondLiveData
}
}
data class User(val name: String = "test", var age: Int = 18)
}
3.MediatorLiveData
合并两个LiveData
var count = 0
// A 数据
private val liveData1 = MutableLiveData<String>()
// B 数据
private val liveData2 = MutableLiveData<Int>()
val liveCombind = MediatorLiveData<String>()
init {
liveCombind.addSource(liveData1) {
addData()
}
liveCombind.addSource(liveData2) {
addData()
}
}
private fun addData() {
count++
if (count == 10) {
count = 0
//TODO
}
}
边栏推荐
- 五分钟商学院(基础---商业篇)
- 打包报错 AAPT: error: failed to read PNG signature: file does not start with PNG signature.
- 2022牛客暑期多校训练营6(ABGIJM)
- Gartner全球集成系统市场数据追踪,超融合市场增速第一
- 【实用工具系列】MathCAD入门安装及快速上手使用教程
- [WeChat applet development (8)] Summary of audio background music playback problems
- What is the stability of the quantitative trading interface system?
- Pytorch分布式训练/多卡训练DDP——模型初始化(torch.distribute 与 DDP的区别)
- 国内BI厂商一览
- 直播预告 | ICML 2022 11位一作学者在线分享神经网络,图学习等前沿研究
猜你喜欢
随机推荐
正则表达式的实际使用
1018.值周
联盟链技术应用的难点
【实用工具系列】MathCAD入门安装及快速上手使用教程
Users should clearly know that quantitative trading is not a simple procedure
异常处理(try,catch,finally)
伦敦银行情中短线的支撑和阻力位
函数习题(下)
PyQt5: Getting Started Tutorial
高数_复习_第4章:向量代数和空间解析几何
用户要清晰知道,量化交易并非简单的程序
都在说云原生,那云原生到底是什么?
mysql中的key是怎么用的,或者这个值有什么意义,如下图?
CV review: softmax code implementation
linux上使用docker安装redis
setter与getter访问器属性——数据驱动显示
三:OpenCV图片颜色通道数据转换
String类常用方法
68.qt quick-qml多级折叠下拉导航菜单 支持动态添加/卸载 支持qml/widget加载等
【面试高频题】可逐步优化的链表高频题









