假设有2个类别:
class MyLiveData:LiveData<Int>()
class MyMutableLiveData:MutableLiveData<Int>()
允许从MutableLiveData
到LiveData
进行铸造:
val ld1=MutableLiveData<Int>()
val ld2:LiveData<Int> = ld1 //ok
但是您不能通过这种方式强制转换自己的实现:
val mutable=MyMutableLiveData()
val immutable:MyLiveData = mutable //type missmatch
我了解MutableLiveData扩展了LiveData,这就是它们可转换的原因。但是我不能MyMutableLiveData
扩展MyLiveData
,因为在这种情况下它是不可变的
有没有解决方法?
UPD :我想我需要表现出扩展LiveData
的动机。我正在尝试实现MutableLiveDataCollection
,它不仅通过setValue/postValue
通知价值变化,而且还通知价值修改,例如添加新元素。我很惊讶对此没有本机解决方案。
无论如何要避免modify
事件,都必须有其他观察方法。而且该方法必须位于LiveDataCollection
不变部分内,因为视图会调用它。继承是恕我直言的自然解决方案。
答案 0 :(得分:0)
关键思想位于MutableLiveData
类中。该类唯一要做的就是更改setValue/postValue
方法上的访问修饰符。我可以做同样的事情,因此最终代码将是:
open class LiveDataCollection<K,
L:MutableCollection<K>,
M:Collection<K>>: LiveData<L>() {
private var active=false
private var diffObservers = ArrayList<Observer<M>>()
fun observe(owner: LifecycleOwner, valueObserver: Observer<L>, diffObserver: Observer<M>) {
super.observe(owner,valueObserver)
diffObservers.add(diffObserver)
}
protected open fun addItems(toAdd:M) {
value?.addAll(toAdd)
if (active)
for (observer in diffObservers)
observer.onChanged(toAdd)
}
override fun removeObservers(owner: LifecycleOwner) {
super.removeObservers(owner)
diffObservers= ArrayList()
}
override fun onActive() {
super.onActive()
active=true
}
override fun onInactive() {
super.onInactive()
active=false
}
}
class MutableLiveDataCollection<K,L:MutableCollection<K>,
M:Collection<K>>: LiveDataCollection<K,L,M>() {
public override fun addItems(toAdd:M) {
super.addItems(toAdd)
}
public override fun postValue(value: L) {
super.postValue(value)
}
public override fun setValue(value: L) {
super.setValue(value)
}
}