static void EnsureTabIndex(BottomTabbedRenderer renderer)
        {
            var rootLayout = (RelativeLayout)renderer.GetChildAt(0);
            var bottomNav  = (BottomNavigationViewEx)rootLayout.GetChildAt(1);
            var menu       = (BottomNavigationMenu)bottomNav.Menu;

            var itemIndex = menu.FindItemIndex(bottomNav.SelectedItemId);
            var pageIndex = renderer.Element.Children.IndexOf(renderer.Element.CurrentPage);

            if (pageIndex >= 0 && pageIndex != itemIndex && pageIndex < bottomNav.ItemCount)
            {
                var menuItem = menu.GetItem(pageIndex);
                bottomNav.SelectedItemId = menuItem.ItemId;

                if (BottomTabbedRenderer.ShouldUpdateSelectedIcon && BottomTabbedRenderer.MenuItemIconSetter != null)
                {
                    BottomTabbedRenderer.MenuItemIconSetter?.Invoke(menuItem, renderer.Element.CurrentPage.Icon, true);

                    if (renderer.LastSelectedIndex != pageIndex)
                    {
                        var lastSelectedPage     = renderer.Element.Children[renderer.LastSelectedIndex];
                        var lastSelectedMenuItem = menu.GetItem(renderer.LastSelectedIndex);
                        BottomTabbedRenderer.MenuItemIconSetter?.Invoke(lastSelectedMenuItem, lastSelectedPage.Icon, false);
                        renderer.LastSelectedIndex = pageIndex;
                    }
                }
                else if (renderer.LastSelectedIndex != pageIndex)
                {
                    renderer.LastSelectedIndex = pageIndex;
                }
            }
        }
        public static void Layout(this BottomTabbedRenderer renderer, int width, int height)
        {
            var rootLayout = (RelativeLayout)renderer.GetChildAt(0);
            var bottomNav  = (BottomNavigationViewEx)rootLayout.GetChildAt(1);

            var Context = renderer.Context;

            rootLayout.Measure(
                MeasureSpecFactory.MakeMeasureSpec(width, MeasureSpecMode.Exactly),
                MeasureSpecFactory.MakeMeasureSpec(height, MeasureSpecMode.AtMost));

            ((IPageController)renderer.Element).ContainerArea = Context.CreateRect(rootLayout.MeasuredWidth, rootLayout.GetChildAt(0).MeasuredHeight);

            rootLayout.Measure(
                MeasureSpecFactory.MakeMeasureSpec(width, MeasureSpecMode.Exactly),
                MeasureSpecFactory.MakeMeasureSpec(height, MeasureSpecMode.Exactly));
            rootLayout.Layout(0, 0, rootLayout.MeasuredWidth, rootLayout.MeasuredHeight);

            if (renderer.Element.Children.Count == 0)
            {
                return;
            }

            int tabsHeight = bottomNav.MeasuredHeight;

            var item = (ViewGroup)bottomNav.GetChildAt(0);

            item.Measure(
                MeasureSpecFactory.MakeMeasureSpec(width, MeasureSpecMode.Exactly),
                MeasureSpecFactory.MakeMeasureSpec(tabsHeight, MeasureSpecMode.Exactly));

            item.Layout(0, 0, width, tabsHeight);

            return;
        }
        public static void Layout(this BottomTabbedRenderer renderer, int width, int height)
        {
            var rootLayout = (RelativeLayout)renderer.GetChildAt(0);
            var bottomNav  = (BottomNavigationViewEx)rootLayout.GetChildAt(1);

            var Context = renderer.Context;

            rootLayout.Measure(
                MeasureSpecFactory.MakeMeasureSpec(width, MeasureSpecMode.Exactly),
                MeasureSpecFactory.MakeMeasureSpec(height, MeasureSpecMode.AtMost));

            ((IPageController)renderer.Element).ContainerArea = Context.CreateRect(rootLayout.MeasuredWidth, rootLayout.GetChildAt(0).MeasuredHeight);

            rootLayout.Measure(
                MeasureSpecFactory.MakeMeasureSpec(width, MeasureSpecMode.Exactly),
                MeasureSpecFactory.MakeMeasureSpec(height, MeasureSpecMode.Exactly));
            rootLayout.Layout(0, 0, rootLayout.MeasuredWidth, rootLayout.MeasuredHeight);

            if (renderer.Element.Children.Count == 0)
            {
                return;
            }

            int tabsHeight = bottomNav.MeasuredHeight;

            var item = (ViewGroup)bottomNav.GetChildAt(0);

            item.Measure(
                MeasureSpecFactory.MakeMeasureSpec(width, MeasureSpecMode.Exactly),
                MeasureSpecFactory.MakeMeasureSpec(tabsHeight, MeasureSpecMode.Exactly));

            item.Layout(0, 0, width, tabsHeight);


            var menuItems = bottomNav.GetBottomNavigationItemViews();
            var count     = menuItems.Length;

            if (count == 0)
            {
                return;
            }
            var itemWidth = width / count;

            for (int i = 0; i < count; i++)
            {
                var menu = menuItems[i];
                menu.Measure(
                    MeasureSpecFactory.MakeMeasureSpec(itemWidth, MeasureSpecMode.Exactly),
                    MeasureSpecFactory.MakeMeasureSpec(tabsHeight, MeasureSpecMode.Exactly));
                menu.Layout(i * itemWidth, 0, itemWidth * (i + 1), tabsHeight);
            }
        }
Ejemplo n.º 4
0
        static void EnsureTabIndex(BottomTabbedRenderer renderer)
        {
            var rootLayout = (RelativeLayout)renderer.GetChildAt(0);
            var bottomNav  = (BottomNavigationViewEx)rootLayout.GetChildAt(1);
            var menu       = (BottomNavigationMenu)bottomNav.Menu;

            var itemIndex = menu.FindItemIndex(bottomNav.SelectedItemId);
            var pageIndex = renderer.Element.Children.IndexOf(renderer.Element.CurrentPage);

            if (pageIndex >= 0 && pageIndex != itemIndex && pageIndex < bottomNav.ItemCount)
            {
                bottomNav.SelectedItemId = menu.GetItem(pageIndex).ItemId;
            }
        }
        public static void SwitchPage(this BottomTabbedRenderer renderer, IMenuItem item)
        {
            var rootLayout = (RelativeLayout)renderer.GetChildAt(0);
            var bottomNav  = (BottomNavigationViewEx)rootLayout.GetChildAt(1);
            var menu       = (BottomNavigationMenu)bottomNav.Menu;

            var index            = menu.FindItemIndex(item.ItemId);
            var pageIndex        = index % renderer.Element.Children.Count;
            var currentPageIndex = renderer.Element.Children.IndexOf(renderer.Element.CurrentPage);

            if (currentPageIndex != pageIndex)
            {
                renderer.Element.CurrentPage = renderer.Element.Children[pageIndex];
            }
        }
        public static void SwitchPage(this BottomTabbedRenderer renderer, IMenuItem item)
        {
            var rootLayout = (RelativeLayout)renderer.GetChildAt(0);
            var bottomNav  = (BottomNavigationViewEx)rootLayout.GetChildAt(1);
            var menu       = (BottomNavigationMenu)bottomNav.Menu;

            renderer.CurrentMenuItemId = item.ItemId;
            var index            = menu.FindItemIndex(item.ItemId);
            var pageIndex        = index % renderer.Element.Children.Count;
            var currentPageIndex = renderer.Element.Children.IndexOf(renderer.Element.CurrentPage);

            if (pageIndex == currentPageIndex)
            {
                if (renderer.Element.CurrentPage is NavigationPage nav)
                {
                    nav.Navigation.PopToRootAsync();
                }
            }
            else
            {
                renderer.Element.CurrentPage = renderer.Element.Children[pageIndex];
            }
        }
        public static void Layout(this BottomTabbedRenderer renderer, int width, int height)
        {
            var rootLayout = (RelativeLayout)renderer.GetChildAt(0);
            var bottomNav  = (BottomNavigationViewEx)rootLayout.GetChildAt(1);

            var Context = renderer.Context;

            rootLayout.Measure(
                MeasureSpecFactory.MakeMeasureSpec(width, MeasureSpecMode.Exactly),
                MeasureSpecFactory.MakeMeasureSpec(height, MeasureSpecMode.AtMost));

            ((IPageController)renderer.Element).ContainerArea = Context.CreateRect(rootLayout.MeasuredWidth, rootLayout.GetChildAt(0).MeasuredHeight);

            rootLayout.Measure(
                MeasureSpecFactory.MakeMeasureSpec(width, MeasureSpecMode.Exactly),
                MeasureSpecFactory.MakeMeasureSpec(height, MeasureSpecMode.Exactly));
            rootLayout.Layout(0, 0, rootLayout.MeasuredWidth, rootLayout.MeasuredHeight);

            if (renderer.Element.Children.Count == 0)
            {
                return;
            }

            int tabsHeight = bottomNav.MeasuredHeight;

            var item = (ViewGroup)bottomNav.GetChildAt(0);

            item.Measure(
                MeasureSpecFactory.MakeMeasureSpec(width, MeasureSpecMode.Exactly),
                MeasureSpecFactory.MakeMeasureSpec(tabsHeight, MeasureSpecMode.Exactly));

            item.Layout(0, 0, width, tabsHeight);
            int item_w = width / item.ChildCount;

            for (int i = 0; i < item.ChildCount; i++)
            {
                var frame = (FrameLayout)item.GetChildAt(i);
                frame.Measure(
                    MeasureSpecFactory.MakeMeasureSpec(item_w, MeasureSpecMode.Exactly),
                    MeasureSpecFactory.MakeMeasureSpec(tabsHeight, MeasureSpecMode.Exactly));
                frame.Layout(i * item_w, 0, i * item_w + item_w, tabsHeight);

                var imgView    = bottomNav.GetIconAt(i);
                var baselayout = frame.GetChildAt(1);
                if (BottomTabbedRenderer.VisibleTitle == false)
                {
                    baselayout.Visibility = ViewStates.Gone;
                    //Icon Height
                    int imgH = imgView.LayoutParameters.Height;
                    //Icon Width
                    int imgW = Math.Min(imgView.LayoutParameters.Width, item_w - (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Left) - (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Right));

                    int imgTop  = (tabsHeight - imgH - (int)Context.ToPixels(BottomTabbedRenderer.ItemSpacing)) / 2;
                    int imgLeft = (item_w - imgW) / 2;

                    switch (BottomTabbedRenderer.ItemAlign)
                    {
                    case ItemAlignFlags.Default:
                    case ItemAlignFlags.Top:
                        imgTop = (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Top);
                        break;

                    case ItemAlignFlags.Bottom:
                        imgTop = tabsHeight - imgH - (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Bottom);
                        break;
                    }
                    //layout icon
                    imgView.Measure(MeasureSpecFactory.MakeMeasureSpec(imgW, MeasureSpecMode.Exactly), MeasureSpecFactory.MakeMeasureSpec(imgH, MeasureSpecMode.Exactly));
                    imgView.Layout(imgLeft, imgTop, imgW + imgLeft, imgH + imgTop);
                    continue;
                }

                if (baselayout != null)
                {
                    baselayout.Visibility = ViewStates.Visible;
                    if (baselayout.GetType() == typeof(BaselineLayout))
                    {
                        //Container text
                        var basel = (BaselineLayout)baselayout;
                        //Small text
                        var small = bottomNav.GetSmallLabelAt(i);
                        //Large text
                        var large = bottomNav.GetLargeLabelAt(i);

                        //Height Container text
                        int baselH = Math.Max(small.Height, large.Height);
                        //width Container text
                        int baselW = Math.Min(small.Width, item_w - (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Left) - (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Right));
                        //Icon Height
                        int imgH = imgView.LayoutParameters.Height;
                        //Icon Width
                        int imgW = Math.Min(imgView.LayoutParameters.Width, item_w - (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Left) - (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Right));

                        int imgTop       = (tabsHeight - imgH - baselH - (int)Context.ToPixels(BottomTabbedRenderer.ItemSpacing)) / 2;
                        int imgLeft      = (item_w - imgW) / 2;
                        int topBaseLine  = imgTop + imgH + (int)Context.ToPixels(BottomTabbedRenderer.ItemSpacing);
                        int leftBaseLine = (item_w - baselW) / 2;

                        switch (BottomTabbedRenderer.ItemAlign)
                        {
                        case ItemAlignFlags.Default:
                            imgTop      = (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Top);
                            topBaseLine = tabsHeight - baselH - (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Bottom);
                            break;

                        case ItemAlignFlags.Top:
                            imgTop      = (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Top);
                            topBaseLine = imgTop + imgH + (int)Context.ToPixels(BottomTabbedRenderer.ItemSpacing);
                            break;

                        case ItemAlignFlags.Bottom:
                            imgTop      = tabsHeight - imgH - baselH - (int)Context.ToPixels(BottomTabbedRenderer.ItemSpacing) - (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Bottom);
                            topBaseLine = imgTop + imgH + (int)Context.ToPixels(BottomTabbedRenderer.ItemSpacing);
                            break;
                        }
                        //layout icon, text
                        imgView.Measure(MeasureSpecFactory.MakeMeasureSpec(imgW, MeasureSpecMode.Exactly), MeasureSpecFactory.MakeMeasureSpec(imgH, MeasureSpecMode.Exactly));
                        imgView.Layout(imgLeft, imgTop, imgW + imgLeft, imgH + imgTop);
                        basel.Measure(MeasureSpecFactory.MakeMeasureSpec(baselW, MeasureSpecMode.Exactly), MeasureSpecFactory.MakeMeasureSpec(tabsHeight, MeasureSpecMode.Exactly));
                        basel.Layout(leftBaseLine, topBaseLine, leftBaseLine + baselW, topBaseLine + baselH);

                        //text break
                        var breaktext = small.Paint.BreakText(small.Text, true, item_w - (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Right) - (int)Context.ToPixels(BottomTabbedRenderer.ItemPadding.Left), null);
                        var text      = small.Text;
                        if (text.Length > breaktext)
                        {
                            small.Text = text.Substring(0, breaktext - 1);
                            large.Text = text.Substring(0, breaktext - 1);
                        }
                    }
                }
            }
        }