Can't pass viewmodel in databinding to child include layout

Viewed 1088

this is some code in my parent xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
    <variable
            name="viewModel"
            type="(..).viewmodels.TestViewModel"/>
</data>


<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay"
        >

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        <include
                android:id="@+id/layout"
                bind:viewModel="@{viewModel}"
                layout="@layout/test_content"/>
    </ScrollView>
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Here's the child include layout

<layout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
                name="viewModel"
                type="(..).TestViewModel" />
    </data>
<LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="74dp"
        tools:layout_editor_absoluteY="1dp"
        xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <Button
                android:id="@+id/disable"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="@color/orange"
                android:text="@{viewModel.buttonText}"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>
</layout>

As you can see in this example I data binded text to the viewmodel and passed it down from parent to child via bind:viewModel="@{viewModel}"

Here's the code in the activity:

val binding:TestContentBinding = DataBindingUtil.setContentView(this, R.layout.test_content)
binding.setLifecycleOwner(this)
binding.viewModel = testViewModel

If I set the binding to the child xml then the data gets binded, however if i set the binding to the parent activity the viewmodel doesn't get passed down.

The back end certainly has the correct data however I don't know how to bind data to the child. I've even tried manually setting the layout's viewModel however that doesn't work. How do I pass the viewmodel?

1 Answers

You could try changing the name of the variable in your included layout.

Here is a quick article explaining how to use include layout with ViewModel.

Related