Mvvmcross Android Viewstub
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:
Solution 1:
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();
}
Solution 2:
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:
publicstaticvoidSetViewStubVisibilityAndInflate(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);
}
Post a Comment for "Mvvmcross Android Viewstub"