Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Renderer not working anymore when tabs are navigation pages #44

Closed
jaspervanmegroot opened this issue Apr 10, 2018 · 5 comments
Closed

Comments

@jaspervanmegroot
Copy link

jaspervanmegroot commented Apr 10, 2018

Custom Renderer for TabbedPage is not working anymore (Android). layout.TabCount stays 0. How the fix this?

Version Xamarin: latest
Version plugin: latest

[assembly: ExportRenderer(typeof(MainPage), typeof(MyTabbedPageRenderer))]
public class MyTabbedPageRenderer : BadgedTabbedPageRenderer
    {
        private ViewPager viewPager;
        private MainPage page;
        private Activity activity;
        private bool setup;
        private TabLayout layout;

        public MyTabbedPageRenderer(Context context) : base(context) { }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (setup)
                return;

            if (e.PropertyName == "Renderer")
            {
                viewPager = (ViewPager)ViewGroup.GetChildAt(0);
                layout = (TabLayout)ViewGroup.GetChildAt(1);
                setup = true;

                ColorStateList colors = Resources.GetColorStateList(Resource.Color.icon_tab, Context.Theme);

                for (int i = 0; i < layout.TabCount; i++)
                {
                    var tab = layout.GetTabAt(i);
                    var icon = tab.Icon;
                    if (icon != null)
                    {
                        icon = Android.Support.V4.Graphics.Drawable.DrawableCompat.Wrap(icon);
                        Android.Support.V4.Graphics.Drawable.DrawableCompat.SetTintList(icon, colors);
                    }
                }
            }
        }

        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {
            base.OnElementChanged(e);
            activity = this.Context as Activity;

            TabLayout layout = null;
            page = (MainPage)e.NewElement;

            for (int i = 0; i < ChildCount; i++)
            {
                var v = GetChildAt(i);
                if (v is ViewPager)
                    viewPager = (ViewPager)v;
                else if (v is TabLayout)
                    layout = (TabLayout)v;
            }
            if (layout != null)
            {
                for (int tabIndex = 0; tabIndex < layout.TabCount; tabIndex++)
                    SetTabIcon(layout, tabIndex);
            }

            try
            {
                viewPager.SetPageTransformer(false, new NoAnimationPageTransformer());
            }
            catch (Exception ex)
            {
                Utils.LogError(DateTime.Now + " - " + ex.Message + " " + ex.StackTrace);
            }
        }

        private void SetTabIcon(TabLayout layout, int tabIndex)
        {
            var tab = layout.GetTabAt(tabIndex);
            tab.SetText("");

            switch (tabIndex)
            {
                case 0:
                    tab.SetIcon(Resource.Drawable.dashboard_icon);
                    break;
                case 1:
                    tab.SetIcon(Resource.Drawable.gesprekken_icon);
                    break;
                case 2:
                    tab.SetIcon(Resource.Drawable.info_icon);
                    break;
                case 3:
                    tab.SetIcon(Resource.Drawable.instellingen_icon);
                    break;
            }
        }
    }
}

MainPage:

public partial class MainPage : Xamarin.Forms.TabbedPage
    {
        public static readonly BindableProperty TabBarHiddenProperty = BindableProperty.Create("TabBarHidden", typeof(bool), typeof(MainPage), false);

        public static int DashboardPage = 0;
        public static int GesprekkenPage = 1;
        public static int InfoPage = 2;
        public static int SettingsPage = 3;
        public static int TimelinePage = -1;

        public bool TabBarHidden
        {
            get { return (bool)GetValue(TabBarHiddenProperty); }
            set { SetValue(TabBarHiddenProperty, value); }
        }

        public MainPage()
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                SetMainPage();
            });
        }

        public MainPage(int page)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                SetMainPage();
                SetCurrentPage(page);
            });
        }

        private void SetMainPage()
        {
            InitializeComponent();

            try
            {
                CreateNavigationPage(new DashboardPage(), "Dashboard", "dashboard_icon");
                CreateNavigationPage(new ContactPage(), "Gesprekken", "gesprekken_icon");
                CreateNavigationPage(new InfoPage(), "Informatie", "info_icon");
                CreateNavigationPage(new SettingsPage(), "Instellingen", "instellingen_icon");

                MessagingCenter.Subscribe<ContactPage, int>(this, "badgeCount", (sender, count) =>
                {
                    SetBadge(count);
                });
            }
            catch (Exception ex)
            {
                Utils.LogError(DateTime.Now + ": " + ex.Message + " - " + ex.StackTrace);
            }

            BarBackgroundColor = (Color)App.Current.Resources["backgroundNavigationBar"];
            BarTextColor = Color.White;
        }

        private void CreateNavigationPage(object objectType, string title, string icon)
        {
            var navigationPage = new FontNavigationPage((Page)objectType);
            navigationPage.Icon = icon;
            navigationPage.Title = title;
            navigationPage.BarBackgroundColor = (Color)App.Current.Resources["backgroundNavigationBar"];
            navigationPage.BarTextColor = Color.White;
            Children.Add(navigationPage);
        }

        public void SetBadge(int count)
        {
            if (Children.Count < 4)
                return;

            if (count == 0)
                TabBadge.SetBadgeText(Children[1], "");
            else
            {
                TabBadge.SetBadgeText(Children[1], count.ToString());
                TabBadge.SetBadgeColor(Children[1], Color.Red);
            }
        }

        public void SetCurrentPage(int page)
        {
            this.CurrentPage = Children[page];
        }

        public void OpenChatPage(ChatPage chatPage)
        {
            this.CurrentPage.Title = "";
            this.CurrentPage.Navigation.PushAsync(chatPage);
        }
    }
@xabre xabre added the bug label Apr 11, 2018
@xabre
Copy link
Owner

xabre commented Apr 11, 2018

@jaspervanmegroot could it be related to #43?

@jaspervanmegroot
Copy link
Author

Where do I need to set a delay?

@Axemasta
Copy link

Axemasta commented Apr 17, 2018

@xabre I'm having an issue with badges not being shown and this issue happens regardless of whether there is a previous screen (like in #43). I can however access the badge text of the view, and it spits out the correct number (according to the binding).

@xabre
Copy link
Owner

xabre commented Jun 25, 2018

This seems to be caused by the same issue as #46.

@xabre xabre changed the title Custom Renderer not working anymore Custom Renderer not working anymore when tabs are navigation pages Jun 25, 2018
@xabre
Copy link
Owner

xabre commented Sep 16, 2018

Check

Pre-release with XF 3.1 android bottom tab placement suport is out:

https://www.nuget.org/packages/Plugin.Badge/2.1.0-pre.2

Please give it a try and let me know if you spot some problems.

@xabre xabre closed this as completed Sep 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants