一。Create a NavigationDrawer
java
建立Navigation Drawer須要用DrawerLayout 做爲界面根控件。在DrawerLayout裏面第一個View爲當前界面主內容;第二個和第三個View爲Navigation Drawer內容。若是當前界面只須要一個Navigation Drawer,則第三個View能夠省略。android
下面的例子中,有兩個View ,第一個FramLayout是顯示內容 的主要頁面,第二個ListView 是NavigationDrawer的內容 。
app
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id ="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- the main content view --> <FrameLayout android:id ="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> <!-- the navigation drawer --> <ListView android:id ="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" android:background="#111"/> </android.support.v4.widget.DrawerLayout>
以上內容要注意有:ide
顯示界面主要的View (也就是上面的FrameLayout)必定要是DrawerLayout的第一個子Viewthis
顯示界面的View 的寬度和高度與父類的要同樣,緣由是當navigation drawer不可見的時候 ,界面 內容表明整個界面 spa
Navigation Drawer (上面的 ListView) 必須使用android:layout_gravity屬性設置水平的 gravity值 .code
抽屜菜單的寬度爲 dp
單位而高度和父View同樣。抽屜菜單的寬度應該不超過320dp,這樣用戶能夠在菜單打開的時候看到部份內容界面。
xml
二。 initialize the Drawer List事件
在您的Activity中須要先初始化Navigation Drawer內容,根據您的應用須要Navigation Drawer的內容可能不是ListView,能夠使用其餘View。ci
在上面的示例中,咱們須要給Navigation Drawer的ListView設置一個Adapter來提供數據。以下所示:
public class MainActivity extends Activity { private String [] mPlanetTitles; private DrawerLayout mDrawerLayout; private ListView mDrawerList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPlanetTitles=new String []{"title1","title2","title3"}; mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList= (ListView) findViewById(R.id.left_drawer); //set the adapter for the list view ArrayAdapter< String > adapter= new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles); mDrawerList.setAdapter(adapter); //set the click listener mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); } }
代碼 中調用 了setOnItemClickListener來接收Navigation drawer List中的點擊後的事件。
三。處理導航點擊事件
在onItemClick()中 什麼取決於你的appa 結構 ,下面的例子中,選擇每個項都會在主內容 View (也就是framelayout)中插入一個不一樣的 fragment.
private class DrawerItemClickListener implements OnItemClickListener{ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub selectItem(position); } } // Swaps fragments in the main content view FrameLayout private void selectItem(int position) { // TODO Auto-generated method stub //create a new fragment and specify thr planet to show based on position android.app.Fragment fragment= new PlanetFragment(); Bundle args = new Bundle(); args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); fragment.setArguments(args); //insert the fragment by replacing any existing fragment FragmentManager fManager = getFragmentManager(); fManager.beginTransaction() .replace(R.id.content_frame, fragment) .commit(); //high light the selected item update the title and close the drawer mDrawerList.setItemChecked(position, true); } @Override public void setTitle(CharSequence title) { // TODO Auto-generated method stub mTitle= (String) title; getActionBar().setTitle(mTitle); }