【问题标题】:Fragment inside a tab won't show on initial load选项卡内的片段不会在初始加载时显示
【发布时间】:2017-01-18 03:52:59
【问题描述】:

我有一个 DialogFragment 显示 2 个选项卡,每个选项卡显示一个片段,动态创建并通过 ChildFragmentTransaction 添加。

问题是,在第一次加载我的DialogFragment 时,除了片段之外的所有内容都会显示,但在选项卡之间切换后,片段会正确显示。在横向和纵向之间切换时也会发生同样的情况,第一次加载时,只显示选项卡,但切换时会出现片段。

这是设置标签的代码。

public override View OnCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
    {
        View v = inflater.Inflate(Resource.Layout.Game_Over_Dialog1, parent, false);

        PlayerTabs = v.FindViewById<TabHost>(Resource.Id.PlayerTabs);
        GameOverOk = v.FindViewById<Button>(Resource.Id.CloseGameOver);
        PlayerTabs.Setup();

        TabHost.TabSpec spec = PlayerTabs.NewTabSpec(player1.Name);
        spec.SetContent(Resource.Id.PlayerDataContainer);
        spec.SetIndicator(player1.Name);
        PlayerTabs.AddTab(spec);

        spec = PlayerTabs.NewTabSpec(player2.Name);
        spec.SetContent(Resource.Id.PlayerDataContainer);
        spec.SetIndicator(player2.Name);
        PlayerTabs.AddTab(spec);

        Dialog.SetTitle("Game Over");
        Dialog.SetCancelable(true);

        GameOverOk.Click += delegate { Dialog.Dismiss(); };
        PlayerTabs.TabChanged += PlayerTabs_TabChanged;
        ChildFragmentManager.BeginTransaction().Add(Resource.Id.PlayerDataContainer, player1Frag).Commit();
        return v;
    }

我试图将 ChildFragmentTransaction 放置在各个位置,在设置标签之前和之后,在 onStart 中,我还尝试以编程方式更改选定的标签,但没有任何效果。

关于为什么我在第一次显示对话框时看不到片段有什么想法吗?任何解决方法,还是我错过了一些琐碎的事情?

谢谢!

【问题讨论】:

    标签: android android-fragments tabs xamarin.android


    【解决方案1】:

    添加PlayersTab.setCurrentTab(0)(参数是索引号,因此在这种情况下为 0 或 1)应该可以解决问题。我会在第二个之后添加它

        PlayerTabs.AddTab(spec);
    

    【讨论】:

    • spec 是一个TabSpec,它没有setCurrentTab。我会尝试使用TabWidget,以防它只是拼写错误。
    • 没有改变
    • 抱歉,应该拨打PlayersTab
    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 2010-11-17
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多