1
我有這樣的型號如何將對象與兩個佈局綁定?
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();
}
謝謝你的建議!我試圖通過在onCheckedChanged中將我的observable布爾字段設置爲true來使它們兩個都被檢查,但是我會通過你的 –