Android的动作条选项卡中设置最初选择的选项卡 [英] Android ActionBar tabs set initially selected tab

查看:131
本文介绍了Android的动作条选项卡中设置最初选择的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注意到,当使用

actionBar.setSelectedNavigationItem(x)

在我的活动的的onCreate()方法,在位置0的标签项总是选择的第一,然后在位置x的标签项被装载。这意味着,(因为我使用的片段)2片段被加载。其中之一是不必要的......

in the onCreate() method of my Activity, the tab item at position 0 is always selected first and then the tab item at position x is loaded. This means that (since I'm using Fragments) 2 Fragments are loaded. One of them being unnecessary...

下面是我的code:

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()方法被再次运行时。

It seems that the tab at position 0 is selected initially by default. But, as you can see, I'm passing bundles to make sure the last selected tab is still selected when the activity onCreate() method is run again.

例如, 如果最后选择的标签是在2位,所述的onCreate()运行,并在位置的标签被0被加载,则在位置2的标签被加载

For example, if the last selected tab is at position 2, the onCreate() runs and the tab at position is 0 is loaded, then the tab at position 2 is loaded.

我怎样才能确保在第一位置0使用actionBar.setSelectedNavigationItem(位置)时,动作条不选择标签。

How can I make sure the ActionBar doesn't select tab at position 0 first when using actionBar.setSelectedNavigationItem(position).

推荐答案

使用其他 addTab 调用覆盖此行为。你需要添加你想成为第一个选定的标签(在你的情况下,在位置2标签)。 <一href="http://developer.android.com/reference/android/app/ActionBar.html#addTab(android.app.ActionBar.Tab)">Relevant的Javadoc

Use the other addTab calls to override this behaviour. You'll need to add the tab you want to be selected first (in your case, the tab at position 2). Relevant Javadoc

actionBar.addTab(tab2);
actionBar.addTab(tab0, 0, false);
actionBar.addTab(tab1, 1, false);

这篇关于Android的动作条选项卡中设置最初选择的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆