RecyclerView with DataBinding-MVVM - android

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.

Related

Can't pass viewmodel in databinding to child include layout

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?
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.

How to pass OnClickListener to included layout?

I need to pass OnClickListener from parent to included layout.
I have a layout with an included view that accepts OnClickListener:
<?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">
<data>
<variable
name="clickListener"
type="android.view.View.OnClickListener" />
</data>
<include // I need to pass here
android:id="#+id/button_more"
layout="#layout/button_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
And the included layout (button_more.xml):
<?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">
<data>
<variable
name="listener"
type="android.view.View.OnClickListener" />
</data>
<ImageButton
android:id="#+id/button_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?selectableItemBackgroundBorderless"
android:onClick="#{listener}"
android:padding="12dp"
android:src="#drawable/ic_more_vert_black_24dp"
app:tintColor="#{android.R.attr.textColorPrimary}" />
How to pass OnClickListener to included layout?
U can pass clickListener from parent to included layout in this way:
<include layout="#layout/button_more"
app:listener="#{clickListener}"/>

Android databinding <include> tag binding with viewModel is not working

I want to include a custom xml layout in my main layout, and bind a custom viewModel with custom xml, but it not working, The value in viewModel is not appear in xml. There is my codes:
Main.xml:
<layout>
<data>
<import type="com.example.databindingpractise.upDownchoiceWidget.UpDownChoiceViewModel"/>
<variable
name="upDownviewModel"
type="UpDownChoiceViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/upanddown"
app:upDownviewModel="#{upDownviewModel}"
layout="#layout/up_and_down_choice_layout"/>
</LinearLayout>
up_and_down_choice_layout.xml:
<layout >
<data>
<import type="com.example.databindingpractise.upDownchoiceWidget.UpDownChoiceViewModel"/>
<variable
name="upDownviewModel"
type="UpDownChoiceViewModel"/>
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/order_around_shadow">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:text="#{upDownviewModel.upLocation}"/>
</LinearLayout>
MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
LayoutInflater layoutInflater = LayoutInflater.from(this);
UpAndDownChoiceLayoutBinding upAndDownChoiceLayoutBinding = DataBindingUtil.inflate(layoutInflater, R.layout.up_and_down_choice_layout, null, false);
UpDownChoiceViewModel upDownChoiceViewModel = new UpDownChoiceViewModel();
upAndDownChoiceLayoutBinding.setUpDownviewModel(upDownChoiceViewModel);
}
How to binding the upDownChoiceViewModel with up_and_down_choice_layout.xml, Could anyone solve my question?
Your code should look like this
<layout>
<data>
<variable
name="upDownviewModel"
type="com.example.databindingpractise.upDownchoiceWidget.UpDownChoiceViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/upanddown"
app:upDownviewModel="#{upDownviewModel}"
layout="#layout/up_and_down_choice_layout"/>
</LinearLayout>
up_and_down_choice_layout
<layout >
<data>
<variable
name="upDownviewModel"
type="com.example.databindingpractise.upDownchoiceWidget.UpDownChoiceViewModel"/>
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/order_around_shadow">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:text="#{upDownviewModel.upLocation}"/>
</LinearLayout>
Use like below.
<data>
<variable
name="upDownviewModel"
type="com.example.databindingpractise.upDownchoiceWidget.UpDownChoiceViewModel" />
</data>

android-integrate databinding with BottomSheet

while integrating the BottomSheet we need to make the parent layout as the CoordinatorLayout but in databinding we use <layout>. While implementing this it throws an exception :-
Caused by: java.lang.IllegalArgumentException: The view is not a child of CoordinatorLayout.
How to integrate BottomSheet with databinding
<layout>
<data>
<import type="android.view.View" />
<variable
name="cabLayoutBinder"
type="newage.com.hopin.rideBooking.CabSelectActivity" />
<variable
name="modelBinder"
type="newage.com.hopin.rideBooking.model.DataBinders" />
<variable
name="fareSetters"
type="newage.com.hopin.rideBooking.model.FareDetails" />
</data>
<android.support.design.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".rideBooking.CabSelectActivity">
</android.support.design.widget.CoordinatorLayout>
</layout>
You put context in your coordinator, but in databinding you don't implement this way.
Try to remove this line and try again:
tools:context=".rideBooking.CabSelectActivity"
Let me know if it worked.
This is how I am using it
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".com.abc.Activity">
<data>
<variable
name="viewModel"
type=".com.abc.ViewModel" />
</data>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
layout="#layout/toolbar" />
</FrameLayout>
<!-- Adding bottom sheet after main content -->
<include layout="#layout/bottom_sheet" />
</android.support.design.widget.CoordinatorLayout>
</layout>

android get Only one layout element and one data element are allowed error when i using presenter for binding

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>

Categories

Resources