Search code examples
androidactionbarsherlock

Disabling a fragment tab activity


I have a class that is SherlockFragmentActivity and inside it two SherlockFragment. When I enter the class it automatically puts me inside the first tab (Tab1 "FragmentTab1"), and I can go to the other tab (Tab2 "FrgmentTab2") by clicking on the tab up. How can I disable a tab?

I want to disable Tab2(bbb) from Tab1(aaa).

my main class code:

public class MainClass extends SherlockFragmentActivity {

    ActionBar.Tab Tab1,Tab2;
    Fragment fragmentTab1 = new FragmentTab1();
    Fragment fragmentTab2 = new FragmentTab2();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mm);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ActionBar actionBar = getSupportActionBar();

        // Create Actionbar Tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set Tab Icon and Titles
        Tab1 = actionBar.newTab().setText("aaa");
        Tab2 = actionBar.newTab().setText("bbb");

        // Set Tab Listeners
        Tab1.setTabListener(new TabListener(fragmentTab1));
        Tab2.setTabListener(new TabListener(fragmentTab2));


        // Add tabs to actionbar
        actionBar.addTab(Tab1);
        actionBar.addTab(Tab2);
    }

My tabs codes (both classes have same clean-empty code right now)

public class FragmentTab1 extends SherlockFragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragmenttab2, container, false);

        return rootView;
    }
}

For further explanation please ask.


Solution

  • For removing tab from tabbar, you can use

    getActivity().getSupportActionBar().removeTabAt(index)
    

    If you want to disable touch events on that particular tab, you need to create custom view for this Tab a disable focus on that view.

    EDIT

    Sory, forgot to mention. If you are using API version>=11, you can use

    getActivity().getActionBar()
    

    If you are using support lib for compatability, e.g. ActionBarSherlock or appCompat you need to cast it first, in your case

    ((SherlockFragmentActivity) getActivity()).getSupportActionBar()