在模型中使用具有多对多关系的 Android 数据绑定
Using Android Data Binding with many-to-many relationship in model
我是 Android 开发的新手,一直在尝试双向数据绑定。虽然我已经能够在视图和模型之间执行一些基本绑定,但我遇到了一个场景,我需要更新两个实体之间的多对多关系。
考虑以下(人为的)示例:
public class Person extends BaseObservable {
private Set<Thing> things;
@Bindable
public boolean hasThing(Thing thing) {
return things.contains(thing);
}
public void setHasThing(Thing thing, boolean hasThing) {
boolean changed = hasThing
? things.add(thing)
: things.remove(thing);
if (changed) {
// notify change
}
}
}
我想将 onChecked
事件绑定到 Person
中添加或删除 Thing
的操作:
<data>
<variable name="person" type="org.example.model.Person"/>
<variable name="thing" type="org.example.model.Thing"/>
</data>
<!-- obviously doesn't work -->
<androidx.appcompat.widget.SwitchCompat
android:checked="@={person.hasThing(thing)}"/>
完成此任务的最佳方法是什么?我调查了 @BindingMethod
和 @BindingAdapter
,但是:
- 根据 Javadoc,
@BindingMethod
对参数和 return 类型的方法有限制,这似乎使其更适合类型转换等用例
@BindingAdapter
/@InverseBindingAdapter
似乎很有希望,但我正在努力思考如何触发适配器:
<androidx.appcompat.widget.SwitchCompat
android:checked="@={?}"
app:person="@{person}"
app:thing="@{thing}"/>
提前致谢!
事实证明我让这件事变得比需要的更难了。
以下作品:
<data>
<variable name="person" type="org.example.model.Person"/>
<variable name="thing" type="org.example.model.Thing"/>
</data>
<androidx.appcompat.widget.SwitchCompat
android:checked="@{person.hasThing(thing)}"
app:onCheckedChanged="@{(view, checked) -> person.setHasThing(thing, checked)}"/>
@BindingAdapter("onCheckedChanged")
public static void setOnCheckedChangeListener(CompoundButton button, CompoundButton.OnCheckedChangeListener changeListener) {
button.setOnCheckedChangeListener(changeListener);
}
我是 Android 开发的新手,一直在尝试双向数据绑定。虽然我已经能够在视图和模型之间执行一些基本绑定,但我遇到了一个场景,我需要更新两个实体之间的多对多关系。
考虑以下(人为的)示例:
public class Person extends BaseObservable {
private Set<Thing> things;
@Bindable
public boolean hasThing(Thing thing) {
return things.contains(thing);
}
public void setHasThing(Thing thing, boolean hasThing) {
boolean changed = hasThing
? things.add(thing)
: things.remove(thing);
if (changed) {
// notify change
}
}
}
我想将 onChecked
事件绑定到 Person
中添加或删除 Thing
的操作:
<data>
<variable name="person" type="org.example.model.Person"/>
<variable name="thing" type="org.example.model.Thing"/>
</data>
<!-- obviously doesn't work -->
<androidx.appcompat.widget.SwitchCompat
android:checked="@={person.hasThing(thing)}"/>
完成此任务的最佳方法是什么?我调查了 @BindingMethod
和 @BindingAdapter
,但是:
- 根据 Javadoc,
@BindingMethod
对参数和 return 类型的方法有限制,这似乎使其更适合类型转换等用例 @BindingAdapter
/@InverseBindingAdapter
似乎很有希望,但我正在努力思考如何触发适配器:<androidx.appcompat.widget.SwitchCompat android:checked="@={?}" app:person="@{person}" app:thing="@{thing}"/>
提前致谢!
事实证明我让这件事变得比需要的更难了。
以下作品:
<data>
<variable name="person" type="org.example.model.Person"/>
<variable name="thing" type="org.example.model.Thing"/>
</data>
<androidx.appcompat.widget.SwitchCompat
android:checked="@{person.hasThing(thing)}"
app:onCheckedChanged="@{(view, checked) -> person.setHasThing(thing, checked)}"/>
@BindingAdapter("onCheckedChanged")
public static void setOnCheckedChangeListener(CompoundButton button, CompoundButton.OnCheckedChangeListener changeListener) {
button.setOnCheckedChangeListener(changeListener);
}