setActionbar();//构造这样一个方法
if(savedInstanceState!=null){
int tab=savedInstanceState.getInt("key");
bar.setSelectedNavigationItem(tab);
}//提取保存的tab选择位置,防止横竖屏幕切换出现错误
}
ActionBar bar;
public void setActionbar(){
bar=getActionBar();
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowHomeEnabled(false);//去掉actionbar题目
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);//设置actionbar模式
bar.addTab(bar.newTab().setText("TAB01").setTabListener(new FraTabListener(this,
Fragment1.class, "frg1")));
bar.addTab(bar.newTab().setText("TAB02").setTabListener(new FraTabListener(this,
Fragment2.class, "frg2")));
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("key", bar.getSelectedNavigationIndex());//保存tab选择位置信息
}
static class FraTabListener implements TabListener{//自己写tab监听器
private Class<?> mClass;
private Activity mActivity;
private String tag;
private Fragment mfrg;
public FraTabListener(Activity activity,Class<?> mclass,String tag) {
this.mActivity=activity;
this.mClass=mclass;
this.tag=tag;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(mfrg==null){
mfrg=Fragment.instantiate(mActivity, mClass.getName());
ft.add(R.id.container, mfrg, tag);
}else{
ft.attach(mfrg);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if(mfrg!=null){
ft.detach(mfrg);//清除不被选择的fragment
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}}
if(savedInstanceState!=null){
int tab=savedInstanceState.getInt("key");
bar.setSelectedNavigationItem(tab);
}//提取保存的tab选择位置,防止横竖屏幕切换出现错误
}
ActionBar bar;
public void setActionbar(){
bar=getActionBar();
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowHomeEnabled(false);//去掉actionbar题目
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);//设置actionbar模式
bar.addTab(bar.newTab().setText("TAB01").setTabListener(new FraTabListener(this,
Fragment1.class, "frg1")));
bar.addTab(bar.newTab().setText("TAB02").setTabListener(new FraTabListener(this,
Fragment2.class, "frg2")));
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("key", bar.getSelectedNavigationIndex());//保存tab选择位置信息
}
static class FraTabListener implements TabListener{//自己写tab监听器
private Class<?> mClass;
private Activity mActivity;
private String tag;
private Fragment mfrg;
public FraTabListener(Activity activity,Class<?> mclass,String tag) {
this.mActivity=activity;
this.mClass=mclass;
this.tag=tag;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(mfrg==null){
mfrg=Fragment.instantiate(mActivity, mClass.getName());
ft.add(R.id.container, mfrg, tag);
}else{
ft.attach(mfrg);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if(mfrg!=null){
ft.detach(mfrg);//清除不被选择的fragment
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}}