I have a fragment layout fragment_config.xml, which cointains the following:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:bind="http://schemas.android.com/tools">
<data>
<import type="android.view.View"/>
<variable name="viewModel" type ="...GlobalConfigViewModel"/>
</data>
...
<ToggleButton
android:id="@+id/btnShowAdvanced"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textOff="Show Advanced"
android:textOn="Hide Advanced"
android:checked="@={viewModel.advancedShown}"/>
<com.minh.minh.pumpnotifier.global.configuration.AdvancedBox
android:id="@+id/advancedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="@{viewModel.advancedShown ? View.VISIBLE : View.GONE}"
app:viewModel = "@{viewModel}"/>
...
</layout>
What is supposed to happen is that the visibility of Advanced Box toggles with the "checked"-state of the toggle button. I already confirmed that the two-way-databinding in the toggle-button sets the boolean "advancedShown" in the viewModel correctly. The "setVisibility"-method however is never called in the AdvancedBox-class (which extends LinearLayout).
Something else I tried is to set the visibility binding in the root element of the advanced_box.xml, since it also has a reference to the viewModel:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<import type="android.view.View"/>
<variable
name="viewModel"
type="...GlobalConfigViewModel" />
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/advancedSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="@{viewModel.advancedShown ? View.VISIBLE : View.GONE}"
android:orientation="vertical">
Both methods do not work out though. My question is why it does not work and what would be the correct way to use databinding in this case?
Your viewModel.advancedShown
should be ObservableField
or marked with @Bindable
annotation (in the second variant you need manually invoke notifyPropertyChanged(BR.advancedShown)
on your GlobalConfigViewModel
to trigger changes). You can find more information in official docs