简体   繁体   English

结合android ViewModel和数据绑定的最佳实践

[英]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:问题:

  • Should the properties / model property of Data Binding Library be kept inside of ViewModel class (A) or in the view (activity, fragment) (B)数据绑定库的属性 / model 属性是否应该保留在ViewModel class 中(A)或视图(活动、片段)中(B)
  • If (A): If the Data Binding Library properties / models are kept in ViewModel class, is it considered bad practice that view logic is executed inside ViewModel by changing data from the data binding library?如果(A):如果数据绑定库属性/模型保存在 ViewModel class 中,通过更改数据绑定库中的数据在 ViewModel 中执行视图逻辑是否被认为是不好的做法?
  • Is there a good code example (some GitHub repo) where there is an example of a decent combination of those 2 concepts?是否有一个很好的代码示例(一些 GitHub 存储库),其中有一个很好地组合这两个概念的示例?

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.

相关问题 ViewModel + Data Binding中的最佳实践和模式。 ViewModel中的ObservableField好吗? - Best practices and patterns in ViewModel + Data Binding. Is ObservableField in ViewModel OK? Android ViewModel:如何从 Activity 中获取结果(最佳实践)? - Android ViewModel: How to get a result from the Activity (best practice)? BoundService + LiveData + ViewModel 新Android推荐架构最佳实践 - BoundService + LiveData + ViewModel best practice in new Android recommended architecture 如何在一个视图上调用多个 onClick,并将其与数据绑定和 ViewModel 结合起来? - How to call multiple onClick on one view, and combine this with data binding and ViewModel? Android数据绑定在自定义视图中注入ViewModel - Android data binding inject ViewModel in custom view 从Android项目到ViewModel的数据绑定 - Data Binding from Android project to ViewModel android viewmodel mutablelivedata 不更新,2 路数据绑定 - android viewmodel mutablelivedata not updating, 2 way data binding Android:使用数据绑定调用 ViewModel 函数 - Android: Call ViewModel function with Data Binding 如何将架构组件与Android上的数据绑定相结合? - how to combine Architecture Components with data binding on android? Android Google Map-更新数据的最佳做法 - Android google map - best practice for updating data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM