Android 数据绑定在自定义视图中注入 ViewModel

Gre*_*s36 11 data-binding android android-custom-view kotlin android-viewmodel

我要离开这个 stackoverflow 答案/sf/answers/2437229581/将数据对象绑定到自定义视图。但是,在一切都设置好之后,我的视图没有被数据更新,而是TextView保持空白。这是我的代码(简化,为了适应这里)

activity_profile.xml::

<layout xmlns...>
    <data>
        <variable name="viewModel" type="com.example.ProfileViewModel"/>
    </data>
    <com.example.MyProfileCustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:viewModel="@{viewModel}">
</layout>
Run Code Online (Sandbox Code Playgroud)


view_profile.xml

<layout xmlns...>
    <data>
        <variable name="viewModel" type="com.example.ProfileViewModel"/>
    </data>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@{viewModel.name}">
</layout>
Run Code Online (Sandbox Code Playgroud)


MyProfileCustomView.kt

class MyProfileCustomView : FrameLayout {
    constructor...

    private val binding: ViewProfileBinding = ViewProfileBinding.inflate(LayoutInflater.from(context), this, true)

    fun setViewModel(profileViewModel: ProfileViewModel) {
        binding.viewModel = profileViewModel
    }
}
Run Code Online (Sandbox Code Playgroud)



ProfileViewModel.kt

class ProfileViewModel(application: Application) : BaseViewModel(application) {
    val name = MutableLiveData<String>()

    init {
        profileManager.profile()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(::onProfileSuccess, Timber::d)
    }

    fun onProfileSuccess(profile: Profile) {
        name.postValue(profile.name)
    }
}
Run Code Online (Sandbox Code Playgroud)

一切正常,API 调用profileManager.profile()成功,ViewProfileBinding使用正确的设置成功创建了类。问题是当我做name.postValue(profile.name)的时候,视图没有更新为profile.name

fuk*_*uda 8

缺少的部分是设置Lifecycle Owner

binding.setLifecycleOwner(parent) //parent should be a fragment or an activity
Run Code Online (Sandbox Code Playgroud)


Jua*_*dez 6

回答对我有帮助,并希望抛出一个可以添加的 util 方法来获取 LifeCycleOwner

fun getLifeCycleOwner(view: View): LifecycleOwner? {
    var context = view.context

    while (context is ContextWrapper) {
        if (context is LifecycleOwner) {
            return context
        }
        context = context.baseContext
    }

    return null
}
Run Code Online (Sandbox Code Playgroud)

在您看来:

getLifeCycleOwner(this)?.let{
   binding.setLifecycleOwner(it)
}
Run Code Online (Sandbox Code Playgroud)