3
votes

I am using Tabbed page in my xamarin forms project. I am trying to use OnTabReselected event in MyTabsRenderer Class in android. But the events like OnTabSelected, OnTabReselected and OnTabUnselected are not getting called. Does anyone have any solution for this.

xamarin forms version: 3.2.0.871581

VS version : 15.8.6

Here is my code snippet(MyTabsRenderer Class):

using Android.OS;
using Android.Views;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android.AppCompat;
using MyProject;
using MyProject.Droid;
using Xamarin.Forms.Platform.Android;
using System.ComponentModel;
using Android.Support.V4.View;
using Android.Content.Res;
using Android.Content;
using Android.Support.Design.Widget;

[assembly: ExportRenderer(typeof(TabController), typeof(MyTabsRenderer))]
namespace MyProject.Droid
{
    public class MyTabsRenderer : TabbedPageRenderer, TabLayout.IOnTabSelectedListener
    {


        bool setup;
        ViewPager viewPager;
        TabLayout tabLayout;

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

        protected override void SetTabIcon(TabLayout.Tab tab, FileImageSource icon)
        {
            base.SetTabIcon(tab, icon);
        }

        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {
            base.OnElementChanged(e);
        }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            //if (setup)
            //    return;


            if (e.PropertyName == "Renderer" || e.PropertyName == "CurrentPage")
            {
                viewPager = (ViewPager)ViewGroup.GetChildAt(0);
                tabLayout = (TabLayout)ViewGroup.GetChildAt(1);
                setup = true;
                ColorStateList colors = GetTabColor();

                for (int i = 0; i < tabLayout.TabCount; i++)
                {
                    var tab = tabLayout.GetTabAt(i);
                    SetTintColor(tab, colors);
                }
                tabLayout.SetOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
            }
        }


        void OnTabReselected(TabLayout.Tab tab)
        {
            // To have the logic only on he tab on position 1
            if (tab == null || tab.Position != 1)
            {
                return;
            }

            if (tab.Text == "Play")
            {
                tab.SetText("Pause");
                tab.SetIcon(Resource.Drawable.icon);
                // App.pauseCard = false;
            }
            else
            {
                tab.SetText("Play");
                tab.SetIcon(Resource.Drawable.icon);
                //  App.pauseCard = true;
            }

            SetTintColor(tab, GetTabColor());

        }

        void SetTintColor(TabLayout.Tab tab, ColorStateList colors)
        {
            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);
            }
        }

        ColorStateList GetTabColor()
        {
            return ((int)Build.VERSION.SdkInt >= 23)
                ? Resources.GetColorStateList(Resource.Color.icon_tab, Forms.Context.Theme)
                               : Resources.GetColorStateList(Resource.Color.icon_tab);
        }

    }
}
1
Try making full method name void TabLayout.IOnTabSelectedListener.OnTabReselected(TabLayout.Tab tab)R15
Are you using android:TabbedPage.ToolbarPlacement="Bottom"?FreakyAli

1 Answers

0
votes

It is probably because you are setting the OnTabSelectedListener wrong.

Try setting the listener to this instead: tabLayout.SetOnTabSelectedListener(this);

Also for inspiration check out TabbedPageRenderer.cs source code,