带有旋转片段的 ActionBar 选项卡
我正在构建一个带有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了。我每次都重新创建该片段,不再这样做解决了它。
将我的 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()
toft.replace()