为了使用ActionBar实现Tab导航,按如下步骤进行即可。
1.调用ActionBar的setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)方法设置使用Tab导航方式。
2.调用ActionBar的addTab方法添加多个Tab标签,并为每个Tab标签添加事件监听。
Fragment相当于activity片段,通常使用单独的activity组合多个fragment,这样既可以在一个activity创建多个用户界面,又可以让多个activity复用同一个fragment,总之,fragment相当于activity的模块化区域。
fragment有自己的生命周期,可以接受处理属于它自身的事件,并允许activity运行期间动态的添加删除fragment。
fragment允许定义自己的布局,也可以通过生命周期回调方法定义自己的行为,这一点非常像activity。
实例的布局文件:
StandardWidget\res\layout\tabnavigation.xml
1 29 10 11
activity代码:
1 package com.example.standardwidget; 2 3 4 import android.os.Bundle; 5 import android.support.v4.app.Fragment; 6 import android.support.v4.app.FragmentTransaction; 7 import android.support.v7.app.ActionBar; 8 import android.support.v7.app.ActionBar.TabListener; 9 import android.support.v7.app.ActionBarActivity; 10 import android.support.v7.app.ActionBar.Tab; 11 import android.view.Menu; 12 import android.view.MenuInflater; 13 import android.view.MenuItem; 14 import android.widget.Toast; 15 16 public class MyActionBarActivity extends ActionBarActivity implements 17 TabListener { 18 19 private static final String SELECTED_ITEM = "selected_item"; 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 // TODO Auto-generated method stub 24 25 super.onCreate(savedInstanceState); 26 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 27 setContentView(R.layout.tabnavigation); 28 // 设置是否显示app的图标 29 getSupportActionBar().setDisplayShowHomeEnabled(true); 30 // 设置是否将app图标转变成成可点击的按钮 31 getSupportActionBar().setHomeButtonEnabled(true); 32 33 getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 34 getSupportActionBar().addTab( 35 getSupportActionBar().newTab().setText("第一页") 36 .setTabListener(this)); 37 38 getSupportActionBar().addTab( 39 getSupportActionBar().newTab().setText("第二页") 40 .setTabListener(this)); 41 42 getSupportActionBar().addTab( 43 getSupportActionBar().newTab().setIcon(R.drawable.p13) 44 .setText("第三页").setTabListener(this)); 45 46 47 } 48 49 @Override 50 protected void onRestoreInstanceState(Bundle savedInstanceState) { 51 // TODO Auto-generated method stub 52 super.onRestoreInstanceState(savedInstanceState); 53 54 if (savedInstanceState.containsKey(SELECTED_ITEM)) { 55 //选中前面保存的索引对应的fragment页 56 getSupportActionBar().setSelectedNavigationItem( 57 savedInstanceState.getInt(SELECTED_ITEM)); 58 } 59 60 } 61 62 @Override 63 protected void onSaveInstanceState(Bundle outState) { 64 // TODO Auto-generated method stub 65 super.onSaveInstanceState(outState); 66 //将当前选中的fragment页的索引保存到bundle中 67 outState.putInt(SELECTED_ITEM, getSupportActionBar() 68 .getSelectedNavigationIndex()); 69 } 70 71 @Override 72 public boolean onCreateOptionsMenu(Menu menu) { 73 // TODO Auto-generated method stub 74 // 为ActionBar扩展菜单项 75 MenuInflater inflater = getMenuInflater(); 76 inflater.inflate(R.menu.main_activity_actions, menu); 77 return super.onCreateOptionsMenu(menu); 78 79 } 80 81 @Override 82 public boolean onOptionsItemSelected(MenuItem item) { 83 // TODO Auto-generated method stub 84 // 处理动作按钮的点击事件 85 switch (item.getItemId()) { 86 case R.id.action_search: 87 Toast.makeText(this, "openSearch", 2000).show(); 88 return true; 89 case R.id.action_settings: 90 91 Toast.makeText(this, "openSettings", 2000).show(); 92 return true; 93 94 case android.R.id.home: 95 Toast.makeText(this, "App Icon", 2000).show(); 96 return true; 97 default: 98 return super.onOptionsItemSelected(item); 99 }100 }101 102 @Override103 public void onTabReselected(Tab arg0, FragmentTransaction arg1) {104 // TODO Auto-generated method stub105 106 }107 108 @Override109 public void onTabSelected(Tab arg0, FragmentTransaction arg1) {110 // TODO Auto-generated method stub111 Fragment fragment = new DummyFragment();112 //用于向fragment传入参数113 Bundle argsBundle = new Bundle();114 argsBundle.putInt(DummyFragment.ARG_SELECTION_NUMBER, arg0.getPosition()+1);115 //向fragment传入参数116 fragment.setArguments(argsBundle);117 //获取fragmenttransaction对象118 FragmentTransaction fTransaction = getSupportFragmentManager().beginTransaction();119 //使用fragment代替该activity中的container组件120 fTransaction.replace(R.id.container, fragment);121 fTransaction.commit();122 }123 124 @Override125 public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {126 // TODO Auto-generated method stub127 128 }129 130 131 132 }
fragment代码:
1 package com.example.standardwidget; 2 3 import android.os.Bundle; 4 import android.support.v4.app.Fragment; 5 import android.view.Gravity; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.TextView;10 11 public class DummyFragment extends Fragment {12 13 protected static final String ARG_SELECTION_NUMBER ="selection_number";14 //该方法的返回值就是该fragment显示的view组件15 @Override16 public View onCreateView(LayoutInflater inflater, ViewGroup container,17 Bundle savedInstanceState) {18 // TODO Auto-generated method stub19 TextView textView = new TextView(getActivity());20 textView.setGravity(Gravity.START);21 //获取创建该fragment时传入的参数bundle22 Bundle args = getArguments();23 //设置TextView显示的文本24 textView.setText(args.getInt(ARG_SELECTION_NUMBER)+"");25 textView.setTextSize(30);26 27 28 return textView;29 }30 }
运行该实例,效果如图: