Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Does binding variable and passing to include tag layout work?

I have a problem with databinding. In the documentation, it said we can use include tag to host a custom layout and passing binding variable to it. When I tryout on 4.1.2 phone and emulator, the data not seems to bind but only bind the main layout fields.

This is my code of the main layout:

<layout>

<data>
    <variable
        name="Job"
        type="nz.co.certifi.CERTIFI.Model.JobModel" />
</data>

<ScrollView
    android:background="@color/TransparentColor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="false"
    android:layout_alignParentEnd="false"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true">


    <RelativeLayout
        android:background="@color/TransparentColor"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            app:Job="@{Job}"
            android:id="@+id/layoutCertification"
            layout="@layout/view_certification_control"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"/>

This is the layoutCertification:

<layout>
<data>
    <variable
        name="Job"
        type="nz.co.certifi.CERTIFI.Model.JobModel" />
    <variable
        name="Form"
        type="nz.co.certifi.CERTIFI.Model.FormROIModel" />
</data>

<nz.co.certifi.CERTIFI.Control.EditTextWithModel
    xmlns:sparkNS="http://schemas.android.com/apk/res/nz.co.certifi.CERTIFI"
    sparkNS:modelProperty="CertificateId"
    sparkNS:modelType="JobModel"
    sparkNS:validationType="required_only"
    android:contentDescription="Job: Form Certificate Id"
    sparkNS:errorRequiredMessage="@string/error_reference_no_required"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/activity_roi_step_one_hint_reference_no"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@{Job == null? Form.certificateId : Job.certificateId}"
    android:textStyle="bold"
    android:id="@+id/txtReferenceNo"
    android:layout_alignParentTop="false"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/btnReference"
    android:layout_toStartOf="@+id/btnReference"
    android:layout_centerVertical="true" />
like image 792
LittleFunny Avatar asked Dec 24 '15 08:12

LittleFunny


People also ask

Is data binding good in Android?

Data Binding allows you to effortlessly communicate across views and data sources. This pattern is important for many Android designs, including model view ViewModel (MVVM), which is currently one of the most common Android architecture patterns.

Which is better view binding or data binding in Android?

ViewBinding vs DataBindingThe main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with DataBinding due to annotation processors affecting DataBinding's build time.

What is the difference between one-way data binding and two-way data binding Android?

In one-way binding, the flow is one-directional. In a two-way binding, the flow is two-directional. This means that the flow of code is from ts file to Html file. This means that the flow of code is from ts file to Html file as well as from Html file to ts file.

Which is the correct way to reference bound data in the XML layout?

Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression.


1 Answers

Yes it does. http://developer.android.com/tools/data-binding/guide.html#includes

Main layout

<data>

    <variable
        name="plu"
        type="org.test.test.viewmodels.PluDetailViewModel" />
</data>
.
.
.
  <include
          layout="@layout/keypad_pludetail"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentStart="true"
          bind:plu="@{plu}"
                        />

Included layout

<data>

    <variable
        name="plu"
        type="org.test.test.viewmodels.PluDetailViewModel" />
</data>
.
.
.
 <Button
                android:id="@+id/keypad_accept"
                style="@style/KeyPadButton"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="@string/keypad_accept"
                android:enabled="@{plu.isOK}"
                android:onClick="@{plu.confirm}"
              />

In fragment

    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_plu_details, container, false);
    binding.setPlu(pluDetailViewModel);
    binding.executePendingBindings();
like image 85
pfulop Avatar answered Sep 22 '22 22:09

pfulop