[英]ActionBar Tabs with fragments on rotate
I'am build an app with an ActionBar
and two Tabs below. 我正在构建一个带有
ActionBar
和下面两个标签的应用程序。 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. 如果旋转,选项卡状态会自动切换到tab1(正常,因为
onCreate
被调用)但内容不会更改。 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). 如果我在新方向中选择一个选项卡,
onCreateView()
调用所选Fragment
的onCreateView()
方法,但视图不会更新(始终保持不变)。 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.
textview简单说明选择了哪个选项卡。
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. 上面提到的Fragment布局只包含带有硬编码Text的TextView。 (Only for testing purposes)
(仅用于测试目的)
The Main layout looks like this. Main布局如下所示。
<?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>
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()
我的TabListener和
onTabSelected(Tab tab, FragmentTransaction ft)
, ft.add()
更改为ft.replace()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.