Enable / Disable Android ActionBar.Tab

I am developing an application using the new tab system (ActionBar.addTab ()), I will have 3 tabs. During a certain process, I would like to block (disable) two of them (do not delete, just do not accept clicks and do not change the "selector".

I managed to not change the content (by implementing the TabListener, not changing the fragment, etc.), but the selector changes.

Do I need to disable / enable tabs at all? without having to remove and add again?

Thanks in advance! BR, Danilo

+8
android android-actionbar android-fragments
source share
3 answers

Sorry, there is no “enable” or “disable” tabbed action bar. As you noticed, you can delete and re-add them - this is the closest you can get AFAIK.

+2
source share

I got into the same problem. Finally managed to solve the problem. As already mentioned, there is no way to disable / enable the tab. There is only one way out - and you need to return to the previous tab using

getActionBar().setSelectedNavigationItem(tab); 

Please note that this must be NOT from

 onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) 

because then it gets stuck in a recursive loop. Thus, inside onTabSelected () - send a messsage to the activity handler to execute setSelectedNavigation ().

One catch here (you'll find out when you implement it): what value of the "tab" you need to pass to setSelectedNavigation. To do this, we need to monitor whether this is by pressing the user’s tab button or by force switching to the last tab using 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; } 
+1
source share

I also had the same problem. My application has three tabs, and on each tab there are buttons to go to the next and previous tab. I cannot find a way to enable or disable tabs.

I used the DataSetter class to store the previously selected tab stop and boolean to determine the tab selection. If the boolean flag is true, the user presses the next / previous button. Otherwise, the user tries to select a tab by clicking which we do not allow. I used a handler to select tabs. Inside the fragment, when the user clicks the next button, I broadcast this request. The broadcast request has an integer parameter, which is the position of the next or previous tab that we want to set. Code inside snippet:

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.first_tab_fragment, container, false); next = (Button)rootView.findViewById(R.id.nextbutton); next.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent("TAB_CLICKED"); Bundle bundle = new Bundle(); bundle.putInt(MainActivity.POSITION,1); intent.putExtras(bundle); getActivity().sendBroadcast(intent); } }); return rootView; } 

I used CustomViewPager instead of ViewPager to prevent tab scrolling. CustomViewPager Class:

  public class CustomViewPager extends ViewPager { private boolean enabled; public CustomViewPager(Context context) { super(context); } public CustomViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub if (this.enabled) { return super.onTouchEvent(event); } return false; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { // TODO Auto-generated method stub if (this.enabled) { return super.onInterceptTouchEvent(event); } return false; } public void setPagingEnabled(boolean enabled) { this.enabled = enabled; } } 

Inside the oncreate MainActivity.java method, add two lines:

  viewPager = (CustomViewPager) findViewById(R.id.pager); viewPager.setPagingEnabled(false); 

Below is the code from MainActivity.java:

  @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { Log.d("MainActivity","onTabSelected"); if(setter.getFlag() == true) { viewPager.setCurrentItem(setter.getposition()); } } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub Log.d("MainActivity","onTabUnselected"); //handler.sendEmptyMessage(0); Message message = new Message(); message.arg1 = setter.getposition(); handler.sendMessage(message); } Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { int arg1 = msg.arg1; Log.d("arg1",arg1+""); //viewPager.setCurrentItem(arg1); mActionBar.setSelectedNavigationItem(arg1); Log.d("handler","Inside handler"); } }; BroadcastReceiver receiver = new BroadcastReceiver(){ public void onReceive(Context context, android.content.Intent intent) { String action = intent.getAction(); Bundle bundle = intent.getExtras(); int position = bundle.getInt(MainActivity.POSITION); Log.d("Action",action); Log.d("position",position+""); setter.setposition(position); setter.setflag(true); Message message = new Message(); message.arg1 = position; handler.sendMessage(message); }; }; } 

Finally DataSetter.java:

  public class DataSetter { int position; boolean flag; void setposition(int value) { position = value; } void setflag(boolean value) { flag = value; } int getposition() { return position; } boolean getFlag() { return flag; } } 

More details can be found at: http://suhas1989.wordpress.com/2014/10/13/enable-or-disable-actionbar-tab-in-android/

0
source share

All Articles