【发布时间】:2017-03-10 00:10:58
【问题描述】:
我有这样的模型
public class MyCheckedVM extends BaseObservable {
@Bindable
ObservableBoolean observableBoolean = new ObservableBoolean();
@Bindable
public ObservableBoolean getObservableBoolean() {
return observableBoolean;
}
@Bindable
public void setObservableBoolean(ObservableBoolean observableBoolean) {
this.observableBoolean = observableBoolean;
}
public void onCheckChanged(CompoundButton view, boolean isChecked) {
Log.d(TAG, "onCheckChanged");
}
我将此模型设置为与ToggleButton 和内部Fragment 的主要活动绑定,其中也有这样的按钮
<ToggleButton
android:onCheckedChanged="@{viewModel.onCheckChanged}"
android:checked="@{viewModel.observableBoolean}"
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
但是当我尝试点击任何按钮时,它们都在相互独立地发生变化。也许我将模型绑定到Fragmnet的方式是错误的@
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
FragmentExampleBinding fragmentExampleBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_example, container, false);
fragmentExampleBinding.setViewModel(((MainActivity) getActivity()).getViewModel()); //getter in MainActivity for instance of viewModel
return fragmentExampleBinding.getRoot();
}
【问题讨论】:
标签: android mvvm data-binding observable