类库appcompat-support-v7库项目 (注意:请使用Android 19以下版本最佳,因为设计概念的变化,Android L版本支持不佳)
android-support-v4.jar
效果预览
主布局文件slinding_layout.xml
Activity逻辑文件
public class SlidingPaneLayoutActivity extends ActionBarActivity { private SlidingPaneLayout mSlidingPaneLayout; private ListView mSliderList; private ActionBarDrawerToggle mActionBarToggle; private CharSequence mDrawerTitle; private CharSequence mTitle; private String[] mPlanetTitles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.slinding_layout); //获取标题 mTitle = mDrawerTitle = getTitle(); //获取菜单列表 mPlanetTitles = getResources().getStringArray(R.array.planets_array); //获取SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.sliding_layout); mSliderList = (ListView) findViewById(R.id.sliding_list); //设置背景 mSlidingPaneLayout.setShadowDrawable(new ColorDrawable(0x99000000)); mSliderList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, mPlanetTitles)); mSliderList.setOnItemClickListener(new DrawerItemClickListener()); getSupportActionBar().setHomeButtonEnabled(true); mActionBarToggle = new ActionBarDrawerToggle( this, /* host Activity */ new DrawerLayout(this), /* 注意,这里必须有个 DrawerLayout*/ R.drawable.ic_toggler_home, R.string.slider_open, /* "open drawer" description for accessibility */ R.string.slider_close /* "close drawer" description for accessibility */ ) { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); getSupportActionBar().setTitle(mTitle); postInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } @Override public boolean onOptionsItemSelected(MenuItem item) { //---------------这里需要特别注意---不能再这里调用super.onOptionsItemSelected(item); if(item.getItemId()==android.R.id.home) { if(!mSlidingPaneLayout.isOpen()) { mSlidingPaneLayout.openPane(); }else{ mSlidingPaneLayout.closePane(); } } return true; } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); getSupportActionBar().setTitle(mDrawerTitle); postInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; mSlidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.SimplePanelSlideListener(){ @Override public void onPanelClosed(View panel) { super.onPanelClosed(panel); //模拟调用 mActionBarToggle.onDrawerClosed(panel); } @Override public void onPanelOpened(View panel) { // TODO Auto-generated method stub super.onPanelOpened(panel); //模拟调用 mActionBarToggle.onDrawerOpened(panel); } @Override public void onPanelSlide(View panel, float slideOffset) { super.onPanelSlide(panel, slideOffset); mActionBarToggle.onDrawerSlide(panel, slideOffset); } }); if (savedInstanceState == null) { selectItem(0); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onPrepareOptionsMenu(Menu menu) { boolean drawerOpen = mSlidingPaneLayout.isOpen(); menu.findItem(R.id.action_search).setVisible(!drawerOpen); return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (mActionBarToggle.onOptionsItemSelected(item)) { return true; } switch(item.getItemId()) { case R.id.action_search: Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle()); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Toast.makeText(this, R.string.app_name, Toast.LENGTH_LONG).show(); } return true; default: return super.onOptionsItemSelected(item); } } private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { selectItem(position); } } private void selectItem(int position) { Fragment fragment = new PlanetFragment(); Bundle args = new Bundle(); args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); fragment.setArguments(args); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit(); mSliderList.setItemChecked(position, true); setTitle(mPlanetTitles[position]); mSlidingPaneLayout.closePane(); } @Override public void setTitle(CharSequence title) { mTitle = title; getSupportActionBar().setTitle(mTitle); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mActionBarToggle.syncState(); getSupportActionBar().setDisplayHomeAsUpEnabled(mActionBarToggle.isDrawerIndicatorEnabled()); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mActionBarToggle.onConfigurationChanged(newConfig); getSupportActionBar().setDisplayHomeAsUpEnabled(mActionBarToggle.isDrawerIndicatorEnabled()); } @Override public void onBackPressed() { if(mSlidingPaneLayout.isSlideable() && mSlidingPaneLayout.isOpen()){ mSlidingPaneLayout.closePane(); } else{ super.onBackPressed(); } } private void postInvalidateOptionsMenu() { try { Method method = this.getClass().getMethod("invalidateOptionsMenu", new Class []{}); if(method!=null) { if (Modifier.isPublic(method.getModifiers()) && Modifier.isPublic(method.getDeclaringClass().getModifiers())) { method.invoke(this, new Object[]{}); } else if(!method.isAccessible()) { method.setAccessible(true); method.invoke(this, new Object[]{}); } } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } /** * Fragment that appears in the "content_frame", shows a planet */ public static class PlanetFragment extends Fragment { public static final String ARG_PLANET_NUMBER = "planet_number"; public PlanetFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); int i = getArguments().getInt(ARG_PLANET_NUMBER); String planet = getResources().getStringArray(R.array.planets_array)[i]; int imageId = getResources().getIdentifier("audio_identify_start_default", "drawable", getActivity().getPackageName()); ((ImageView) rootView.findViewById(R.id.section_image)).setImageResource(imageId); getActivity().setTitle(planet); return rootView; } }
fragment布局文件fragment_main.xml