Bottom Tabs on Android. Custom tab click behaviour.

Hello,

Is there a possibility to add custom behaviour on android tab click?

What i actually need is to run some validation before opening tab and fire an event.
I've tried to find in TabbedPageRenderer but it seems that it does not expose any method or delegate which allows to add some custom tabs flow.

BottomNavigationView seems to have all functionality i need, but i don't find a way to get it in TabbedPageRenderer.

On ios i found a way to do it with UITabBarController which gives a possibility to add delegate for ShouldSelectViewController.

I'm using xamarin forms TabbedPage component which in version 3.1 supports bottom placement.

Thanks in advance for any help,
maksirol

Best Answer

Answers

  • LeoZhuLeoZhu Member ✭✭✭
    edited February 19

    you could Customizing a TabbedPageRenderer like this :

    public partial class CustomerTabbedPage : TabbedPage
      {
          public CustomerTabbedPage ()
             {
                InitializeComponent();
             }
      }
    

    in Android :

    class CustomerTabbedPageRender : TabbedPageRenderer, TabLayout.IOnTabSelectedListener
       {
           public CustomerTabbedPageRender (Context context) : base(context)
              {
              }
    
           private CustomerTabbedPage _page;
           protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
              {
                 base.OnElementChanged(e);
                 if (e.NewElement != null)
                     {
                       _page = (CustomerTabbedPage)e.NewElement;
                     }
                else
                    {
                      _page = (CustomerTabbedPage)e.OldElement;
                    }
    
              }
            async void TabLayout.IOnTabSelectedListener.OnTabSelected(TabLayout.Tab tab)
               {
                 //dosomething when you selected the tab 
               }
         }
    
  • maksirolmaksirol USMember

    Hi @LeoZhu
    Thank you for response, but

    async void TabLayout.IOnTabSelectedListener.OnTabSelected(TabLayout.Tab tab)

    It's not being called when I'm adding

    On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);

    to CustomerTabbedPage.

    Like this:

    public partial class CustomerTabbedPage : TabbedPage
      {
          public CustomerTabbedPage ()
             {
                On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
                InitializeComponent();
             }
      }
    
  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited February 22
    1. The interface you need to implement is BottomNavigationView.IOnNavigationItemSelectedListener, not TabLayout.IOnTabSelectedListener
    2. The TabbedPageRenderer already implements this interface, so you will probably want to do your implementation explicitly, and call the base class method from your implementation.
    3. You need to set the listener in the OnElementChanged method.
    4. To set the listener, you need to get a reference to the BottomNavigationView. You can do this either by using reflection to grab the base class's private reference, or traversing down the children tree of the renderer.
Sign In or Register to comment.