Android数据绑定库与Kotlin Android扩展 [英] Android Data Binding Library vs Kotlin Android Extensions

查看:117
本文介绍了Android数据绑定库与Kotlin Android扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读MVVM架构如何工作以及如何使用Android数据绑定库帮助.

I'm reading about how the MVVM architecture works and how to use Android Data Binding Library help.

以一种非常一般的方式,我了解到Android数据绑定在UI层和保存要显示的信息的基础数据模型之间创建了一个链接.

In a very general way I understand that Android Data Binding creates a link between UI layer and the underlying data model that holds the information to display.

Kotlin Android扩展是另一个Kotlin插件,可让您从活动",片段"和视图"中恢复视图.该插件将生成一些额外的代码,使您可以访问XML布局中的视图,就像它们是具有在布局定义中使用的ID名称的属性一样.

Kotlin Android Extensions are another Kotlin plugin that will allow you to recover views from Activities, Fragments and Views. The plugin will generate some extra code that will allow you to access views in the XML layout, just as if they were properties with the name of the id you used in the layout definition.

使用Android数据绑定库和Kotlin Android扩展有什么区别?它们是出于不同的目的吗?它们以什么方式相互补充?

What is the difference between using Android Data Binding Library and Kotlin Android Extensions? Are they for different purposes? Do they complement each other, in what way?

谢谢您的回答.

推荐答案

Kotlin Android扩展和Android数据绑定库都有助于消除对findViewById的使用.

Both, Kotlin Android Extensions and Android Data Binding Library help to eliminate the use of findViewById.

但是,这些功能还可以做得更多,可以相互补充. 详细地说,使用Android数据绑定库,您可以在xml文件中设置"模型,然后可以直接利用该模型为布局中的视图设置值. 参见 <data>标签如何与数据绑定库一起使用.

But there are also more things that these do, that can complement one another. To elaborate, with the Android Data Binding library, you can 'set' models in your xml files, which can then directly be leveraged to set values for the views in the layout. See how a <data> tag can be used with the data binding library.

Kotlin android扩展程序不提供此功能. 同时,Kotlin android扩展提供了一些令人惊奇的功能,例如@parcelize批注,使类几乎不需要样板代码即可被包裹,等等.

Kotlin android extensions does not provide for this. At the same time, Kotlin android extensions provides for some amazing features like @parcelize annotation to make classes parcelable with almost no boilerplate code, etc.

总而言之,虽然它们都取消了findViewById的使用,但它们也具有各自的功能,可以很好地相互补充.

To conclude, while they both eliminate the use of findViewById, they have their own features too which can complement one another well.

这篇关于Android数据绑定库与Kotlin Android扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆