android - kotlin 中数据绑定(bind)的好处?

标签 android kotlin android-databinding kotlin-android-extensions

我想知道在开发 Android 应用程序时编写额外代码以在 Kotlin 中实现数据绑定(bind)的好处。 因为 Kotlin 扩展已经允许我们直接访问 View 而无需 findViewById。

最佳答案

我找到了答案 here :

Hey! Developer Advocate for Android at Google here!

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. If they're working for you please feel free to continue using them in your app!

We’ve been shifting away from them (e.g. 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. All together, these issues cause the API to increase number of crashes for Android apps.

On the other hand, they do offer a lightweight API that can help simplify view lookups. 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.

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. 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. Compared to Kotlin Extensions, it adds compile time checking of view lookups and type safety.
  • Android Kotlin Extensions is not officially recommended (which is not the same as recommendation against). 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.

Reading through the comments here there's a lot of developers that are having great luck with Kotlin Extensions. That's great - and something we'll keep in mind as we look at ways to continue improving our APIs. 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.

关于android - kotlin 中数据绑定(bind)的好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56092250/

相关文章:

android - 是否建议将 Spinner 用于布局更改

android - 操作栏下方的抽屉导航

java - 如何模拟包含可能引发 NPE 的变量的类

Android AlertDialog - 如何处理超大消息?

android - 未固定折叠工具栏布局中的工具栏

android-databinding - appbar_scrolling_view_behavior 未在 SwipeRefreshLayout 数据绑定(bind) android 解决

Android 数据绑定(bind) - 找不到接受参数类型 'long' 的 <> 的 getter

android - EditText 中的双向数据绑定(bind)

java - Kotlin 继承与泛型

constructor - Kotlin:为什么构造函数参数默认具有 "internal"可见性?