我注意到,在位置0的标签项总是选择的第一,然后在位置的标签项x被加载。这意味着(因为我使用碎片)2片段被加载。其中一人是没有必要的......的Android动作条选项卡上设置最初选择的选项卡
这里是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Determine which bundle to use; either the saved instance or a bundle
// that has been passed in through an intent.
Bundle bundle = getIntent().getExtras();
if (bundle == null) {
bundle = savedInstanceState;
}
// Initialize members with bundle or default values.
int position;
if (bundle != null) {
position = bundle.getInt("selected_tab");
} else {
position = 0;
}
// Set the tabs.
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab = actionBar
.newTab()
.setText("Tab 1")
.setTabListener(
new TabListener<RendersGridFragment>(this, "1",
RendersGridFragment.class));
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText("Tab 2")
.setTabListener(
new TabListener<RendersGridFragment>(this, "2",
RendersGridFragment.class));
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText("Tab 3")
.setTabListener(
new TabListener<RendersGridFragment>(this, "3",
RendersGridFragment.class));
actionBar.addTab(tab);
actionBar.setSelectedNavigationItem(position);
}
看来,在位置0的标签在默认情况下最初选择。但是,正如您所看到的,我正在传递包,以确保当再次运行onCreate()方法时仍然选择最后选定的选项卡。
例如, 如果最后选定的标签位于位置2,则onCreate()运行并且位置标签为0,然后加载位置2的标签。
如何确保在使用actionBar.setSelectedNavigationItem(position)时,ActionBar不会选择位置0处的选项卡。
这很完美。我觉得有一个简单的修复。谢谢。 – mdupls 2012-03-27 20:00:35
它可以工作,但是当创建活动时,我可以看到ViewPager的转换。有什么办法,如何禁用它? – sealskej 2012-12-06 15:28:40
我不确定我是否有问题作者的相同问题。在我的情况下,函数setSelectedNavigationItem(...)时,我的Activity的onCreate调用片段实例化两次或更多。为此改变解决了我的问题。谢谢。 – 2013-01-09 16:39:12