Java – switch the action bar tab programmatically – how to attach parameters?

I am switching my Android (Sherlock) action bar tab, as described in this question: programmatically switch tabs in Android using actionbar Sherlock

Thank you in anticipation!

@More information requested by semperfly:

My mainactivity has an actionbar and implements it TabListener

actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    tabCalculate = actionBar.newTab();
    tabCalculate.setText("Calculate");
    tabCalculate.setTabListener(this);
    actionBar.addTab(tabCalculate);

    tabArchive = actionBar.newTab();
    tabArchive.setText("Archive");
    tabArchive.setTabListener(this);
    actionBar.addTab(tabArchive);

This is actionbar Implementation of tablistener:

public void onTabSelected(Tab tab,android.support.v4.app.FragmentTransaction ft) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    SherlockFragment fragment = null;
    if( tabCalculate.equals(tab) ) {
        if( fragmentInput == null ) {
            fragmentInput = new Fragmentinput();                
        }            
        fragment = fragmentInput;
    } else if ( tabArchive.equals(tab)) {
        if( fragmentArchive == null ) {
            fragmentArchive = new FragmentArchive();
        }
        fragment = new FragmentArchive();
    }
    fragmentTransaction.replace(R.id.fragmentsContainer,fragment);

    fragmentTransaction.commit();
}    

public void onTabUnselected(Tab tab,android.support.v4.app.FragmentTransaction ft) {

}

public void onTabReselected(Tab tab,android.support.v4.app.FragmentTransaction ft) {

}

My first method is to load inputfragment from archivefragment in this way:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            SherlockFragment fragment = new Fragmentinput();
            Bundle arguments = new Bundle();
            arguments.putSerializable(FragmentInput.ARG_INPUTDATA_TO_LOAD,inputData);
            fragment.setArguments(arguments);
            fragmentTransaction.replace(R.id.fragmentsContainer,fragment);

            fragmentTransaction.commit();

This is perfectly normal, but the active tab indicator in the actionbar is not switched accordingly. The blue line under the archive tab is active, although the input fragment is active

Then I try to switch labels / clips using this method:

getSherlockActivity().getSupportActionBar().setSelectedNavigationItem(0);

This works, but I can't pass parameters to the tab to activate at this time

Solution

It's late, but you can use:

getActivity().getActionBar().setSelectedNavigationItem(1); //where 1 equals the 2nd tab
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>