什么时候应该使用InverseBindingAdapter? [英] When should InverseBindingAdapter be used?
问题描述
为什么在Android DataBinding中需要 InverseBindingAdapter
,我们什么时候应该使用它?
Why do I need InverseBindingAdapter
in Android DataBinding, When should we use it?
推荐答案
Inverse
一词实际上是在说您需要了解的所有内容,如果您想到了DataBinding的实际作用.如果为 Layout
启用了DataBinding,则可以将其视为两件事情:
The word Inverse
is really saying everything you need to know, if you think of what DataBinding is actually doing.
If you enable DataBinding for your Layout
you can perceive it as a pair of two things:
-
View
(< layout></layout>
中包含的所有Views
) -
模型
(< data></data>
中包含的所有变量)
View
(allViews
included in<layout></layout>
)Model
(all variables included in<data></data>
)
常规数据绑定确保 Model
中的更改将在 View
中生效.它由 @ {expression}
调用.
Regular DataBinding makes sure that a change in the Model
will have its effects in the View
. It is invoked by @{expression}
.
反向数据绑定确保 View
中的更改将在 Model
中生效.它由 @ = {expression}
调用(此处必须使用 =
char).
Inverse DataBinding makes sure that a change in the View
will have its effects in the Model
. It is invoked by @={expression}
(the =
char is essential here).
如果您认为在 View
中进行更改的方式是什么,则可以是:
If you think what are the ways to make a change in the View
, those can be:
- 内容更改(例如
TextView
中的文本) - 已检查状态的更改(例如,在
RadioGroup
中) - 焦点改变
- ...
其中一些默认情况下处于启用状态,而其他则需要编写自定义的 InverseBindingAdapter
.有关更多信息,请此处.
Some of them are enabled by default and for others you will need to write a custom InverseBindingAdapter
. More on that here.
如果您仍然需要有关该主题的更多信息,建议您观看 Google I/O 2016上有关高级数据绑定的演示文稿.
这篇关于什么时候应该使用InverseBindingAdapter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!