I am studying data binding and mvvm. I have a problem when I would like to BaseViewModel.kt
include some UI related variables like flag isLoading
and loadingText
. When a network request is executed, I set isLoading
to true, and some child of my base view model should set the text. For example, for LoginViewModel.kt
text, there may be a "login". Can I pass these variables to the included base layout?
So, this login_activity.xml
may include this layout:
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type="core.sdk.ui.login.LoginViewModel" />
</data>
<include
android:id="@+id/progress_include"
layout="@layout/progress_bar"
android:visibility="@{viewModel.isLoading ? View.VISIBLE : View.GONE}"
bind:viewModel="@{viewModel}"/>
Now I want mine to progress_bar.xml
be nice and versatile and use a basic presentation 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 similar to
****/ data binding error ****msg:Cannot find the setter for attribute 'bind:viewModel' with parameter type core.sdk.ui.login.LoginViewModel
, , , + , .