MainActivity如下:
package cc.testsimpleactionbar4; import android.os.Bundle; import android.app.ActionBar; import android.app.Activity; import android.app.ActionBar.Tab; import android.app.ActionBar.TabListener; import android.app.FragmentTransaction; /** * Demo描述: * 利用ActionBar结合Fragment实现Tab导航. * 当选中不同的Tab时利用Fragment显示不同的内容 * * 参考资料: * 1 Android疯狂讲义(第二版) * Thank you very much * */ public class MainActivity extends Activity { private ActionBar mActionBar; private final static String SELECTED_TAB_INDEX="selected_tab"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initActionBar(); } private void initActionBar(){ mActionBar=getActionBar(); //设置导航方式为Tab mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); Tab tab1=mActionBar.newTab(); tab1.setText("第一页"); tab1.setTabListener(new TabListenerImpl()); mActionBar.addTab(tab1); Tab tab2=mActionBar.newTab(); tab2.setText("第二页"); tab2.setTabListener(new TabListenerImpl()); mActionBar.addTab(tab2); Tab tab3=mActionBar.newTab(); tab3.setText("第三页"); tab3.setTabListener(new TabListenerImpl()); mActionBar.addTab(tab3); } private class TabListenerImpl implements TabListener{ @Override public void onTabReselected(Tab arg0, FragmentTransaction arg1) { } @Override public void onTabSelected(Tab tab, FragmentTransaction transaction) { System.out.println(" onTabSelected "); TestFragment testFragment=new TestFragment(); Bundle bundle=new Bundle(); bundle.putString(TestFragment.KEY, "这是第"+(tab.getPosition()+1)+"个Tab"); testFragment.setArguments(bundle); FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.relativeLayoutContainer, testFragment); fragmentTransaction.commit(); } @Override public void onTabUnselected(Tab arg0, FragmentTransaction arg1) { } } //以下处理横竖屏的切换 //将当前选中的Tab页的索引保存到Bundle中 @Override protected void onSaveInstanceState(Bundle bundle) { super.onSaveInstanceState(bundle); bundle.putInt(SELECTED_TAB_INDEX, mActionBar.getSelectedNavigationIndex()); } //获取已经保存的被选中Tab页的索引,且设置ActionBar. //setSelectedNavigationItem()方法会调用onTabSelected() @Override protected void onRestoreInstanceState(Bundle bundle) { super.onRestoreInstanceState(bundle); mActionBar.setSelectedNavigationItem(bundle.getInt(SELECTED_TAB_INDEX)); } }
TestFragment如下:
package cc.testsimpleactionbar4; import android.app.Fragment; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class TestFragment extends Fragment { public static final String KEY="key"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { TextView textView=new TextView(getActivity()); textView.setGravity(Gravity.CENTER); Bundle bundle=this.getArguments(); String content=bundle.getString(KEY); textView.setText(content); textView.setTextSize(30); return textView; } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relativeLayoutContainer" android:layout_width="match_parent" android:layout_height="match_parent" > </RelativeLayout>