Android入门之ActionBar实现Tab导航
效果图:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jk.actionbartab" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="jk.actionbartab.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="action_settings">Settings</string> <string name="app_name">Tab导航</string> <string name="title_activity_main">ActionBar的Tab导航</string> <string name="personal_page">个人</string> <string name="query_page">查询</string> <string name="pay_page">支付</string> </resources>
MainActivity.java
package jk.actionbartab; import jk.actionbartab.R; import android.app.ActionBar; import android.app.Activity; import android.app.Fragment; import android.app.FragmentTransaction; import android.os.Bundle; public class MainActivity extends Activity implements ActionBar.TabListener { private static final String SELECTED_ITEM = "selected_item"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ActionBar actionBar = getActionBar(); // 设置ActionBar的导航方式:Tab导航 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // 依次添加3个Tab页,并为3个Tab标签添加事件监听器 actionBar.addTab(actionBar.newTab().setText(R.string.personal_page) .setTabListener(this)); actionBar.addTab(actionBar.newTab().setText(R.string.query_page) .setTabListener(this)); actionBar.addTab(actionBar.newTab().setText(R.string.pay_page) .setTabListener(this)); } public void onRestoreInstanceState(Bundle savedInstanceState) { if (savedInstanceState.containsKey(SELECTED_ITEM)) { // 选中前面保存的索引对应的Fragment页 getActionBar().setSelectedNavigationItem( savedInstanceState.getInt(SELECTED_ITEM)); } } public void onSaveInstanceState(Bundle outState) { // 将当前选中的Fragment页的索引保存到Bundle中 outState.putInt(SELECTED_ITEM, getActionBar() .getSelectedNavigationIndex()); } public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } // 当指定Tab被选中时激发该方法 public void onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) { // 创建一个新的Fragment对象 Fragment fragment = new DummyFragment(); // 创建一个Bundle对象,用于向Fragment传入参数 Bundle args = new Bundle(); args.putInt(DummyFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1); // 向fragment传入参数 fragment.setArguments(args); // 获取FragmentTransaction对象 FragmentTransaction ft = getFragmentManager().beginTransaction(); // 使用fragment代替该Activity中的container组件 ft.replace(R.id.container, fragment); // 提交事务 ft.commit(); } public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } }
DummyFragment.java
package jk.actionbartab; 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 DummyFragment extends Fragment { public static final String ARG_SECTION_NUMBER = "section_number"; // 该方法的返回值就是该Fragment显示的View组件 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setGravity(Gravity.CENTER_HORIZONTAL); // 获取创建该Fragment时传入的参数Bundle Bundle args = getArguments(); // 设置TextView显示的文本 textView.setText(args.getInt(ARG_SECTION_NUMBER) + ""); textView.setTextSize(30); // 返回该TextView return textView; } }