从Fragment更改Tab / Fragment

时间:2013-06-12 20:59:08

标签: android android-fragments android-tabhost android-fragmentactivity android-tabs

我有一个从FragmentActivity扩展的Main.class并实现了TabHost.OnTabChangeListener。 我有4个标签,这个叫4级,从Fragment扩展。

例如,如果我在tab1的片段中并按一个按钮来访问tab2,我会执行以下操作:

public class Tab01 extends Fragment {
...
Intent intent = new Intent(getActivity(), Main.class);
intent.putExtra("Screen","Tab02");
Tab01.this.startActivity(intent);
getActivity().finish();
...
}

在Main.class中:

public class Main extends FragmentActivity implements TabHost.OnTabChangeListener {
.....
private void initialiseTabHost(Bundle args) {
..
String nameTab = "";
String nameTabExtra = myIntent.getStringExtra("Screen");
if(nameTabExtra == null){
            mTabHost.setCurrentTab(0);
        }else{
            if (nameTabExtra.equals("Tab01")){
                nameTab = "Tab01";
                mTabHost.setCurrentTab(0);
            }else if (nameTabExtra.equals("Tab02")){
                nameTab = "Tab02";
                mTabHost.setCurrentTab(1);              
            }else if (nameTabExtra.equals("Tab03")){
                nameTab = "Tab03";
                mTabHost.setCurrentTab(2);
            }else if (nameTabExtra.equals("Tab04")){
                nameTab = "Tab04";
                mTabHost.setCurrentTab(3);
            } 
        }
this.onTabChanged(nameTab);

这有效,但这是唯一的方法吗?感谢

1 个答案:

答案 0 :(得分:-1)