Search code examples
androiddata-bindingmvvmcross

Binding won't update TabLayout custom title view


In MvvmCross 4.x I created a Android TabLayout with custom Views as title/header. The custom Views are bound to some text and a number (displayed as badge).

It all works fine. However, when updating (increasing) the number for the badge the tab header view won't follow. I needed to create a custom binding for the headers, but I think I did something wrong here.

I use the following code after the tab Fragment and ViewModels are created:

var bindingContext = new MvxAndroidBindingContext(Context, (IMvxLayoutInflaterHolder)Context,
    FragmentStatePagerAdapter.TryGetTabViewModel(tabIndex));
var view =  bindingContext.BindingInflate(tabTitleResourceId, null);

var tab = TabLayout.GetTabAt(tabIndex);
tab.SetCustomView(view);

Then the View (tab headers) get refreshed once, but then no more.

Any ideas why the Views are not updated?

I use MvxFragment (from Support library) combined with a MvxCachingFragmentStatePagerAdapter, if that matters.


Solution

  • To solution was keeping the MvxAndroidBindingContext alive somewhere.

    So, the following not great code should work.

    var bindingContext = new MvxAndroidBindingContext(Context(IMvxLayoutInflaterHolder)Context,
    FragmentStatePagerAdapter.TryGetTabViewModel(tabIndex));
    tabCachedBindingsList.Add(bindingContext);
    var view =  bindingContext.BindingInflate(tabTitleResourceId, null);
    
    var tab = TabLayout.GetTabAt(tabIndex);
    tab.SetCustomView(view);