Enable / Disable Tab in ActionBar

Can I enable / disable tabs in ActionBar? With TabHost, this is not a problem. I do:

tabHost.getTabWidget().getChildAt(3).setVisibility(true); 

and everything works .. but if I want to do the same with tabs in the ActionBar ?? In class Tab, setEnable(); does not exist setEnable();

 ActionBar bar = getActionBar(); Tab tab = bar.newTab(); tab.setText("Test"); tab.setEnable(false); /*DON'T EXIST!!*/ 

How can i do

+6
source share
6 answers

You can use the removeTab( ActionBar.Tab tab ) method of the ActionBar :

 bar.removeTab( tab ); 

And then use addTab( ActionBar.Tab tab, int position ) to return it, while maintaining the Tab position that you deleted:

 bar.addTab( tab, savedTabPosition ); 
+7
source

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 ) { //Do nothing - unless you want to do something. } @Override public void onTabSelected( Tab tab, FragmentTransaction ft ) { TabItem item = tabHolder.getTab( tab.getPosition() ); if( item.isEnabled() ) { ft.remove( item.getFragment() ); ft.commit(); } } @Override public void onTabUnselected( Tab tab, FragmentTransaction ft ) { //Do nothing - unless you want to do something. } } 

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 :)

+4
source

There is an easy way to remove Tabs-bar from the action bar. Just enter:

 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 

This will delete all tabs.

+3
source

you can override: public boolean onPrepareOptionsMenu(Menu menu) , here set the enable / disable tab, for example:

 menu.findItem(R.id.send).setEnabled(mMessageNeedtoSend); 

and then you can set mMessageNeedtoSend true or false, call invalidateOptionsMenu() to update the ActionBar.

0
source

Late answer, but hope this workaround can help others interested in this issue.

 private Handler mHandler; private int mLastSelectedTabIndex = 0; @Override protected void onCreate(Bundle savedInstanceState) { // ... mHandler = new Handler(); // ... ActionBar bar = getActionBar(); Tab tab = bar.newTab().setText("Enabled Tab") .setTabListener(mTabListener); bar.addTab(tab); tab = bar.newTab().setText("Disabled Tab") .setTabListener(mTabListener); bar.addTab(tab); } private TabListener mTabListener = new TabListener() { // ... @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { int position = tab.getPosition(); if (position == 1) { // Disabled tabs are selected. mHandler.postAtFrontOfQueue(new Runnable() { @Override public void run() { getActionBar().setSelectedNavigationItem( mLastSelectedTabIndex); } }); } else { // Enabled tabs are selected. Do something on your own. mLastSelectedTabIndex = position; } } // ... }; 

If you use the ViewPager with it, as usual, you can simply use your position instead of mLastSelectedTabIndex as follows:

 getActionBar().setSelectedNavigationItem(mViewPager.getCurrentItem()); 
0
source

This is done in two simple steps (Assuming you are customizing the tab view):

1- Disable function: Avoid changing tabs

  @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { int position = tab.getPosition(); if (mModifyMode) { mPagerAdapter.changeFragment(position); } } 

2- Disable click events:

  public void setModifyMode(boolean modifyMode) { mModifyMode = modifyMode; for (int i = 0; i < mActionBar.getTabCount(); i++) { View parent = (View) mActionBar.getTabAt(i).getCustomView().getParent(); parent.setEnabled(mModifyMode); } } 

I tested it right now and it works;)

0
source

Source: https://habr.com/ru/post/901703/


All Articles