【问题标题】:OnTabChanged not called when click on current tab单击当前选项卡时未调用 OnTabChanged
【发布时间】:2019-10-15 13:52:52
【问题描述】:

我使用 TabHost 和 MvxTabsFragmentActivity 使用 Xamarin.Android 实现了一个选项卡式应用程序。 我想在第二次点击时刷新当前标签。

有没有类似 OnTabReselected from 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>

主视图:

    [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 创建,Activity 继承自 MvxTabsFragmentActivity。

【问题讨论】:

  • 忽略我之前说的,我今天学到了一些我认为不可能的新东西。试试这个stackoverflow.com/questions/43045672/…
  • 我使用 TabHost 是因为 MvxTabsFragmentActivity 使用的是 TabHost,而不是 TabLayout。

标签: xamarin xamarin.android mvvmcross


【解决方案1】:

你可以继承你TabHost的MvxTabsFragmentActivity中的ActionBar.ITabListener接口,然后就可以得到你需要的事件了。

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

【讨论】:

  • 没有一个方法被命中。
  • 我可以看看你的代码吗?如果没有,请查看docs.microsoft.com/en-us/xamarin/android/user-interface/layouts/…
  • 你检查了上面的链接吗?另外,您似乎没有使用我在回答中建议的内容
  • 我删除了你建议我的东西,因为我只想添加我正在使用的代码。
  • 我建议您尝试复制链接中的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-01
相关资源
最近更新 更多