Search code examples
xamarinxamarin.formsxamarin.androidcustom-renderer

Select a disabled tab firing TabSelected event indefinitely on Custom TabbedPage renderer


Hello I'm trying to disable certain tabs based on a property in a custom Xamarin.Forms content page. I added a handler to the TabLayout's TabSelected event to keep on the current selected tab if the other tab can not be selected. However, the event keeps fire indefinitely. I made pretty sure that it's only getting registered once so I'm not sure why it does so

public class ExtendedTabbedPageRenderer : TabbedPageRenderer
{
    public ExtendedTabbedPageRenderer(Context context) : base(context)
    {
    }

    private TabLayout _tabLayout;
    ViewPager _viewPager;
    private IList<ContentTab> _customTabs;
    private int _lastSelectedPosition;

    protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
    {
        base.OnElementChanged(e);
        for (int i = 0; i < ChildCount; i++)
        {
            var view = GetChildAt(i);
            if (view is TabLayout layout)
                _tabLayout = layout;
            else if (view is ViewPager pager)
                _viewPager = pager;
        }

        _customTabs = e.NewElement.Children.Select(p => (ContentTab)p).ToList();
        //_tabLayout.AddOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(_viewPager));
        _tabLayout.TabSelected += TabLayoutOnTabSelected;
        List<TabLayout.Tab> tt = new List<TabLayout.Tab>();
        for (int i = 0; i < _tabLayout.TabCount; i++)
        {
            tt.Add(_tabLayout.GetTabAt(i));
        }
    }

    private void TabLayoutOnTabSelected(object sender, TabLayout.TabSelectedEventArgs tabSelectedEventArgs)
    {
        var tab = tabSelectedEventArgs.Tab;
        if(tab.Position.Equals(_lastSelectedPosition))
            return;

        var customTab = _customTabs[tab.Position];

        if (customTab.EnableUserInteraction)
        {
            tab.Select();
            _viewPager.SetCurrentItem(tab.Position, true);
            _lastSelectedPosition = tab.Position;
            return;
        }

        _viewPager.SetCurrentItem(_lastSelectedPosition, false);
        var lastTab = _tabLayout.GetTabAt(_lastSelectedPosition);
        lastTab.Select();
    }

Solution

  • Okay as I inspected through the TabbedPageRenderer source code, I found out that it has a private implementation for TabLayout.IOnTabSelectedListener which causes it to always try to select the tab once the user interacted with the the solution was to call

    _tabLayout.ClearOnTabSelectedListeners();
    

    on the OnElementChanged