Android ActionBar tabs set initially selected tab

后端 未结 6 1156
-上瘾入骨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条回答
  • 2020-12-15 05:58

    Percy Vega's reply seems to be the best working solution.

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
    
            boolean preselected = (i == ErrorDetails.tab_id);
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this),preselected);
        }
    
    0 讨论(0)
  • 2020-12-15 06:04

    For any others looking to do this you can also set the tab to selected by setting the position and then set true or false to indicate which tab should be selected

    actionBar.addTab(tab1, 0, false);
    actionBar.addTab(tab2, 1, true);
    actionBar.addTab(tab3, 2, false);
    

    Here are the docs on this approach: http://developer.android.com/reference/android/app/ActionBar.html#addTab(android.app.ActionBar.Tab, int, boolean)

    0 讨论(0)
  • 2020-12-15 06:04

    bkurzius' answer helped me to fix a problem I was having with the same issue.

    What I did was:

    private final String TAB_SELECTED = "tab_selected"
    ...
    private int mTabSelected;
    ...
    mTabSelected = savedInstanceState.getInt(TAB_SELECTED);
    ...
    final ActionBar actionbar = getActionBar();
    ...
    actionbar.addTab(tab1, mTabSelected == 0);
    actionbar.addTab(tab2, mTabSelected == 1);
    actionbar.addTab(tab3, mTabSelected == 2);
    ...
    outState.putInt(TAB_SELECTED, getActionBar().getSelectedNavigationIndex());
    

    This way, the setSelected parameter is true only if mTabSelected is equal to the tab's index.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-15 06:15

    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);
    
    0 讨论(0)
  • 2020-12-15 06:17

    you can use below statment in activtiy onStart method:

    protected void onStart() {
        super.onStart();
        actionBar.selectTab(mainTab);
    }
    

    which mainTab variable here is of type Tab. this way you need to define tabs as class-wide variables like this:

    Tab mainTab, tab2,tab3;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //add tabs to action bar
        ....
    }
    
    0 讨论(0)
提交回复
热议问题