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) {
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) {
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/