我可以使用具有重叠值集的 BindingAdapter 吗?
Can I have BindingAdapters with overlapping value sets?
假设我有这个带有方法的视图模型:
public int getValueA() {
return a;
}
public int getValueB() {
return b;
}
@BindingAdapter("valueA")
public void setupSomething(View view, int valueA) {
// do something with a
}
@BindingAdapter({"valueA", "valueB"})
public void setupSomethingElse(View view, int valueA, int valueB) {
// do something with a and b
}
然后我将其绑定到一个视图:
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
bind:valueA="@{viewmodel.valueA}"
bind:valueB="@{viewmodel.valueB}"/>
如何调用这两个 BindingAdapter
方法?现在数据绑定只是调用后者。我想我可以从 setSomethingElse
中调用 setSomething
但这对我来说有点可疑(并且部分地破坏了数据绑定的目的)。
这就像你在暗示自己:你需要从 setupSomethingElse
调用 setupSomething()
。这样做很好,数据绑定是如何工作的。 只有最合适的 @BindingAdapter
才会用于您的属性。
或者,您可以使用 @BindingAdapter
的 requireAll()
字段。但这只有在您可以为您的值处理 Java 默认值(在您的情况下为 0)时才可行。
Whether every attribute must be assigned a binding expression or if some can be absent. When this is false, the BindingAdapter
will be called when at least one associated attribute has a binding expression. The attributes for which there was no binding expression (even a normal XML value) will cause the associated parameter receive the Java default value. Care must be taken to ensure that a default value is not confused with a valid XML value.
@BindingAdapter({"valueA", "valueB"}, requireAll = false)
public void setupSomethingElse(View view, int valueA, int valueB) {
if (valueA != 0) {
// do something with a
if (valueB != 0) {
// do something with a and b
}
}
}
所以您不再需要 setupSomething()
。但我个人更喜欢第一种方法。
假设我有这个带有方法的视图模型:
public int getValueA() {
return a;
}
public int getValueB() {
return b;
}
@BindingAdapter("valueA")
public void setupSomething(View view, int valueA) {
// do something with a
}
@BindingAdapter({"valueA", "valueB"})
public void setupSomethingElse(View view, int valueA, int valueB) {
// do something with a and b
}
然后我将其绑定到一个视图:
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
bind:valueA="@{viewmodel.valueA}"
bind:valueB="@{viewmodel.valueB}"/>
如何调用这两个 BindingAdapter
方法?现在数据绑定只是调用后者。我想我可以从 setSomethingElse
中调用 setSomething
但这对我来说有点可疑(并且部分地破坏了数据绑定的目的)。
这就像你在暗示自己:你需要从 setupSomethingElse
调用 setupSomething()
。这样做很好,数据绑定是如何工作的。 只有最合适的 @BindingAdapter
才会用于您的属性。
或者,您可以使用 @BindingAdapter
的 requireAll()
字段。但这只有在您可以为您的值处理 Java 默认值(在您的情况下为 0)时才可行。
Whether every attribute must be assigned a binding expression or if some can be absent. When this is false, the
BindingAdapter
will be called when at least one associated attribute has a binding expression. The attributes for which there was no binding expression (even a normal XML value) will cause the associated parameter receive the Java default value. Care must be taken to ensure that a default value is not confused with a valid XML value.
@BindingAdapter({"valueA", "valueB"}, requireAll = false)
public void setupSomethingElse(View view, int valueA, int valueB) {
if (valueA != 0) {
// do something with a
if (valueB != 0) {
// do something with a and b
}
}
}
所以您不再需要 setupSomething()
。但我个人更喜欢第一种方法。