Android 数据绑定在自定义视图中注入 ViewModel
Android data binding inject ViewModel in custom view
我将离开这个 Whosebug 答案 将数据对象绑定到自定义视图。但是,在设置完所有内容后,我的视图并未使用数据进行更新,而是 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>
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>
MyProfileCustomView.kt
:
class MyProfileCustomView : FrameLayout {
constructor...
private val binding: ViewProfileBinding = ViewProfileBinding.inflate(LayoutInflater.from(context), this, true)
fun setViewModel(profileViewModel: ProfileViewModel) {
binding.viewModel = profileViewModel
}
}
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)
}
}
一切正常,对 profileManager.profile()
的 API 调用成功,ViewProfileBinding
class 使用正确的设置成功创建。问题是当我执行 name.postValue(profile.name)
时,视图未更新为 profile.name
的值
缺的一块是设置一个Lifecycle Owner.
binding.setLifecycleOwner(parent) //parent should be a fragment or an activity
回答对我有帮助,想抛出一个 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
}
在您看来:
getLifeCycleOwner(this)?.let{
binding.setLifecycleOwner(it)
}
我将离开这个 Whosebug 答案 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>
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>
MyProfileCustomView.kt
:
class MyProfileCustomView : FrameLayout {
constructor...
private val binding: ViewProfileBinding = ViewProfileBinding.inflate(LayoutInflater.from(context), this, true)
fun setViewModel(profileViewModel: ProfileViewModel) {
binding.viewModel = profileViewModel
}
}
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)
}
}
一切正常,对 profileManager.profile()
的 API 调用成功,ViewProfileBinding
class 使用正确的设置成功创建。问题是当我执行 name.postValue(profile.name)
时,视图未更新为 profile.name
缺的一块是设置一个Lifecycle Owner.
binding.setLifecycleOwner(parent) //parent should be a fragment or an activity
回答对我有帮助,想抛出一个 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
}
在您看来:
getLifeCycleOwner(this)?.let{
binding.setLifecycleOwner(it)
}