如何在自定义视图中观察LiveData

5 android kotlin android-livedata

我应该如何在自定义视图中观察 LiveData。我尝试将其上下文转换为生命周期所有者,但它会产生一些问题,并且并非在所有情况下都有效。我尝试放置一个设置器,但它也不起作用

Ami*_*min 14

视图本身没有生命周期。我个人使用了 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

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


另一种选择是将我们的自定义视图转变为 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}
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您更喜欢第一种方法,另一种选择可以结合这两种想法,使其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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)