OnTabChanged не вызывается при нажатии на текущую вкладку - PullRequest
2 голосов
/ 15 октября 2019

Я реализовал приложение с вкладками с Xamarin.Android, используя TabHost и MvxTabsFragmentActivity. Я хочу обновить текущую вкладку при повторном нажатии на нее.

Есть ли какой-либо метод, например, OnTabReselected от Android?

Вот как я создаю вкладки, используя 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>

И 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();

        }

Я добавил только соответствующий код. Вкладки создаются с использованием TabHost, а действие наследуется от MvxTabsFragmentActivity.

1 Ответ

1 голос
/ 15 октября 2019

Вы можете унаследовать интерфейс ActionBar.ITabListener в MvxTabsFragmentActivity вашего TabHost, а затем получить нужные вам события.

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);
}
...
...