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

Android 導航抽屜 Navigation Drawer 實作教學

編輯:關於 Android 程式設計

2013 年前後,YouTube、Facebook、Path 等應用紛紛採用 Navigation Drawer 這種導航形式;Google 也為了解決 Android 錢版面越來越混亂的問題,把它納入官方設計規範。雖然後來 Google+ 改用了別的方案,引發過一輪關於抽屜導航利弊的討論,但對開發者而言,把功能做出來才是重點。

版面配置

<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"
    tools:context="com.example.naviganationdrawer.MainActivity" >

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/mDrawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <!-- 主內容必須是第一個子 View -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!-- 導航抽屜 -->
        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="120dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/holo_red_light"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />

    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

DrawerLayout 作為版面的根控件。其中第一個子 View 是當前界面的主要內容,第二與第三個子 View 則是抽屜選單內容。若只需要一個抽屜,第三個 View 可以省略。

顯示主要內容的 View 一定要放在第一位。原因在於 XML 版面中的 View 順序對應 Android 系統的 z-ordering,而抽屜必須疊在內容之上,順序錯了它就會被蓋住。

右側內容的版面

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/content_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</FrameLayout>

onCreate 中的初始化

mPlanetTitles = new String[]{"財經", "科技"};

mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, mPlanetTitles));

mDrawerLayout = (DrawerLayout) findViewById(R.id.mDrawerLayout);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

點擊事件

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 FragmentContent(mImageList[position]);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame, fragment)
            .commit();

    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

這裡做了四件事:替換主內容區的 Fragment、更新列表的勾選狀態、設定標題、關閉抽屜。最後那句 closeDrawer() 別忘了——使用者選完項目後抽屜應該自動收起,這是預期行為。

設定標題

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

FragmentContent

public class FragmentContent extends Fragment {

    private int resourceId;
    private View view;
    private ImageView mImageView;

    public FragmentContent(int i) {
        resourceId = i;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragmentcontent, null);
        mImageView = (ImageView) view.findViewById(R.id.content_image);
        mImageView.setImageResource(resourceId);
        return view;
    }
}

搭配 ActionBar 的抽屜切換按鈕

實作到這裡,抽屜已經能用了,但還缺一個直覺的開關入口。官方規範建議在 ActionBar 左側放一個漢堡選單圖示,並在抽屜展開時把它變換成返回箭頭,同時讓 ActionBar 的標題改成目前所選頁面的名稱。這部分可以透過 ActionBarDrawerToggle 來處理,你只要把 DrawerLayout 與 ActionBar 傳進建構子,並在 Activity 的 onPostCreate 中呼叫 syncState() 即可。

此外別忘了處理 onOptionsItemSelected,讓使用者點擊 ActionBar 左側圖示時也能開合抽屜。少了這一步,抽屜就只能靠滑動開啟,桌上型模擬器或平板上的操作體驗會打折扣。

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