简体   繁体   English

Kotlin中数据绑定的好处?

[英]Benefits of Databinding in kotlin?

I would like to know the benefits of writing extra code to implement dataBinding in Kotlin when developing Android Apps. 我想知道在开发Android应用程序时编写额外的代码以在Kotlin中实现dataBinding的好处。 Because Kotlin extensions already allows us to access the views directly without the need of findViewById's. 因为Kotlin扩展已经允许我们直接访问视图,而不需要findViewById。

I've found an answer here : 我在这里找到了答案:

Hey! 嘿! Developer Advocate for Android at Google here! Google的Android开发者倡导者!

I wanted to add a bit of background here. 我想在这里添加一些背景。 Kotlin Extensions with synthetic views was never intentionally “recommended” though that shouldn't be taken as a recommendation to not use them. 从未有意“推荐”具有综合视图的Kotlin扩展,但不应将其作为不使用它们的建议。 If they're working for you please feel free to continue using them in your app! 如果它们正在为您工作,请随时在您的应用中继续使用它们!

We've been shifting away from them (eg we don't teach them in the Udacity course) because they expose a global namespace of ids that's unrelated to the layout that's actually inflated with no checks against invalid lookups, are Kotlin only, and don't expose nullability when views are only present in some configuration. 我们一直在远离它们(例如,我们不在Udacity课程中教他们),因为它们公开了ID的全局名称空间,该名称空间与实际上没有夸大无效查找的布局无关的ID,仅是Kotlin,并且不当视图仅在某些配置中存在时,不会公开可空性。 All together, these issues cause the API to increase number of crashes for Android apps. 这些问题共同导致API增加Android应用程序的崩溃次数。

On the other hand, they do offer a lightweight API that can help simplify view lookups. 另一方面,它们确实提供了轻量级的API,可以帮助简化视图查找。 In this space it's also worth taking a look at Data Binding which also does automatic view lookups - as well as integrates with LiveData to automatically update your views as data changes. 在此空间中,还值得一看的是Data Binding,它也执行自动视图查找-并与LiveData集成以在数据更改时自动更新视图。

Today, there's a few options in this space that work: 如今,在这个领域中有一些可行的选择:

  • Data Binding is the recommendation for view lookup as well as binding, but it does add a bit of overhead when compared to Android Kotlin Extensions. 推荐使用数据绑定进行视图查找和绑定,但是与Android Kotlin Extensions相比,它确实增加了一些开销。 It's worth taking a look to see if this is a good fit for your app. 值得一看,看看这是否适合您的应用程序。 Data Binding also allows you to observe LiveData to bind views automatically when data changes. 数据绑定还允许您观察LiveData在数据更改时自动绑定视图。 Compared to Kotlin Extensions, it adds compile time checking of view lookups and type safety. 与Kotlin扩展相比,它增加了视图查找和类型安全性的编译时检查。
  • Android Kotlin Extensions is not officially recommended (which is not the same as recommendation against). 官方不建议您使用Android Kotlin扩展程序 (这与针对它的建议不同)。 It does come with the issues mentioned above, so for our code we're not using them. 它确实带有上述问题,因此对于我们的代码,我们不使用它们。
  • Butter Knife is another solution that is extremely popular and works for both Kotlin and the Java Programming Language. Butter Knife是另一种非常流行的解决方案,适用于Kotlin和Java编程语言。

Reading through the comments here there's a lot of developers that are having great luck with Kotlin Extensions. 在这里阅读注释,有很多开发人员对Kotlin Extensions感到很幸运。 That's great - and something we'll keep in mind as we look at ways to continue improving our APIs. 太好了-在寻找继续改进API的方式时,我们会牢记一些事情。 If you haven't taken a look at Data Binding, definitely give it a shot. 如果您还没有看过数据绑定,那么一定要尝试一下。

As an aside, our internal code style guide is not intended to be directly applied outside of our codebase. 顺便说一句,我们的内部代码样式指南不能直接在我们的代码库之外应用。 For example, we use mPrefixVariables, but there's no reason that every app should follow that style. 例如,我们使用mPrefixVariables,但是没有理由每个应用程序都应该遵循这种风格。

To clarify things, Accessing view directly and dataBinding are different. 为了明确起见,直接访问视图和dataBinding是不同的。 Prior to kotlin, we used to have a library called butterknife which did help to access views directly. 在Kotlin之前,我们曾经有一个名为butterknife的库,它确实有助于直接访问视图。

So coming back to dataBinding , what it does exactly is setting the values to the views directly/ getting event trigger(two way binding) inside xml instead you doing it explicitly in activity/fragment class. 所以回到dataBinding ,它的确切作用是直接将值设置为视图/在xml内获取事件触发器(双向绑定),而不是在activity / fragment类中显式地进行操作。

what's the benefit: Assume you have 20 Textviews inside your layout xml, you want set the values to those textviews from response object(with 20 corresponding fields). 有什么好处:假设您在版面xml中有20个Textview,则需要将值设置为响应对象中的那些Textview(具有20个对应字段)。

1)Without databinding, you need to access each view and set the value from object(so 20 lines of code). 1)没有数据绑定,您需要访问每个视图并从对象设置值(所以有20行代码)。

2) With databinding, you just bind the object to xml(one liner) and your Xml will populate respective fields using the binding code inside xml. 2)使用数据绑定,您只需将对象绑定到xml(一个衬里),您的Xml将使用xml内的绑定代码填充各个字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM