Search code examples
androiddata-bindingandroid-databinding

msg:fragments do not support data binding expressions (in tag <fragment>) data binding error


I have a problem with data binding when I try to add visibility for tag fragment with map:

<layout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        >
    <data>
        <import type="android.view.View"/>
        <import type="xxx.xxx.MapContract.ViewModel"/>
        <variable
                name="vm"
                type="ViewModel"
                />
    </data>
    <FrameLayout
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
        <fragment
                android:id="@+id/map_fragment"
                class="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="@{vm.showMap}" // here problem
                />

How can I solve this problem use data binding? Why fragments do not support data binding?


Solution

  • I solve this issue just like this:

     <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:visibility="@{!vm.showMap}" // move here
                >
            <fragment
                    android:id="@+id/map_fragment"
                    class="com.google.android.gms.maps.SupportMapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    />
        </FrameLayout>