Dam*_*tes 6 android android-livedata android-viewmodel
我在UI中有一个值,它的值取决于两个LiveData对象.想象一下你需要a subtotal = sum of all items price和a 的商店total = subtotal + shipment price.使用转换,我们可以对小计LiveData对象执行以下操作(因为它只依赖于itemsLiveData):
val itemsLiveData: LiveData<List<Items>> = ...
val subtotalLiveData = Transformations.map(itemsLiveData) {
items ->
getSubtotalPrice(items)
}
Run Code Online (Sandbox Code Playgroud)
在总数的情况下,能够做这样的事情会很棒:
val shipPriceLiveData: LiveData<Int> = ...
val totalLiveData = Transformations.map(itemsLiveData, shipPriceLiveData) {
items, price ->
getSubtotalPrice(items) + price
}
Run Code Online (Sandbox Code Playgroud)
但是,遗憾的是,这是不可能的,因为我们不能在map函数中放置多个参数.谁知道实现这个目标的好方法?
Sha*_*hmi 11
您可以在这种情况下使用switchMap(),因为它返回 LiveData 对象,该对象可以是Transformations.map()
在下面的代码我得到两个对象的最终量的总和onwardSelectQuote和returnSelectQuote
finalAmount = Transformations.switchMap(onwardSelectQuote) { data1 ->
Transformations.map(returnSelectQuote) { data2 -> ViewUtils.formatRupee((data1.finalAmount!!.toFloat() + data2.finalAmount!!.toFloat()).toString())
}
}
Run Code Online (Sandbox Code Playgroud)
Jos*_*hua 10
我想出了另一个解决方案。
class PairLiveData<A, B>(first: LiveData<A>, second: LiveData<B>) : MediatorLiveData<Pair<A?, B?>>() {
init {
addSource(first) { value = it to second.value }
addSource(second) { value = first.value to it }
}
}
class TripleLiveData<A, B, C>(first: LiveData<A>, second: LiveData<B>, third: LiveData<C>) : MediatorLiveData<Triple<A?, B?, C?>>() {
init {
addSource(first) { value = Triple(it, second.value, third.value) }
addSource(second) { value = Triple(first.value, it, third.value) }
addSource(third) { value = Triple(first.value, second.value, it) }
}
}
fun <A, B> LiveData<A>.combine(other: LiveData<B>): PairLiveData<A, B> {
return PairLiveData(this, other)
}
fun <A, B, C> LiveData<A>.combine(second: LiveData<B>, third: LiveData<C>): TripleLiveData<A, B, C> {
return TripleLiveData(this, second, third)
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以组合多个源。
val totalLiveData = Transformations.map(itemsLiveData.combine(shipPriceLiveData)) {
// Do your stuff
}
Run Code Online (Sandbox Code Playgroud)
如果你想有 4 个或更多的源,你需要创建你自己的数据类,因为 Kotlin 只有 Pair和Triple。
在我看来,没有理由uiThread在 Damia 的解决方案中运行。
最后,我使用MediatorLiveData来实现相同的目标.
fun mapBasketTotal(source1: LiveData<List<Item>>, source2: LiveData<ShipPrice>): LiveData<String> {
val result = MediatorLiveData<String>()
uiThread {
var subtotal: Int = 0
var shipPrice: Int = 0
fun sumAndFormat(){ result.value = format(subtotal + shipPrice)}
result.addSource(source1, { items ->
if (items != null) {
subtotal = getSubtotalPrice(items)
sumAndFormat()
}
})
result.addSource(source2, { price ->
if (price != null) {
shipPrice = price
sumAndFormat()
}
})
}
return result
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2940 次 |
| 最近记录: |