public class MainActivity extends FragmentActivity {
@SuppressLint("NewApi")
TabListener tabListener = new TabListener() {
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
System.out.println(tab.getPosition() + "取消选择");
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
System.out.println(tab.getPosition() + "选择");
switch (tab.getPosition()) {
case 0:
android.support.v4.app.FragmentTransaction beginTransaction_1 = supportFragmentManager
.beginTransaction();
beginTransaction_1.replace(R.id.fl, new Fragment1());
beginTransaction_1.commit();
break;
case 1:
android.support.v4.app.FragmentTransaction beginTransaction_2 = supportFragmentManager
.beginTransaction();
beginTransaction_2.replace(R.id.fl, new Fragment2());
beginTransaction_2.commit();
break;
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
System.out.println(tab.getPosition() + "重新选择");
}
};
private FragmentManager supportFragmentManager;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 得到fragment管理者
supportFragmentManager = getSupportFragmentManager();
// 得到ActionBar对象
ActionBar actionBar = getActionBar();
// 设置标题
actionBar.setTitle("1409A");
// 设置actionBar图标的显示
actionBar.setDisplayShowHomeEnabled(false);
// 设置actionBar返回图标是否显示(小三角)
actionBar.setDisplayHomeAsUpEnabled(true);
// 设置actionBar横向排列
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// 添加ActionBar标签页
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.boy_2)
.setText("第一组").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.ic_launcher)
.setText("第二组").setTabListener(tabListener));
}
}