当前位置:网站首页>LiveData : Transformations.map和 Transformations.switchMap用法
LiveData : Transformations.map和 Transformations.switchMap用法
2022-08-09 22:11:00 【猿小帅01】
项目背景 : 某个int值,在A页面需要获取它的显示int数值,在B页面需要需要将int值转换为String字符串
1. Transformations.map
基于此背景,此文介绍下Transformations.map和Transformations.switchMap,数据源A监听数据源B的内容,变化时将B
的内容转化为相应的其他内容并且通知给A数据源的观察者。
案例
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的内容的变化, 变化时从B内容获取相应的数据源C, 添加到A的监听列表
即 A 同时监听B和C ,B内容的变化只会(switch)更换A监听列表里的C,C内容的变化才会通知A监听器
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
}
}
边栏推荐
- 【Burning】It's time to show your true strength!Understand the technical highlights of the 2022 Huawei Developer Competition in one article
- 2022/8/9 考试总结
- c:forEach varStatus属性
- [WeChat applet development (8)] Summary of audio background music playback problems
- CGLIB源码易懂解析
- daemon
- CV复习:softmax代码实现
- ElasticSearcch集群
- 对象深复制,面试题
- Interfering with BGP routing---community attributes
猜你喜欢
【实用工具系列】MathCAD入门安装及快速上手使用教程
【Burning】It's time to show your true strength!Understand the technical highlights of the 2022 Huawei Developer Competition in one article
月薪5K的运维小白如何成为月薪5W的高级架构师?
Install win7 virtual machine in Vmware and related simple knowledge
ElasticSearcch集群
毕昇编译器优化:Lazy Code Motion
Interfering with BGP routing---community attributes
DXF笔记:文字对齐的研究
The 2022-8-9 sixth group of input and output streams
高手这样看现货白银走势图
随机推荐
Bi Sheng Compiler Optimization: Lazy Code Motion
matplotlib散点图自定义坐标轴(文字坐标轴)
shell数组
leetcode:321. 拼接最大数
2022-08-09 mysql/stonedb-子查询性能提升-概论
国内BI厂商一览
1018.值周
Janus Official DEMO Introduction
Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut
JS中表单操作、addEventListener事件监听器
pip 离线到内网安装包
金仓数据库 KingbaseGIS 使用手册(6.2. 管理函数)
&&、||、&、|
Leetcode 530. 二叉搜索树的最小绝对差
打包报错 AAPT: error: failed to read PNG signature: file does not start with PNG signature.
Redis集群
linux上使用docker安装redis
请讲一讲JS中的 for...in 与 for...of (上)
量化交易接口系统有哪些稳定性?
【对象——对象及原型链上的属性——对象的操作方法】