其他分享
首页 > 其他分享> > android-按下按钮时更改标签的指示器(片段和tabHost)

android-按下按钮时更改标签的指示器(片段和tabHost)

作者:互联网

我正在使用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();
}
}

解决方法:

调用tabHost.setCurrentTab(n)

其中tabHost是窗口小部件,n是要选择的选项卡的位置.

标签:android-tabhost,fragment-tab-host,android
来源: https://codeday.me/bug/20191030/1970099.html