Search code examples
androidviewmodelandroid-databinding

How to pass data variable to included layout?


Is there a way to pass data variable to included layout?

parent layout

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="@layout/fragment_settings">

    <data>

        <variable
            name="viewModel"
            type="......settings.SettingsFragmentViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:overScrollMode="never"
        android:padding="@dimen/spacing_default">

        <include
            android:id="@+id/main"
            layout="@layout/layout_settings_main" />

    </androidx.constraintlayout.widget.ConstraintLayout>

and want to have viewModel in included layout

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="@layout/fragment_settings">

    <data>

        <variable
            name="viewModel"
            type="......settings.SettingsFragmentViewModel" />
    </data>

    <merge>
...........

or is it possible only when setting like this:

 binding = FragmentSettingsBinding.inflate(inflater, container, false).apply {
            lifecycleOwner = this@SettingsFragment
            viewModel = this@SettingsFragment.viewModel
            main.viewModel = this@SettingsFragment.viewModel
        }

Solution

  • In parent xml inside include tag pass data variable to included layout using bind:viewModel.

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:bind="http://schemas.android.com/apk/res-auto"
    tools:showIn="@layout/fragment_settings">
    
    <data>
    
        <variable
            name="viewModel"
            type="......settings.SettingsFragmentViewModel" />
    </data>
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:overScrollMode="never"
        android:padding="@dimen/spacing_default">
    
        <include
            android:id="@+id/main"
            layout="@layout/layout_settings_main"
            bind:viewModel="@{viewModel}" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Your included layout will get object instance in viewModel.

    For more details check this tutorial