Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Navigation Drawer抽屜導航實例解析

編輯:關於 Android 程式設計

側滑抽屜式菜單現在幾乎是應用標配:不用跳頁、不用按硬件菜單鍵,點一下按鈕菜單就滑出來,而且能放進去不少內容。本文以官方 DrawerLayout 為例,把它的結構梳理一遍。

準備工作

DrawerLayout 位於 Support Library 中,使用時需要引入 android-support-v4.jar,並在代碼裡 import android.support.v4.widget.DrawerLayout;。

如果找不到這個類,先用 SDK Manager 更新 Android Support Library,然後到 Android SDK\extras\android\support\v4 路徑下找到 android-support-v4.jar,複製到專案的 libs 目錄並 Add to Build Path。

新建 Android 專案時,也可以直接選擇使用 Navigation Drawer 模板:

NavigationDrawerFragment 加載了什麼

抽屜本質上是一個 ListView,這個 Fragment 負責把它 inflate 出來、綁定點擊監聽、掛上適配器:

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
     //給抽屜 ListView 找到對應的 XML 佈局
     mDrawerListView = (ListView) inflater.inflate(
             R.layout.fragment_navigation_drawer, container, false);

     //綁定點擊監聽器,點擊時選中對應項
     mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> parent, View view,
                 int position, long id) {
             selectItem(position);
         }
     });

     //綁定適配器
     mDrawerListView.setAdapter(new ArrayAdapter<String>(
             getActionBar().getThemedContext(),
             android.R.layout.simple_list_item_activated_1,
             android.R.id.text1,
             new String[]{
                     getString(R.string.title_section1),
                     getString(R.string.title_section2),
                     getString(R.string.title_section3),
                     getString(R.string.title_section4),
                     getString(R.string.title_section5),
             }));

     //設置抽屜 ListView 以顯示某一選中項的形態出現
     mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
     return mDrawerListView;
 }

抽屜列表佈局:fragment_navigation_drawer.xml

這裡用 choiceMode="singleChoice" 讓列表具備單選效果,並自訂分隔線顏色與高度:

 <ListView xmlns:android="https://schemas.android.com/apk/res/android"
     xmlns:tools="https://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@color/image_bg_green"
     android:choiceMode="singleChoice"
     android:divider="@color/image_bg_lightgreen"
     android:dividerHeight="1dp"
     tools:context="net.nowamagic.magicapp_v7.NavigationDrawerFragment" /> 

列表項佈局:fragment_main.xml

抽屜裡的每一格由一個相對佈局填充,內容就是一個 TextView:

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
     xmlns:tools="https://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     tools:context="net.nowamagic.magicapp_v7.MainActivity$PlaceholderFragment" >

     <TextView
         android:id="@+id/section_label"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
 </RelativeLayout> 

基於 Navigation Drawer 模板新建的專案,跑起來大致如下:

總結一下結構關係:DrawerLayout 作為容器,第一個子 View 是主內容區,第二個子 View 是抽屜(這裡是 ListView),而 Fragment 負責填充抽屜的數據與互動。理解這個層級之後,替換成自定義適配器或加上頭部背景,都是水到渠成的事。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved