Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Data Binding - how to use ViewStub with data binding

Tags:

Is there anyway to use viewStubs with dataBinding ? can ViewStubProxy help ?

My stub current looks like this:

    <ViewStub   android:id="@+id/stub_import"   android:inflatedId="@+id/panel_import"    android:layout="@layout/progress_overlay"    android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:layout_gravity="@{myobject.bottom ? bottom:top}" /> 

But this layout will be replaced when i inflate the viewStub so how can ViewStubs be used with android dataBinding ?

this is what i see from the docs:

ViewStubs

ViewStubs are a little different from normal Views. They start off invisible and when they either are made visible or are explicitly told to inflate, they replace themselves in the layout by inflating another layout.

Because the ViewStub essentially disappears from the View hierarchy, the View in the binding object must also disappear to allow collection. Because the Views are final, a ViewStubProxy object takes the place of the ViewStub, giving the developer access to the ViewStub when it exists and also access to the inflated View hierarchy when the ViewStub has been inflated.

When inflating another layout, a binding must be established for the new layout. Therefore, the ViewStubProxy must listen to the ViewStub's ViewStub.OnInflateListener and establish the binding at that time. Since only one can exist, the ViewStubProxy allows the developer to set an OnInflateListener on it that it will call after establishing the binding.

like image 396
j2emanue Avatar asked Jan 11 '16 01:01

j2emanue


People also ask

Is Data Binding good in Android?

Using data binding can lead to faster development times, faster execution times and more readable and maintained code. Android data binding generates binding classes at compile time for layouts.

What is one way data binding in Android?

In one-way binding, the data flow is one-directional. This means that the flow of code is from typescript file to Html file.

What is view stub?

android.view.ViewStub. A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or when inflate() is invoked, the layout resource is inflated. The ViewStub then replaces itself in its parent with the inflated View or Views.


2 Answers

Just set the listener as the doc says :

mBinding.viewStub.setOnInflateListener(new ViewStub.OnInflateListener() {     @Override     public void onInflate(ViewStub stub, View inflated) {         ViewStubBinding binding = DataBindingUtil.bind(inflated);         binding.setModel(model);     } });    public void inflateViewStub(View view) {     if (!mBinding.viewStub.isInflated()) {         mBinding.viewStub.getViewStub().inflate();     } } 
like image 113
Andre Classen Avatar answered Sep 28 '22 20:09

Andre Classen


Declare your xml namespace, and pass the variable through that. This works with <include>, too, btw. Here's an example:

main_layout.xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:my-namespace="http://schemas.android.com/apk/res-auto">     <data>         <variable name="myData" type="com.example.SomeModel" />     </data>      <RelativeLayout         android:layout_width="wrap_content"         android:layout_height="wrap_content">          <ViewStub             android:id="@+id/view_stub"             android:inflatedId="@+id/view"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout="@layout/another_layout"             my-namespace:data="@{myData}"             />      </RelativeLayout> </layout> 

another_layout.xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android"> <!-- No need to declare my-namespace here -->     <data>         <variable name="data" type="com.example.SomeModel" />     </data>      <RelativeLayout         android:layout_width="wrap_content"         android:layout_height="wrap_content">          <TextView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@{data.someValue}" />      </RelativeLayout> </layout> 

So now you just have to call inflate() and the layout will have the data. You can check the generated binding class to verify this. It even has type safety, so you can't pass any other type into data.

like image 35
Vedavyas Bhat Avatar answered Sep 28 '22 20:09

Vedavyas Bhat