I am learning data binding and mvvm. I have an issue where I would like a BaseViewModel.kt
to include some UI related variables such as an isLoading
flag and loadingText
. When a network request is made, I set isLoading
to true and some child of my base view model should set the text. For example for a LoginViewModel.kt
the text might be 'logging in'. Is it possible to pass these variables to an included base layout?
So a login_activity.xml
might include this in it's layout:
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type="core.sdk.ui.login.LoginViewModel" />
</data>
<!-- Various click listeners using the viewModel variable -->
<include
android:id="@+id/progress_include"
layout="@layout/progress_bar"
android:visibility="@{viewModel.isLoading ? View.VISIBLE : View.GONE}"
bind:viewModel="@{viewModel}"/>
Now I want my progress_bar.xml
to be nice and generic and use the base view model:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="core.sdk.ui.login.LoginActivity">
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type="core.sdk.ui.base.BaseViewModel" />
</data>
<LinearLayout
android:id="@+id/circular_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<android.support.v4.widget.ContentLoadingProgressBar
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/progress_text"
style="@style/TextAppearance.AppCompat.Subhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-thin"
android:gravity="center_horizontal"
android:text="@{viewModel.loadingText}"
android:textStyle="italic"
tools:text="loading..." />
</LinearLayout>
The error I get is something like
****/ data binding error ****msg:Cannot find the setter for attribute 'bind:viewModel' with parameter type core.sdk.ui.login.LoginViewModel
If this isn't possible the only alternative I can see is to remove the include and copy and paste the progress bar + text to every view model which isn't very nice.