How do I get the tabreselect event with tabbar at the bottom on android?

gimkimgimkim THMember ✭✭
edited August 2018 in Xamarin.Forms

I'm trying to get an event firing when tab item of tabbedpage is reselected.

I've searched trough the forum and found that on Android, I need to create a customrenderer with

void TabLayout.IOnTabSelectedListener.OnTabReselected(TabLayout.Tab tab)
{
//do something
}

But it only triggered when the tabbar is on top.

If is set the tabbar placement to bottom with
On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
The "OnTabReselected" above won't be triggered.

So, how do I get the tabreselect event with tabbar at the bottom on android?

Thanks.

Best Answer

  • JoeMankeJoeManke US ✭✭✭✭✭
    edited August 2018 Accepted Answer

    When you use ToolbarPlacement.Bottom, the renderer creates a BottomNavigationView, not a TabLayout, so you need to use the BottomNavigationView.IOnNavigationItemReselectedListener interface instead of TabLayout.IOnTabSelectedListener.

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited August 2018 Accepted Answer

    When you use ToolbarPlacement.Bottom, the renderer creates a BottomNavigationView, not a TabLayout, so you need to use the BottomNavigationView.IOnNavigationItemReselectedListener interface instead of TabLayout.IOnTabSelectedListener.

  • RedoRedo IDMember ✭✭

    Why it's not working for me? Nothing happen

  • IRONicMANIRONicMAN Member ✭✭
    edited January 28

    You need to call SetOnNavigationItemReselectedListener in OnElementChanged and set it to the Custom Renderer

    `public class CustomTabbedPageRenderer : TabbedPageRenderer, BottomNavigationView.IOnNavigationItemReselectedListener
    {
    public CustomTabbedPageRenderer(Context context)
    : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
    {
        base.OnElementChanged(e);
    
        if(e.OldElement == null && e.NewElement != null)
        {
            for (int i = 0; i <= this.ViewGroup.ChildCount - 1; i++)
            {
                var childView = this.ViewGroup.GetChildAt(i);
                if(childView is ViewGroup viewGroup)
                {
                    for (int j = 0; j <= viewGroup.ChildCount - 1; j++)
                    {
                        var childRelativeLayoutView = viewGroup.GetChildAt(j);
                        if(childRelativeLayoutView is BottomNavigationView bottomNavigationView)
                        {
                            bottomNavigationView.SetOnNavigationItemReselectedListener(this);
                        }
                    }
                }
            }
        }
    }
    
    void BottomNavigationView.IOnNavigationItemReselectedListener.OnNavigationItemReselected(IMenuItem item)
    {
        // Do whatever your want here
    }
    

    }`

Sign In or Register to comment.