2

I implemented a tabbed application with Xamarin.Android using TabHost and MvxTabsFragmentActivity. I want to refresh the current tab when clicking on it the second time.

Is there any method like OnTabReselected from Android?

That's how I am creating the tabs, using TabHost:

    <TabHost android:id="@android:id/tabhost"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:background="@color/white">
      <LinearLayout android:orientation="vertical"
                    android:layout_width="match_parent" 
                    android:layout_height="match_parent">
        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="7dp"
          android:background="@drawable/gradient_border_top"
          android:orientation="horizontal" />
        <TabWidget android:id="@android:id/tabs"
                   android:orientation="horizontal"
                   android:layout_width="match_parent" 
                   android:layout_height="56dp"
                   android:layout_weight="0"
                   android:background="@color/white" />
        <FrameLayout android:id="@android:id/tabcontent"
                     android:layout_width="0dp"
                     android:layout_height="0dp"
                     android:layout_weight="0" />
      </LinearLayout>
    </TabHost>

And MainView:

    [Activity(Theme = "@style/MyTheme", ScreenOrientation = ScreenOrientation.Portrait, WindowSoftInputMode = SoftInput.AdjustPan)]
    public class MainView : MvxTabsFragmentActivity
    {
        public MainView() : base(Resource.Layout.main_layout, Resource.Id.actualtabcontent)
        {
        }

        private static TabHost TabHost { get; set; }

        public override void OnTabChanged(string tag)
        {
            var pos = TabHost.CurrentTab;

            var tabView = TabHost.TabWidget.GetChildTabViewAt(pos);
            tabView.FindViewById<ImageView>(Resource.Id.tabImage)
                .SetColorFilter(new Color(ContextCompat.GetColor(Application.Context, Resource.Color.tabColorSelected)));
            tabView.FindViewById<TextView>(Resource.Id.tabTitle)
                .SetTextColor(new Color(ContextCompat.GetColor(Application.Context, Resource.Color.tabColorSelected)));

            for (var i = 0; i < TabHost.TabWidget.ChildCount; i++)
            {
                if (pos != i)
                {
                    var tabViewUnselected = TabHost.TabWidget.GetChildTabViewAt(i);
                    tabViewUnselected.FindViewById<ImageView>(Resource.Id.tabImage)
                        .SetColorFilter(new Color(ContextCompat.GetColor(Application.Context, Resource.Color.tabColor)));
                    tabViewUnselected.FindViewById<TextView>(Resource.Id.tabTitle)
                        .SetTextColor(new Color(ContextCompat.GetColor(Application.Context, Resource.Color.tabColor)));
                }
            }

            base.OnTabChanged(tag);
            }
        }

// This is where I add all 4 tabs
        protected override void AddTabs(Bundle args)
        {
            AddTab<TrackHomeView>(
                args,
                Mvx.IoCProvider.IoCConstruct<TrackHomeViewModel>(),
                CreateTabFor(((int)TabIdentifier.TrackTab).ToString(), Resource.Drawable.ic_track_icon, Strings.Track));

            AddTab<SendView>(
                args,
                Mvx.IoCProvider.IoCConstruct<SendViewModel>(),
                CreateTabFor(((int)TabIdentifier.SendTab).ToString(), Resource.Drawable.ic_send_icon, Strings.Send));

            if (MainViewModel.IsUserLoggedIn())
            {
                AddTab<ProfileView>(
                    args,
                    Mvx.IoCProvider.IoCConstruct<ProfileViewModel>(),
                    CreateTabFor(((int)TabIdentifier.ProfileTab).ToString(), Resource.Drawable.ic_profile_icon, Strings.Profile));
            }
            else
            {
                AddTab<CreateAccountView>(
                    args,
                    Mvx.IoCProvider.IoCConstruct<CreateAccountViewModel>(),
                    CreateTabFor(((int)TabIdentifier.ProfileTab).ToString(), Resource.Drawable.ic_profile_icon, Strings.Profile));
            }

            AddTab<MoreView>(
                args,
                Mvx.IoCProvider.IoCConstruct<MoreViewModel>(),
                CreateTabFor(((int)TabIdentifier.MoreTab).ToString(), Resource.Drawable.ic_more_icon, Strings.More));
        }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            TabHost = FindViewById<TabHost>(global::Android.Resource.Id.TabHost);
            TabHost.TabWidget.SetDividerDrawable(null);
            TabHost.Setup();

        }

I added only relevant code. The tabs are created using TabHost, and the Activity is inherited from MvxTabsFragmentActivity.

Kam
  • 391
  • 1
  • 3
  • 12
  • Ignore what I said before, I learnt something new today that I thought was impossible. Try this https://stackoverflow.com/questions/43045672/tab-change-listener-android – Saamer Oct 15 '19 at 14:13
  • I used TabHost because MvxTabsFragmentActivity uses TabHost, not TabLayout. – Kam Oct 15 '19 at 14:23

1 Answers1

1

You can inherit the ActionBar.ITabListener interface in your TabHost's MvxTabsFragmentActivity and then you can get the events that you need.

public class MainActivity : MvxTabsFragmentActivity, ActionBar.ITabListener
{
 public void OnTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
{
    // Optionally refresh/update the displayed tab.
    Log.Debug(Tag, "The tab {0} was re-selected.", tab.Text);
}

public void OnTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
{
    // Display the fragment the user should see
    Log.Debug(Tag, "The tab {0} has been selected.", tab.Text);
}

public void OnTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
{
    // Save any state in the displayed fragment.
    Log.Debug(Tag, "The tab {0} as been unselected.", tab.Text);
}
...
FreakyAli
  • 13,349
  • 3
  • 23
  • 63