带有旋转片段的 ActionBar 选项卡

发布于 2025-01-03 01:05:18 字数 1677 浏览 5 评论 0原文

我正在构建一个带有 ActionBar 和下面两个选项卡的应用程序。 如果设备/模拟器未旋转,则一切正常。如果旋转,选项卡状态会自动切换到 tab1(正常,因为 onCreate 被调用),但内容不会更改。如果我选择新方向的选项卡,则会调用所选 Fragment 中的 onCreateView() 方法,但视图不会更新(始终保持不变)。有什么建议吗?

代码。

主要活动:

    ActionBar actionbar = getActionBar();
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab ATab = actionbar.newTab().setText(R.string.player);
    ActionBar.Tab BTab = actionbar.newTab().setText(R.string.stations);

    Fragment AFragment = new AFragment();
    Fragment BFragment = new BFragment();

    PlayerTab.setTabListener(new MyTabsListener(AFragment));
    StationsTab.setTabListener(new MyTabsListener(BFragment));

    actionbar.addTab(ATab);
    actionbar.addTab(BTab);

具有显示简单文本视图的相同选项卡。文本视图简单地说明选择了哪个选项卡。

Fragments:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.a, container, false);
}

上面提到的 Fragment 布局仅包含带有硬编码文本的 TextView。 (仅用于测试目的)


主布局如下所示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >


    <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
 </LinearLayout>

</LinearLayout>

I'am build an app with an ActionBar and two Tabs below.
Everything works fine if the device / emulator isnt rotated. If rotated, tab state switches automaticale to tab1 (normal, because onCreate get called) but the content dont get changed. If I select a tab in the new orientation, the onCreateView() method from the selected Fragment get called but the view dont get updated (stay always the same). Any Tips?

The code.

Main Activity:

    ActionBar actionbar = getActionBar();
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab ATab = actionbar.newTab().setText(R.string.player);
    ActionBar.Tab BTab = actionbar.newTab().setText(R.string.stations);

    Fragment AFragment = new AFragment();
    Fragment BFragment = new BFragment();

    PlayerTab.setTabListener(new MyTabsListener(AFragment));
    StationsTab.setTabListener(new MyTabsListener(BFragment));

    actionbar.addTab(ATab);
    actionbar.addTab(BTab);

With identical tabs that display a simple textview. The textview simple say which tab is selected.

Fragments:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.a, container, false);
}

The Fragment layout, mentioned above, only contains a TextView with hardcoded Text. (Only for testing purposes)


The Main layout looks like this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >


    <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
 </LinearLayout>

</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪异←思 2025-01-10 01:05:18

解决了。我每次都重新创建该片段,不再这样做解决了它。

将我的 TabListener 和 onTabSelected(Tab tab, FragmentTransaction ft)ft.add() 更改为 ft.replace()

Solved. I have recreated the fragment everytime, doesnt do that anymore solved it.

Changed in my TabListener and onTabSelected(Tab tab, FragmentTransaction ft), ft.add() to ft.replace()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文