3

我正在使用 Fragments 和 tabHost,一切都很好。我需要做的是:当我按下一个按钮时,它必须将我带到另一个片段,我需要更改选项卡的指示器:

在此处输入图像描述

当我按下按钮时,片段会更改,但选项卡指示器不会。

现在,指示器仅在按下 Tab 时发生变化,但我想要在按下按钮时获得相同的效果。这可能吗?。

代码:

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();
}
}
4

1 回答 1

4

称呼tabHost.setCurrentTab(n)

tabHost小部件在哪里,n是要选择的选项卡的位置。

于 2013-06-27T12:58:30.880 回答