如何将自定义MutableLiveData强制转换为自定义LiveData?

时间:2019-09-13 13:46:19

标签: kotlin android-livedata mutablelivedata

假设有2个类别:

class MyLiveData:LiveData<Int>()

class MyMutableLiveData:MutableLiveData<Int>()

允许从MutableLiveDataLiveData进行铸造:

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不变部分内,因为视图会调用它。继承是恕我直言的自然解决方案。

1 个答案:

答案 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)
    }
}