Android ActionBar tabs set initially selected tab

后端 未结 6 1157
-上瘾入骨i
-上瘾入骨i 2020-12-15 05:53

I have noticed that when using

actionBar.setSelectedNavigationItem(x)

in the onCreate() method of my Activity, the tab item at position 0

6条回答
  •  -上瘾入骨i
    2020-12-15 06:13

    If you have 3 tabs (i.e. tab 0, tab 1, tab 2) and want tab 1 to be preselected. Do this:

    for (int i = 0; i < mFragmentPagerAdapter.getCount(); i++) {
        boolean preselected = (i == 1);
        actionBar.addTab(actionBar.newTab().setText(
            mFragmentPagerAdapter.getPageTitle(i)).setTabListener(this), preselected);
    }
    

    You will be using:

    public abstract void addTab (ActionBar.Tab tab, boolean setSelected)
    

    as per this API specification.

提交回复
热议问题