質問

I'm using fragments and tabHost and everything works pretty well. What I need to do is: When I press a button, it has to take me to another fragment and I need to change the indicator of tab:

enter image description here

When I press the button the fragment is changed but the tab indicator doesn't.

Right now, the indicator only changes when the Tab is pressed, but I want the same effect when I press a button. Is this possible?.

CODE:

public class AplicacionActivity extends FragmentActivity {
    final public static String tagFragmentClientes = "fragmentClientes";
final public static String tagFragmentSettings = "fragmentSettings";
final public static String tagFragmentLogout = "fragmentLogout";

    /*Principal fragments of the tabs*/
ClientesActivity fragmentClientes;
SettingsActivity fragmentSettings;
LogoutActivity fragmentLogout;

    @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aplicacion);

    fragmentClientes = new ClientesActivity();
    fragmentSettings = new SettingsActivity();
    fragmentLogout = new LogoutActivity();

    mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setOnTabChangedListener(listener);

    mTabHost.setup();

    initializeTab();
}

public void initializeTab() {

    Resources res = getResources(); 

    TabHost.TabSpec spec =  mTabHost.newTabSpec(tagFragmentClientes);

    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }
    });
    spec.setIndicator(" Clientes ", res.getDrawable(R.drawable.clientes));
    mTabHost.addTab(spec);


    spec = mTabHost.newTabSpec(tagFragmentSettings);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }
    });
    spec.setIndicator(" Settings ",res.getDrawable(R.drawable.settings));
    mTabHost.addTab(spec);

    spec =   mTabHost.newTabSpec(tagFragmentLogout);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }
    });
    spec.setIndicator(" Logout ",res.getDrawable(R.drawable.logouticon));
    mTabHost.addTab(spec);
}

TabHost.OnTabChangeListener listener    =   new TabHost.OnTabChangeListener() {
    public void onTabChanged(String tabId) {

        /*Set current tab..*/
        if(tabId.equals(tagFragmentClientes)){

            pushFragments(tagFragmentClientes, fragmentClientes, false);

        }else if(tabId.equals(tagFragmentSettings)){

            pushFragments(tagFragmentSettings, fragmentSettings, false);

        }else if(tabId.equals(tagFragmentLogout)){

            mostrarAvisoCierreApp();
            //nos saque el aviso de nuevo si lo volvemos a presionar.
            mTabHost.getCurrentTabView().setOnTouchListener(new OnTouchListener(){
                @Override
                public boolean onTouch(View v, MotionEvent event){

                    if (event.getAction() == MotionEvent.ACTION_DOWN){
                        if (mTabHost.getCurrentTabTag().equals(tagFragmentLogout)){
                            mostrarAvisoCierreApp();
                        }
                    }
                    return false;
                }
            });

        }
    }
};

public void pushFragments(String tag, Fragment fragment, boolean backStack){

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();

    ft.replace(android.R.id.tabcontent, fragment, tag);

    if(backStack){
        ft.addToBackStack(null);
    }

    ft.commit();
}
}
役に立ちましたか?

解決

Call tabHost.setCurrentTab(n)

Where tabHost is the widget and n is the position of the tab to select.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top