I have not tested this - it will be up to you, but it should give you a general idea of how you can deal with your problem.
There are three steps:
First step
We need something that can handle the enable / disable action for us. To do this, we create the following class:
public class TabItem { private Tab tab; private Fragment fragment; private boolean enabled; public TabItem( Tab tab, Fragment fragment ) { this.tab = tab; this.fragment = fragment; enabled = true; } public Tab getTab() { return tab; } public Fragment getFragment() { return fragment; } public void toggleEnabled() { enabled = enabled ? false : true; } public boolean isEnabled() { return enabled; } }
Second step
We need something that can contain these TabItems and an easy way to access them. To do this, add the following class:
public class TabHolder { private HashMap<Integer, TabItem> tabs; public TabHolder() { tabs = new HashMap<Integer, TabItem>(); } public void addTab( TabItem tab ) { tabs.put( tab.getTab().getPosition(), tab ); } public TabItem getTab( int position ) { return tabs.get( position ); } }
Third step
We need to handle the Tabs selection ourselves, so we need to create a custom TabListener :
private class MyTabListener implements TabListener { @Override public void onTabReselected( Tab tab, FragmentTransaction ft ) {
Finally
Now we can use the created infrastructure. For this we need TabHolder :
tabHolder = new TabHolder(); //Needs to be declared in the same class as our TabListener
We need to add our Tabs to this:
tabHolder.addTab( new TabItem( tab, fragmentForThisTab ) );
And we have to set our custom TabListener on each Tab :
tab.setTabListener( new MyTabListener() );
On / off
To enable or disable Tab , we simply call:
tabHolder.getTab( position ).toggleEnabled();
Let me know how this happens :)