Esempio n. 1
0
        protected override void OnElementChanged(ElementChangedEventArgs <TabbedPage> e)
        {
            base.OnElementChanged(e);

            _tabLayout = ViewGroup.FindChildOfType <TabLayout>();

            if (_tabLayout == null)
            {
                Console.WriteLine("No TabLayout found. Badge not added.");
                return;
            }

            _tabbedPage = e.NewElement as TabbedPage;
            _viewPager  = (ViewPager)GetChildAt(0);

            _tabLayout.TabSelected += (s, a) => {
                var page = _tabbedPage.Children[a.Tab.Position];

                if (page is TabbedPage)
                {
                    var tabPage = (TabbedPage)page;
                    SetTab(a.Tab, tabPage.Icon.File);
                }

                _viewPager.SetCurrentItem(a.Tab.Position, false);
            };

            _tabLayout.TabUnselected += (s, a) => {
                var page = _tabbedPage.Children[a.Tab.Position];

                if (page is TabbedPage)
                {
                    SetTab(a.Tab, page.Icon.File);
                }
            };

            _tabView = _tabLayout.FindChildOfType <TabLayout.TabView>();

            for (var i = 0; i < _tabLayout.TabCount; i++)
            {
                AddTabBadge(i);
            }

            Element.ChildAdded   += OnTabAdded;
            Element.ChildRemoved += OnTabRemoved;
        }
Esempio n. 2
0
        public static void ApplyBadge(this TabLayout.TabView tabView, XColor color, string text,
                                      XColor textColor)
        {
            if (!tabView.GetChildrenOfType <BadgeFrameLayout>().Any())
            {
                tabView.SetClipChildren(false);
                tabView.SetClipToPadding(false);

                TextView tabTextView = tabView.GetChildrenOfType <TextView>().Single();
                tabView.RemoveView(tabTextView);

                FrameLayout badgeContainerFrameLayout = new FrameLayout(tabView.Context)
                {
                    LayoutParameters = new FrameLayout.LayoutParams(LP.WrapContent, LP.WrapContent)
                };

                badgeContainerFrameLayout.AddView(tabTextView);

                BadgeFrameLayout badgeContainer = CreateBadgeContainer(tabView.Context);
                badgeContainer.Visibility = !string.IsNullOrEmpty(text) ? ViewStates.Visible : ViewStates.Invisible;

                badgeContainer.Background = CreateBadgeBackground(tabView.Context, color);
                badgeContainer.AddView(CreateBadgeText(tabView.Context, text, textColor));

                badgeContainerFrameLayout.AddView(badgeContainer);

                tabView.AddView(badgeContainerFrameLayout);
            }
            else
            {
                BadgeFrameLayout badgeContainer = tabView.GetChildrenOfType <BadgeFrameLayout>().Single();
                badgeContainer.Visibility = !string.IsNullOrEmpty(text) ? ViewStates.Visible : ViewStates.Invisible;

                ((PaintDrawable)badgeContainer.Background).Paint.Color = color.IsDefault ? XColor.FromRgb(255, 59, 48).ToAndroid() : color.ToAndroid();

                TextView textView = (TextView)badgeContainer.GetChildAt(0);
                textView.Text = text;
                textView.SetTextColor(textColor.IsDefault ? XColor.White.ToAndroid() : textColor.ToAndroid());
            }
        }