Android 抽屉导航 (DrawerLayout)


先看看效果


使用最新的android-support-v4.jar

  1. <android.support.v4.widget.DrawerLayout    
  2.     xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:id="@+id/drawer_layout"    
  4.     android:layout_width="match_parent"    
  5.     android:layout_height="match_parent">    
  6.     <!-- The main content view -->    
  7.      ...  
  8.     <!-- The navigation drawer -->    
  9.     <ListView  
  10.         android:id="@+id/left_drawer"  
  11.         android:layout_width="120dp"  
  12.         android:layout_height="match_parent"  
  13.         android:layout_gravity="start"  
  14.         android:choiceMode="singleChoice"  
  15.         android:divider="@android:color/transparent"  
  16.         android:dividerHeight="0dp"  
  17.         android:background="#EEA9B8"/>    
  18. </android.support.v4.widget.DrawerLayout>  
  1.     /* 抽屉导航变量 */  
  2.     private DrawerLayout mDrawerLayout;  
  3.     private ListView mDrawerList;  
  4.     private ActionBarDrawerToggle mDrawerToggle;  
  5.   
  6. ...  
  7.   
  8.         /* 抽屉导航相关 */  
  9.         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);  
  10.         mDrawerList = (ListView) findViewById(R.id.left_drawer);  
  11.         mDrawerLayout.setDrawerShadow(R.drawable.draw, GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK);  
  12.   
  13.         mDrawerList.setAdapter(new SimpleAdapter(this, getDrawerItems(false),  
  14.                 R.layout.drawer_item, new String[] { "drawer_img",  
  15.                         "drawer_title" }, new int[] { R.id.drawer_img,  
  16.                         R.id.drawer_title }));  
  17.   
  18.         mDrawerList.setOnItemClickListener(new DrawerItemClickListener());  
  19.         mDrawerToggle = new ActionBarDrawerToggle(  
  20.                 this,                  /* host Activity */  
  21.                 mDrawerLayout,         /* DrawerLayout object */  
  22.                 R.drawable.draw,  /* nav drawer image to replace 'Up' caret */  
  23.                 R.string.search_label,  /* "open drawer" description for accessibility */  
  24.                 R.string.defalut_text  /* "close drawer" description for accessibility */  
  25.                 ) {  
  26.             public void onDrawerClosed(View view) {  
  27.                 getActionBar().setTitle("onDrawerClosed");  
  28.                 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()  
  29.             }  
  30.   
  31.             public void onDrawerOpened(View drawerView) {  
  32.                 getActionBar().setTitle("onDrawerOpened");  
  33.                 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()  
  34.             }  
  35.         };  
  36.         mDrawerLayout.setDrawerListener(mDrawerToggle);  
  37.   
  38. ...  
  39.   
  40.     /** 
  41.      * 监听抽屉导航item点击事件 
  42.      */  
  43.     private class DrawerItemClickListener implements ListView.OnItemClickListener {  
  44.   
  45.         @Override  
  46.         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  47.             selectItem(view, position);  
  48.         }  
  49.     }  
  50.   
  51.     private boolean flag = false;  
  52.     private void selectItem(View view, int position) {  
  53.         if (position == 0) {  
  54.             if (!flag) {  
  55.                 mDrawerList.setAdapter(new SimpleAdapter(this, getDrawerItems(true),  
  56.                         R.layout.drawer_item, new String[] { "drawer_img",  
  57.                                 "drawer_title" }, new int[] { R.id.drawer_img,  
  58.                                 R.id.drawer_title }));  
  59.                 flag = true;  
  60.             } else {  
  61.                 mDrawerList.setAdapter(new SimpleAdapter(this, getDrawerItems(false),  
  62.                         R.layout.drawer_item, new String[] { "drawer_img",  
  63.                                 "drawer_title" }, new int[] { R.id.drawer_img,  
  64.                                 R.id.drawer_title }));  
  65.                 flag = false;  
  66.             }  
  67.         } else {  
  68.             TextView title = (TextView) view.findViewById(R.id.drawer_title);  
  69.             String cityName = title.getText().toString();  
  70.             this.cityInput.setText(cityName);  
  71.         }  
  72.     }
智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告