protected override void OnElementChanged(ElementChangedEventArgs <TabbedPage> e)
        {
            base.OnElementChanged(e);

            BottomTabbedPage formsPage = (BottomTabbedPage)Element;

            if (!formsPage.Labels)
            {
                var childViews = ViewGroup.GetViewsByType(typeof(BottomNavigationItemView));

                foreach (var childView in childViews)
                {
                    childView.FindAndRemoveById(Resource.Id.largeLabel);
                    childView.FindAndRemoveById(Resource.Id.smallLabel);
                }
            }

            if (e.NewElement != null)
            {
                var relativeLayout = this.GetChildAt(0) as Android.Widget.RelativeLayout;
                if (relativeLayout != null)
                {
                    var bottomNavigationView = relativeLayout.GetChildAt(1) as BottomNavigationView;
                    bottomNavigationView.SetShiftMode(false, false);

                    BottomNavigationMenuView bottomNavigationMenuView = (BottomNavigationMenuView)bottomNavigationView.GetChildAt(0);

                    int tabCount = formsPage.Tabs.Count;

                    for (int i = 0; i < tabCount; i++)
                    {
                        var tabData = formsPage.Tabs[0];
                        BottomNavigationItemView tabItemView = (BottomNavigationItemView)bottomNavigationMenuView.GetChildAt(i);

                        if (tabData.BadgeCaption > 0)
                        {
                            if (_badgeId == 0)
                            {
                                _badgeId = Android.Views.View.GenerateViewId();
                            }

                            TextView badgeTextView = new BadgeView(Context)
                            {
                                Id = _badgeId, BadgeCaption = tabData.BadgeCaption.ToString(), BadgeColor = tabData.BadgeColor.ToAndroid()
                            };

                            tabData.PropertyChanged += (sender, args) =>
                            {
                                TabData currentTabData = (TabData)sender;
                                BottomNavigationItemView currentTabItemView = _tabViews[currentTabData];

                                BadgeView currentBadgeTextView = currentTabItemView.FindViewById(_badgeId) as BadgeView;

                                if (currentBadgeTextView != null)
                                {
                                    currentBadgeTextView.BadgeColor   = currentTabData.BadgeColor.ToAndroid();
                                    currentBadgeTextView.BadgeCaption = currentTabData.BadgeCaption > 0 ? currentTabData.BadgeCaption.ToString() : string.Empty;
                                }
                            };

                            tabItemView.AddView(badgeTextView);
                            _tabViews.Add(tabData, tabItemView);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void UpgradeTabbedItemsAppereance()
        {
            if (bottomTabBarItemsStateList == null)
            {
                InitializeBottomTabBarItemsStateList();
            }

            if (ViewGroup != null && ViewGroup.ChildCount > 0)
            {
                BottomNavigationView bottomNavigationView = FindChildOfType <BottomNavigationView>(ViewGroup);
                bottomNavigationView.SetShiftMode(false, false);
                BottomNavigationMenuView bottomNavigationMenuView = FindChildOfType <BottomNavigationMenuView>(ViewGroup);

                for (int i = 0; i < bottomNavigationMenuView.ChildCount; i++)
                {
                    BottomNavigationItemView item = (BottomNavigationItemView)bottomNavigationMenuView.GetChildAt(i);
                    item.SetTextColor(bottomTabBarItemsStateList);
                    item.SetShifting(false);
                    item.SetLabelVisibilityMode(LabelVisibilityMode.LabelVisibilityLabeled);
                    item.SetChecked(item.ItemData.IsChecked);

                    Android.Views.View activeLabel = item.FindViewById(Resource.Id.largeLabel);
                    if (activeLabel is TextView)
                    {
                        var font = Typeface.CreateFromAsset(_context.Assets, "Montserrat-SemiBold.ttf");
                        (activeLabel as TextView).Typeface = font;
                    }

                    Android.Views.View inActiveLabel = item.FindViewById(Resource.Id.smallLabel);
                    if (activeLabel is TextView)
                    {
                        var font = Typeface.CreateFromAsset(_context.Assets, "Montserrat-SemiBold.ttf");
                        (inActiveLabel as TextView).Typeface = font;
                    }
                }
            }

            T FindChildOfType <T>(ViewGroup viewGroup) where T : Android.Views.View
            {
                if (viewGroup == null || viewGroup.ChildCount == 0)
                {
                    return(null);
                }

                for (var i = 0; i < viewGroup.ChildCount; i++)
                {
                    var child = viewGroup.GetChildAt(i);

                    var typedChild = child as T;
                    if (typedChild != null)
                    {
                        return(typedChild);
                    }

                    if (!(child is ViewGroup))
                    {
                        continue;
                    }

                    var result = FindChildOfType <T>(child as ViewGroup);

                    if (result != null)
                    {
                        return(result);
                    }
                }

                return(null);
            }
        }