Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/Disable Android ActionBar.Tab

I'm developing an application that uses the new Tabs system (ActionBar.addTab()), I will have 3 tabs. During a determined process I would like to lock (disable) two of them (not remove, only do not accept clicks and not change the 'selector'.

I was able to do not change the content (by implementing the TabListener and not changing the fragment, etc) but the selector changes.

Is there anyway to only disable/enable the tabs? without have to remove and add again?

Thanks in advance! BR, Danilo

like image 521
DaniloBertelli Avatar asked Mar 06 '12 14:03

DaniloBertelli


2 Answers

Sorry, there is no "enable" or "disable" with tabs in the action bar. As you note, you can remove and re-add them -- that is the closest you can get AFAIK.

like image 148
CommonsWare Avatar answered Oct 16 '22 00:10

CommonsWare


I got into the exact same issue. Finally was able to solve it. As said already, there is no way to disable/enable a tab. There is only one way out - and that is to go back to the previous tab using

getActionBar().setSelectedNavigationItem(tab);

Note however, that this should NOT be done from within the

onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction)

method, because then it gets stuck in recursive loop. So within the onTabSelected() - send a messsage to the Activity Handler to do the setSelectedNavigation().

One catch here is (you will know when you implement): what "tab" value to pass in the setSelectedNavigation. For this, we have to keep track of whether it is a user pressed tab switch or forced switch to the last tab via the code.

For this:

public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    if (forcedTabSwitch)
        forcedTabSwitch = false;
    else 
        mTabToBeSwitchedTo = tab.getPosition();
}

and in the Handler,

switch (msg.what) {
            case SWITCH_TO_TAB :
                forcedTabSwitch = true;
                            break;
}
like image 20
KurtCobain Avatar answered Oct 16 '22 00:10

KurtCobain