"At the moment, data binding is only for layout resources, not menu resources"
But, the behaviour can be achieved with Observable.OnPropertyChangedCallback. First you need to define OnPropertyChangedCallback:
private final Observable.OnPropertyChangedCallback propertyChangedCallback = new Observable.OnPropertyChangedCallback() {
@Override
public void onPropertyChanged(Observable observable, int i) {
getActivity().invalidateOptionsMenu();
}
};
Assuming that you have a binding for Fact model in your fragment:
<variable
name="item"
type="ru.dixy.ubiworkerchecklistsmobile.Models.Fact"/>
Now you need to register propertyChangedCallback and unregister it when you are done with it:
@Override
public void onStart() {
super.onStart();
binding.getItem().addOnPropertyChangedCallback(propertyChangedCallback);
}
@Override
public void onStop() {
super.onStop();
binding.getItem().removeOnPropertyChangedCallback(propertyChangedCallback);
}
Now we are ready update your view state based on the Fact model:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_fact, menu);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.compliteitem).setVisible(binding.getItem().isVisible());
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…