Android Data Binding - how to use ViewStub with data binding

android viewstub databinding
android viewstub example
android data binding fragment
android databinding
android viewstub recyclerview
android view viewstub cannot be cast to android view viewgroup
android binding inflate
android widget viewstub

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.

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();
    }
}

Android ViewStub using Data Binding, Android ViewStub using Data Binding. ViewStub: A ViewStub is an invisible, zero​-sized View that can be used to lazily inflate layout resources  ViewStub replaces itself in its parent with the inflated View or Views. ViewStub exists in the view hierarchy until setVisibility(int) or inflate() is invoked. You can reduce memory usage and speed up rendering by loading the views only when they are needed.

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.

ViewStubProxy, Is there anyway to use viewStubs with dataBinding ? can ViewStubProxy help ? My stub current looks like this: <ViewStub android:id="#+id/stub_import"  Home » Android » Android Data Binding – how to use ViewStub with data binding Android Data Binding – how to use ViewStub with data binding Posted by: admin May 11, 2020 Leave a comment

Just to elaborate on @andrew-classen's accepted answer above, and include the @user9113597's answer as well:

In your layout that contains the stub (e.g. my_fragment.xml)

    <data class="MyDataBinding">
        ...
    </data>

    ...
    <ViewStub 
        android:id="@+id/stub_import"
        ... />

In your activity or fragment (example below is using a fragment):

MyDataBinding mBinding;
MyViewModel mViewModel;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState)
{
    mBinding = DataBindingUtil.inflate(inflater, R.layout.my_fragment, container, false); 
    mBinding.viewStub.setOnInflateListener(new ViewStub.OnInflateListener() 
    {
        @Override
        public void onInflate(ViewStub stub, View inflated)
        {
            ViewDataBinding dataBinding = DataBindingUtil.bind(inflated);
            binding.setVariable(BR.mViewModel, mViewModel));
        }
    });
}

public void inflateViewStub(View view) {
if (!mBinding.viewStub.isInflated()) {
    mBinding.viewStub.getViewStub().inflate();
}

}

In your stub layout:

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="mViewModel"
            type="com.example.myapp.MyViewModel" />
    </data>
    ...

Android Data Binding - how to use ViewStub with data binding, Just set the listener as a document: mBinding.viewStub.setOnInflateListener(new ViewStub.OnInflateListener() { @Override public void onInflate(ViewStub stub,  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.

Another way I did this was using the ViewStubProxy. The documentation/source code for ViewStubProxy is good.

When you create your new ViewStubProxy, pass in your ViewStub in the constructor. Then ViewStubProxy.setContainingBinding(<pass in your root layout binding>). If you don't do this you'll get nulls.

Then instead of setting the ViewStub.setOnInflateListener (the ViewStubProxy will set one internally for you), set the ViewStubProxy.setOnInflateListener to set your binding variables and LifecycleOwner.

Sample code:

   private void setBindings(){
      //usual outside layout binding steps
      binding = (MeasurementsLayoutBinding) getBinding();
      binding.setViewModel(viewModel());
      binding.setLifecycleOwner(this);

      //cartSideStubView is the root layout stubview
      cartViewStubProxy = new ViewStubProxy(cartSideStubView);
      cartViewStubProxy.setContainingBinding(binding);

      cartViewStubProxy.setOnInflateListener(((viewStub, view) -> {

         cartViewStubProxy.getBinding().setVariable(BR.viewModel, viewModel());
         cartViewStubProxy.getBinding().setLifecycleOwner(this);

        //after inflation you can find your viewstub views

         cartHitchToFixedAxleInputField = view.findViewById(R.id.cart_hitch_to_fixed_axle_input_field);       


      }));

   }

‍ Android Data Binding, Best Java code snippets using android.databinding. OnInflateListener() { @​Override public void onInflate(ViewStub stub, View inflated) { mBinding. Note: The Data Binding Library generates a class named BR in the module package which contains the IDs of the resources used for data binding. In the example above, the library automatically generates the BR.item variable.

I Also faced This Type of Issues, finally I got a solution and Its Perfectly Working.

ViewStub Holding Layout,,

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data class="MBinding">

        <variable
            name="mmmmm"
            type="com.vvv.bbb.fragments.MFragment" />

    </data> 
  --------------------
  --------------------
   <ViewStub
     android:id="@+id/stub_import"
     -------------------
     -------------------
        <ViewStub/>
  --------------------
  --------------------
<layout/>

layout Inside ViewStub is,,,

<?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 class="MViewStubLayoutBinding">

        <variable
            name="mmmmm"
            type="com.vvv.bbb.fragments.MFragment" />

    </data>
  ----------------
  ----------------
  ----------------

code Inside the Class is,,,

public Class KKKKKK() {
 -----------------
MBinding mBinding;
MViewStubLayoutBinding mviewStubLayoutBinding;

 -----------------------------  
 -----------------------------

mBinding.stubImport.getViewStub().setLayoutResource(R.layout.progress_overlay);

   mBinding.stubImport.setOnInflateListener(new ViewStub.OnInflateListener() {
            @Override
            public void onInflate(ViewStub viewStub, View view) {
                mviewStubLayoutBinding=DataBindingUtil.bind(view);
            }
        });

        autoCheckBinding.vsAutoDetailsOne.getViewStub().inflate();

android.databinding.ViewStubProxy java code examples, Is there anyway to use viewStubs with dataBinding ? can ViewStubProxy help ? My stub current looks like this: <ViewStub android:id="@+id/stub_import"  The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. Layouts are often defined in activities with code that calls UI framework methods.

Android Data Binding - how to use ViewStub with data , you may not use this file except in compliance with the License. import android.​view.ViewStub.OnInflateListener;. /**. * This class represents a ViewStub before and If the inflated layout has data binding, the ViewDataBinding for the inflated. For a more in depth and longer discussion, check out the data binding android developer article. Using data binding can lead to faster development times, faster execution times and easier to read

sdk/sources/android-25/android/databinding/ViewStubProxy.java , Or how to use ViewStub with async inflation R.attr.layout, android. Where getBinding returns binding of layout from Data Binding Library. Android offers support to write declarative layouts using data binding. This minimizes the necessary code in your application logic to connect to the user interface elements. The usage of data binding requires changes in your layout files. Such layout files starts with a layout root tag followed by a data element and a view root element.

Async view stubs - Michael Spitsin, This class represents a ViewStub before and after inflation. If the inflated layout has data binding, the ViewDataBinding for the inflated View is accessible. In this video I'm going to show you how to get started with Data Binding on Android. We'll go over topics like: 1) Data binding library requirements 2) Binding an Activity layout 3) Passing

Comments
  • As a note, ViewStubBinding isn't a class, it's the binding class for the view stub. It took me awhile to figure that out; hopefully, this comment will help someone else.
  • Can not resolve a symbol "ViewStubBinding". Any solution for this?