I am using livedata and databinding in my project. And whenever i fetch data from webservices, i am databinding it with livedata. I can pass livedata object to first viewStub from root layout. It observes livedata as expected. But i am trying to pass this liveData to second viewStub which is placed in first viewStub. But it is not observing livedata when livedata's value changes.
root layout which is contains first viewStub:
<data>
<import type="android.view.View" />
<import type="android.text.TextUtils" />
<variable
name="viewModel"
type="com.test.test.MyViewModel" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ViewStub
android:id="#+id/stub_import"
android:inflatedId="#+id/panel_import"
android:layout_width="match_parent"
android:layout_height="wrap_content"
bind:viewModel="#{viewModel}" />
</RelativeLayout>
first viewStub layout which is contains second viewStub:
<data>
<import type="android.view.View" />
<import type="android.text.TextUtils" />
<variable
name="viewModel"
type="com.test.test.MyViewModel" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
.
.
.
.
<ViewStub
android:id="#+id/second_stub_import"
android:inflatedId="#+id/second_panel_import"
android:layout_width="match_parent"
android:layout_height="wrap_content"
bind:viewModel="#{viewModel}" />
.
.
.
.
</RelativeLayout>
And the second viewStub layout which is using liveData from passed viewModel:
<data>
<import type="android.view.View" />
<import type="android.text.TextUtils" />
<variable
name="viewModel"
type="com.test.test.MyViewModel" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{viewModel.textLiveData}"
android:textColor="#color/general_text_color"
android:textSize="18sp" />
</RelativeLayout>
And additionally i can replace second viewStub to "include tag". This would help as well but it is not observing either.
When a viewStub layout contains another viewStub or include tag, and passing some observable objects to these layouts. it won't observe.
Can anyone tell me any solution for this situation?
Related
I am having some issues with data binding a #string/title into a include tag.
relevant include xml
<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">
<data>
<import type="kotlin.jvm.functions.Function0"/>
<variable
name="saveVisibility"
type="Boolean" />
<variable
name="closeClick"
type="Function0<kotlin.Unit>" />
<variable
name="titleText"
type="String" />
<variable
name="saveClick"
type="Function0<kotlin.Unit>" />
</data>
<TextView
android:id="#+id/toolbarTitle"
style="#style/HeadlineSecondary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_normal"
android:layout_marginEnd="#dimen/margin_normal"
android:text="#{titleText}"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/toolbarSave"
app:layout_constraintStart_toEndOf="#id/toolbarClose"
app:layout_constraintTop_toTopOf="parent"
tools:text="#string/toolbar_title_placeholder" />
As you can see I have a variable called titleText and I am basically just trying to set a textview using it.
Here is the parent XML that is not working for this titleText
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_close_save_databind"
app:titleText="#string/title"
app:saveVisibility="#{viewModel.isModified}"
app:saveClick="#{viewModel::saveData}"
app:closeClick="#{viewModel::closeClick}" />
I am not sure why it can't link the string into the include tag.
So the problem space is pretty simple.
I have a layout. Let's call it fragment1.xml and it looks like this.
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="viewModel"
type="SomeViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="#+id/include_some_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/layout_consent_checkbox"
app:checkedData="#{viewModel.checkedData}" />
</LinearLayout>
</layout>
And let layout file layout_consent_checkbox.xml be this.
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="checkedData"
type="Boolean" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/cb_some_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="#={checkedData}"
android:text="Plis check this?"/>
</LinearLayout>
</layout>
Now according to my knowledge, this should work. checkedData in SomeViewModel should be updating the Boolean value, based on the status change of the CheckBox. But it isn't getting updated. Please have a look and update me on what I'm doing wrong. Thanks!
I have a layout as such:
<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"
>
<data>
<variable
name="viewModel"
type="com.myPackage.MyViewModel" />
</data>
<android.support.design.widget.CoordinatorLayout>
<include layout="#layout/modal_popup_view" />
</android.support.design.widget.CoordinatorLayout>
</layout>
How can I pass the variable viewModel into the included layout?
Define a viewModel (or any other named) variable in your #layout/modal_popup_view.
<data>
<variable
name="viewModel"
type="com.myPackage.PopupViewModel" />
</data>
In MyViewModel, expose the property popupViewModel of the type PopupViewModel. Pass the property to the included layout.
<include layout="#layout/modal_popup_view"
app:viewModel="#{viewModel.popupViewModel}" />
I am using concept of Databinding-mvvm to implement such things. I have declared recyclerview into the my mainactivity. like
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View"/>
<variable
name="ffvm"
type="com.example.the_king.apartmentmanagementsystem.ViewModal.
FragmentViewModal.ViewModal"/>
</data>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.the_king.apartmentmanagementsystem.
Fragment.Fragment_FlatHolder">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
</layout>
my cardview is look like
<layout>
<data>
<import type="android.view.View"/>
<variable
name="cfhvm"
type="com.example.the_king.apartmentmanagementsystem.ViewModal.
CardViewModel.CardViewModal"/>
</data>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.the_king.apartmentmanagementsystem.
CardView.CardViewFlatHolder"
layout_width="match_parent"
android:background="#color/colorPrimaryLight"
android:elevation="25dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:innerRadius="15dp"
android:thicknessRatio="1.9"
layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{cfhvm.firstname+` `+cfhvm.lastname}"
android:layout_marginLeft="5dp"
android:textSize="#dimen/text_title"
/>
</android.support.v7.widget.CardView>
</layout>
for the cardview i have created viewmodel as cardviewmodal and for Recyclerview i have created viewmodel and i have also created adapter to fill data now i got confuse how do i set value of recyclerview from the viewmodel which contains cardviewmodel.
I'm trying to use databinding without any binding data to widgets such as TextViews on main layout but i get this error when i run application:
Only one layout element and one data element are allowed
Main layout:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:slidingLayer="http://schemas.android.com/apk/res-auto">
<data>
</data>
<variable
name="presenter"
type="com.example.Ui.Register.Presenter.ActivityRegisterPresenter"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d1d1d1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/permission_for_read_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/permission_for_read_contacts"
android:textColor="#color/white"/>
<TextView
android:layout_width="match_parent"
android:layout_height="#dimen/default_textview_height"
android:text="#string/get_read_contact_permission"
android:background="#drawable/selector_blue_buttons"
android:clickable="#{()->presenter.getReadContactsPermission()}"
android:textColor="#color/white"/>
</LinearLayout>
<TextView
android:id="#+id/activity_register_view_port"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout>
</FrameLayout>
</layout>
in this activity i don't have any binding by default such as setText, after removing this line:
<variable
name="presenter"
type="com.example.Ui.Register.Presenter.ActivityRegisterPresenter"/>
problem resolved!!
public class ActivityRegisterPresenter {
private ActivityRegisterContract.View view;
public ActivityRegisterPresenter(ActivityRegisterContract.View mView) {
view = mView;
}
public void getReadContactsPermission(){
view.getReadContactsPermission();
}
}
You need to write variable tag within data tag as follows :
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:slidingLayer="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="presenter"
type="com.example.Ui.Register.Presenter.ActivityRegisterPresenter"/>
</data>
...
</layout>
Your <variable> element needs to be inside the <data> element.
E.g.:
<data>
<variable
name="presenter"
type="com.example.Ui.Register.Presenter.ActivityRegisterPresenter"/>
</data>