[英]Best practice to combine android ViewModel and data binding
I am a little confused about how to combine 2 techniques in android, namely我对如何在 android 中结合两种技术感到有些困惑,即
ViewModel should handle business logic, the layer behind the actual view and send data to the view with something like LiveData. ViewModel应该处理业务逻辑,即实际视图背后的层,并使用 LiveData 之类的东西将数据发送到视图。 The view observes to this LiveData and updates itself on changes视图会观察此 LiveData 并根据更改自行更新
Data Binding Library exists to make it easier to bind to the view and interact with the view on another level (for example by updating some properties of some class)数据绑定库的存在是为了更容易绑定到视图并与另一个级别的视图交互(例如通过更新某些类的某些属性)
The questions:问题:
Update: Found official documentation for my issue.更新:找到我的问题的官方文档。 Here is the link: https://developer.android.com/topic/libraries/data-binding/architecture#viewmodel这是链接: https://developer.android.com/topic/libraries/data-binding/architecture#viewmodel
How data binding works数据绑定的工作原理
Consider using LiveData
, it lives inside the ViewModel
and is how the data binding library knows that you must update for example the string of a TextView
.考虑使用LiveData
,它位于ViewModel
中,并且数据绑定库如何知道您必须更新例如TextView
的字符串。
What data binding actually does is something similar to what you would explicitly do in your fragment:数据绑定的实际作用类似于您在片段中显式执行的操作:
Subscribe from your Kotlin code ( Fragment/Activity
) to a LiveData
property that lives within the ViewModel
but in this case, data binding will update the view values for you since you will indicate it before from your XML Layout.从您的 Kotlin 代码 ( Fragment/Activity
) 订阅ViewModel
中的LiveData
属性,但在这种情况下,数据绑定将为您更新视图值,因为您之前会从 XML 布局中指示它。
So the answer is (A):所以答案是(A):
You could have a ViewModel
class with properties of type LiveData<T>
and from your Layout, you can use them directly without subscribing explicitly from your kotlin code as I mentioned before, which continues to guarantee that the ViewModel continues being the provider of information for the user's view, the difference is that instead of you are doing it explicitly, data binding will do it for you.您可以拥有一个ViewModel
class 类型为LiveData<T>
的属性,并且从您的布局中,您可以直接使用它们,而无需像我之前提到的那样从您的 kotlin 代码显式订阅,这继续保证 ViewModel 继续为从用户的角度来看,不同之处在于,不是您明确地执行此操作,而是数据绑定会为您执行此操作。
class MyViewModel : ViewModel {
// view model doesn't know if Fragment/Activity is using data binding or not, it just continues providing info as normal.
val myString : MutableLiveData<String> = MutableLiveData()
init {
myString.value = "a value that data binding will print in a TextView for you"
}
private fun changeMyString() {
// Change the value in the future when you want and then data binding will print the text in your TextView for you.
myString.value = "other value to that TextView"
}
}
Layout:布局:
<TextView
android:text="@{myViewModel.myString}" />
Resources资源
This Google Codelab is pretty useful, it helped me when I started with data binding because it is prepared to teach.这个Google Codelab非常有用,当我开始使用数据绑定时它对我很有帮助,因为它已经准备好进行教学了。
If you just want to go directly to code, android/sunflower is a repository that uses data binding and in general provides useful samples of jetpack features.如果您只想 go 直接编码, android/sunflower是一个使用数据绑定的存储库,通常提供有用的 jetpack 功能示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.