1
votes

I recently found a class called ViewStub that can be used to "lazy-load" a layout-resource. The usage is very straight forward:

In the layout file I use:

<ViewStub
    android:id="@+id/content_stub"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

And in my code:

var stub = this.FindViewById<ViewStub>(Resource.Id.content_stub);
stub.LayoutResource = Resource.Layout.FirstView;
stub.Inflate();

However, this way the Bindings won't work! I know, that using mvvmcross I have to call BindingInflate, but that method is not available for a ViewStub I looked for something like MvxViewStub, but I couldn't find anything.

So, is there a way to somehow get the binding work with the ViewStub? Thanks for your help.

2

2 Answers

3
votes

After crawling through various mvvmcross-sources, I found a solution that seems to work. I'm not shure if thats the way how it should be done, so if someone has a better approach, please tell me.

How it works for now:

using (new MvxBindingContextStackRegistration<IMvxAndroidBindingContext>((IMvxAndroidBindingContext)this.BindingContext))
{
    var stub = this.FindViewById<ViewStub>(Resource.Id.content_stub);
    stub.LayoutInflater = this.LayoutInflater;
    stub.LayoutResource = Resource.Layout.FirstView;
    stub.Inflate();
}
0
votes

I had to do something similar, but the differences are just large enough that I thought I'd post a separate answer in case it's useful for anyone. I have a spinner, and based on the selection in that spinner a different "chunk" of UI is displayed. This leverages ViewStub's ability to automatically inflate the first time the Visibility is set to Visible.

Per the suggestion above, I made it an extension method:

public static void SetViewStubVisibilityAndInflate(this View view, MvxFragment fragment, ViewStates visibility)
{
    if (view is ViewStub && visibility == ViewStates.Visible)
    {
        var stub = (ViewStub)view;
        using (new MvxBindingContextStackRegistration<IMvxAndroidBindingContext>((IMvxAndroidBindingContext)fragment.BindingContext))
        {
            stub.LayoutInflater = fragment.Activity.LayoutInflater;
            stub.Inflate();
        }
    }

    view.Visibility = visibility;
}

And here's the code in my UI (which could be cleaner...):

private void SetCurveInputsVisibility(View view)
{
    var std = view.FindViewById(Resource.Id.CurveTypeStandard);
    var stdVis = (ViewModel.Values.CurveType == ServoCurveType.Standard ? ViewStates.Visible : ViewStates.Gone);
    std.SetViewStubVisibilityAndInflate(this, stdVis);

    var expo = view.FindViewById(Resource.Id.CurveTypeExpo);
    var expoVis = (ViewModel.Values.CurveType == ServoCurveType.Exponential ? ViewStates.Visible : ViewStates.Gone);
    expo.SetViewStubVisibilityAndInflate(this, expoVis);

    var custom = view.FindViewById(Resource.Id.CurveTypeCustom);
    var customVis = (ViewModel.Values.CurveType == ServoCurveType.Custom ? ViewStates.Visible : ViewStates.Gone);
    custom.SetViewStubVisibilityAndInflate(this, customVis);
}