如何在自定义视图中观察 LiveData [关闭]

问题描述 投票:0回答:1

我应该如何在自定义视图中观察 LiveData。我试图将它的上下文转换为 lifecycleOwner 但它会产生一些问题并且在所有情况下都不起作用。 我试着放一个二传手,但它也不管用

android kotlin android-livedata
1个回答
13
投票

View 本身没有生命周期。我个人使用了 3 种方法,它们实际上是同一件事,但其中一种是添加生命周期,而其他的则没有生命周期。

class MyCustomView  @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
): View(context, attrs, defStyleAttr){
    
    
    val myObserver = Observer<Long>{
        //whatever
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        liveData.observeForever(myObserver)
    }

    override fun onDetachFromWindow() {
        super.onDetachFromWindow()
        liveData.removeObserver(myObserver)
    }
}

此方法在附加/分离到窗口时手动观察/删除。当我观察很少的实时数据并且它很简单/有限

时,我更喜欢它

另一个选择是将我们的自定义视图变成 LifecycleOwner。我推荐将此方法用于 BaseCustomViews 和一些极其庞大和复杂的视图(如地图导航视图)。另外,请记住,您需要手动通知视图其父视图被销毁(您可以直接调用该方法或使用视图树导航为所有 BaseCustomView 的子视图调用此函数)。

abstract class BaseCustomView  @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
): View(context, attrs, defStyleAttr), LifecycleOwner {
    protected val lifecycleRegistry = LifecycleRegistry(this);

    override fun getLifecycle() = lifecycleRegistry
    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        lifecycleRegistry.currentState = Lifecycle.State.RESUMED
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        lifecycleRegistry.currentState = Lifecycle.State. CREATED
    }

    @CallSuper
    open fun destroyLifecycle(){
        lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
    }

    
    val myObserver = Observer<Long>{
        //whatever
    }

    init{
        liveData.observe(this, myObserver}
    }
}

如果你更喜欢第一种方法,另一种选择可以结合这两个想法,有一个

BaseCustomView
它的孩子可以轻松观察LiveData。

abstract class BaseCustomView  @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
): RelativeLayout(context, attrs, defStyleAttr) {

    //a list to hold the list of observers and their LiveData
    private val autoObservers = ArrayList<Pair<LiveData<*>, Observer<*>>>()

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        for((liveData, observer) in autoObservers){
            liveData.observeForever(observer as Observer<in Any>)
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        for((liveData, observer) in autoObservers){
            liveData.removeObserver(observer as Observer<in Any>)
        }
    }

    protected fun<T : Any> LiveData<T>.observe( observer: Observer<T> ){
        autoObservers.add(this to observer)

        //if it's not attached, onAttachedToWindow will do the observation
        if(isAttachedToWindow){
            observeForever(observer)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.