Android入门之ActionBar实现Tab导航

效果图:

 

[html][/html] view plaincopyprint?

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <manifest xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     package=”jk.actionbartab”
  4.     android:versionCode=”1″
  5.     android:versionName=”1.0″ >
  6.     <uses-sdk
  7.         android:minSdkVersion=”11″
  8.         android:targetSdkVersion=”17″ />
  9.     <application
  10.         android:allowBackup=”true”
  11.         android:icon=”@drawable/ic_launcher”
  12.         android:label=”@string/app_name”
  13.         android:theme=”@style/AppTheme” >
  14.         <activity
  15.             android:name=”jk.actionbartab.MainActivity”
  16.             android:label=”@string/app_name” >
  17.             <intent-filter>
  18.                 <action android:name=”android.intent.action.MAIN” />
  19.                 <category android:name=”android.intent.category.LAUNCHER” />
  20.             </intent-filter>
  21.         </activity>
  22.     </application>
  23. </manifest>

 

[html][/html] view plaincopyprint?

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <resources>
  3.     <string name=”action_settings”>Settings</string>
  4.     <string name=”app_name”>Tab导航</string>
  5.     <string name=”title_activity_main”>ActionBar的Tab导航</string>
  6.     <string name=”personal_page”>个人</string>
  7.     <string name=”query_page”>查询</string>
  8.     <string name=”pay_page”>支付</string>
  9. </resources>

MainActivity.java

 

 

[java][/java] view plaincopyprint?

  1. package jk.actionbartab;
  2. import jk.actionbartab.R;
  3. import android.app.ActionBar;
  4. import android.app.Activity;
  5. import android.app.Fragment;
  6. import android.app.FragmentTransaction;
  7. import android.os.Bundle;
  8. public class MainActivity extends Activity implements ActionBar.TabListener {
  9.     private static final String SELECTED_ITEM = “selected_item”;
  10.     public void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_main);
  13.         final ActionBar actionBar = getActionBar();
  14.         // 设置ActionBar的导航方式:Tab导航
  15.         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  16.         // 依次添加3个Tab页,并为3个Tab标签添加事件监听器
  17.         actionBar.addTab(actionBar.newTab().setText(R.string.personal_page)
  18.                 .setTabListener(this));
  19.         actionBar.addTab(actionBar.newTab().setText(R.string.query_page)
  20.                 .setTabListener(this));
  21.         actionBar.addTab(actionBar.newTab().setText(R.string.pay_page)
  22.                 .setTabListener(this));
  23.     }
  24.     public void onRestoreInstanceState(Bundle savedInstanceState) {
  25.         if (savedInstanceState.containsKey(SELECTED_ITEM)) {
  26.             // 选中前面保存的索引对应的Fragment页
  27.             getActionBar().setSelectedNavigationItem(
  28.                     savedInstanceState.getInt(SELECTED_ITEM));
  29.         }
  30.     }
  31.     public void onSaveInstanceState(Bundle outState) {
  32.         // 将当前选中的Fragment页的索引保存到Bundle中
  33.         outState.putInt(SELECTED_ITEM, getActionBar()
  34.                 .getSelectedNavigationIndex());
  35.     }
  36.     public void onTabUnselected(ActionBar.Tab tab,
  37.             FragmentTransaction fragmentTransaction) {
  38.     }
  39.     // 当指定Tab被选中时激发该方法
  40.     public void onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
  41.         // 创建一个新的Fragment对象
  42.         Fragment fragment = new DummyFragment();
  43.         // 创建一个Bundle对象,用于向Fragment传入参数
  44.         Bundle args = new Bundle();
  45.         args.putInt(DummyFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1);
  46.         // 向fragment传入参数
  47.         fragment.setArguments(args);
  48.         // 获取FragmentTransaction对象
  49.         FragmentTransaction ft = getFragmentManager().beginTransaction();
  50.         // 使用fragment代替该Activity中的container组件
  51.         ft.replace(R.id.container, fragment);
  52.         // 提交事务
  53.         ft.commit();
  54.     }
  55.     public void onTabReselected(ActionBar.Tab tab,
  56.             FragmentTransaction fragmentTransaction) {
  57.     }
  58. }

DummyFragment.java

 

 

[java][/java] view plaincopyprint?

  1. package jk.actionbartab;
  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.Gravity;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.TextView;
  9. public class DummyFragment extends Fragment {
  10.     public static final String ARG_SECTION_NUMBER = “section_number”;
  11.     // 该方法的返回值就是该Fragment显示的View组件
  12.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  13.             Bundle savedInstanceState) {
  14.         TextView textView = new TextView(getActivity());
  15.         textView.setGravity(Gravity.CENTER_HORIZONTAL);
  16.         // 获取创建该Fragment时传入的参数Bundle
  17.         Bundle args = getArguments();
  18.         // 设置TextView显示的文本
  19.         textView.setText(args.getInt(ARG_SECTION_NUMBER) + “”);
  20.         textView.setTextSize(30);
  21.         // 返回该TextView
  22.         return textView;
  23.     }
  24. }

标签