原文地址:http://blog.****.net/u010212533/article/details/39338337
大家知道,在Android3.0以后,google提供ActionBar这项功能,有了这个以后呢,对于开发者来说可以解决很多的问题。
现在我就把我自己的对ActionBar的三种导航模式理解写下来,方便以后查看:
第一种导航模式:
标准导航模式:这个模式比较简单。
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);//设置导航模式,(标准模式)
第二种导航模式:
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);//设置导航模式,(表格模式:横屏换行显示导航,竖屏同行显示导航) // 设置ActinBar添加Tab选项标签 actionBar.addTab(actionBar.newTab().setText("TAB1").setTabListener(new MyTabListener<FragmentPage1>(this, FragmentPage1.class))); actionBar.addTab(actionBar.newTab().setText("TAB2").setTabListener(new MyTabListener<FragmentPage2>(this, FragmentPage2.class))); actionBar.addTab(actionBar.newTab().setText("TAB3").setTabListener(new MyTabListener<FragmentPage2>(this, FragmentPage2.class))); actionBar.addTab(actionBar.newTab().setText("TAB4").setTabListener(new MyTabListener<FragmentPage2>(this, FragmentPage2.class))); actionBar.addTab(actionBar.newTab().setText("TAB5").setTabListener(new MyTabListener<FragmentPage2>(this, FragmentPage2.class)));
MyTabListenerMyTabListener代码:
public class MyTabListener<T extends Fragment> implements TabListener { private Fragment fragment; private final Activity mActivity; private final Class<T> mClass; public MyTabListener(Activity activity, Class<T> clz) { mActivity = activity; mClass = clz; } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { if (fragment == null) { fragment = Fragment.instantiate(mActivity, mClass.getName()); ft.add(android.R.id.content, fragment, null); } ft.attach(fragment); } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { if (fragment != null) { ft.detach(fragment); } } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } }
第三种导航模式:
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);//设置导航模式,(用一个下拉的list导航) actionBar.setListNavigationCallbacks(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, strs), new OnNavigationListener() { @Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { if (itemPosition == 0) { FragmentPage1 fragmentPage1 = new FragmentPage1(); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.container, fragmentPage1).commit(); } else { FragmentPage2 fragmentPage2 = new FragmentPage2(); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.container, fragmentPage2).commit(); } return false; } });