在没有 LifecycleOwner 的自定义视图中设置 LiveData 观察者 [英] Setting up LiveData observer in custom view without LifecycleOwner

查看:90
本文介绍了在没有 LifecycleOwner 的自定义视图中设置 LiveData 观察者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试新的 Android 架构组件,但在尝试将 MVVM 模型用于自定义视图时遇到了障碍.

I'm trying out the new Android Architecture components and have run into a road block when trying to use the MVVM model for a custom view.

本质上,我创建了一个自定义视图来封装一个通用 UI,并且它是在整个应用程序中使用的相应逻辑.我可以在自定义视图中设置 ViewModel,但随后我必须使用 observeForever() 或在自定义视图中手动设置 LifecycleOwner 如下所示,但似乎都没有正确.

Essentially I have created a custom view to encapsulate a common UI and it's respective logic to use throughout the app. I can set up the ViewModel in the custom view but then I'd have to either use observeForever() or manually set a LifecycleOwner in the custom view like below but neither seem correct.

活动

class MyActivity : AppCompatActivity() {

    lateinit var myCustomView : CustomView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        myCustomView = findViewById(R.id.custom_view)
        myCustomView.onAttach()
    }

    override fun onStop() {
        myCustomView.onDetach()
    }
}

自定义视图

class (context: Context, attrs: AttributeSet) : RelativeLayout(context,attrs){

    private val viewModel = CustomViewModel()

    fun onAttach() {
        viewModel.state.observeForever{ myObserver }
    }

    fun onDetach() {
        viewModel.state.removeObserver{ myObserver }
    }
}

选项 2) 从 Activity 设置lifecycleOwner`

活动

class MyActivity : AppCompatActivity() {

    lateinit var myCustomView : CustomView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        myCustomView = findViewById(R.id.custom_view)
        myCustomView.setLifeCycleOwner(this)
    }
}

自定义视图

class (context: Context, attrs: AttributeSet) : RelativeLayout(context,attrs){

    private val viewModel = CustomViewModel()

    fun setLifecycleOwner(lifecycleOwner: LifecycleOwner) {
        viewModel.state.observe(lifecycleOwner)
    }
}

我只是滥用了模式和组件吗?我觉得应该有一种更简洁的方法来从多个子视图组合复杂的视图,而无需将它们绑定到 Activity/Fragment

Am I just misusing the patterns and components? I feel like there should be a cleaner way to compose complex views from multiple sub-views without tying them to the Activity/Fragment

推荐答案

1 选项 -出于好意,您仍然需要做一些手动工作 - 例如,调用 onAttach\ onDetach 架构组件的主要目的是防止这样做.

1 Option - With good intention, you still have to do some manual work - like, calling onAttach\ onDetach Main purpose of Architecture components is to prevent doing this.

2 选项 -在我看来更好,但我会说将您的逻辑绑定在 ViewModelView 周围有点错误.我相信您可以在 Activity/Fragment 中执行相同的逻辑,而无需将 ViewModel 和 LifecycleOwner 传递给 CustomView.单个方法 updateData 足以达到此目的.

2 Option - In my opinion is better, but I would say it's a bit wrong to bind your logic around ViewModel and View. I believe you can do same logic inside Activity/Fragment without passing ViewModel and LifecycleOwner to CustomView. Single method updateData is enough for this purpose.

因此,在这种特殊情况下,我会说这是对架构组件的过度使用.

So, in this particular case, I would say it's overuse of Architecture Components.

这篇关于在没有 LifecycleOwner 的自定义视图中设置 LiveData 观察者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆