使用DrawerLayout

引言

本文主要參考Android開發(fā)文檔Creating a Navigation Drawer

創(chuàng)建DrawLayout布局

為了在項(xiàng)目中引入DrawLayout布局,讓DrawLayout對象作為布局的根視圖,然后在這個DrawLayout里面添加一個主內(nèi)容視圖(當(dāng)導(dǎo)航抽屜隱藏時,顯示內(nèi)容的布局)以及一個包含導(dǎo)航抽屜內(nèi)容的視圖。如下所示:

<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">

    <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="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>

引入DrawerLayout布局要注意一些要求:

  • 主要內(nèi)容的視圖(FrameLayout)必須是DrawLayout的第一個子節(jié)點(diǎn), 因?yàn)閷?dǎo)航抽屜是在主要內(nèi)容視圖的上面。
  • 主要內(nèi)容視圖的layout_width layout_height均設(shè)置為match_parent,因?yàn)楫?dāng)導(dǎo)航抽屜隱藏時,它填充了整個界面。
  • 抽屜視圖(ListView)必須指定其水平重力(android:layout_gravity)屬性。如果指定為左菜單時,值為left,右菜單為right。然而不過要跟隨系統(tǒng)語言方向(比如系統(tǒng)文字是從做到右,或者從右到左)用start
  • 抽屜視圖用dp單位來指定其寬度,并且高度匹配父視圖。抽屜的寬度不能超過320 dp, 因此用戶總是可以看到主要內(nèi)容視圖的一部分。

初始化Drawer ListView

初始化列表就是為抽屜里的列表填充數(shù)據(jù),這里我們將數(shù)據(jù)定義在res/values/strings.xml里,如下所示

<string-array name="drawer_menu">
        <item>Menu 1</item>
        <item>Menu 2</item>
        <item>Menu 3</item>
        <item>Menu 4</item>
</string-array>

ListView中的子項(xiàng)創(chuàng)建布局如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textColor="#fff"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

接下來,在MainActivity.java中對ListView進(jìn)行初始化,配置適配器,設(shè)置監(jiān)聽器,如下所示:

// 定義變量
private String[] mDrawerMenuTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerListView;

// 初始化變量
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerListView = (ListView)findViewById(R.id.left_drawer);
mDrawerMenuTitles = getResources().getStringArray(R.array.drawer_menu);

// 為ListView設(shè)置適配器
mDrawerListView.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mDrawerMenuTitles));

// 為ListView設(shè)置監(jiān)聽器
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);   // 對點(diǎn)事件的處理
    }
});

處理ListView中子項(xiàng)的點(diǎn)擊事件

這里實(shí)現(xiàn)點(diǎn)擊事件為切換主界面的Fragment,每點(diǎn)擊一個Item,就會創(chuàng)建相應(yīng)的ContentFragment,然后更換掉主界面的Fragment。因此要創(chuàng)建一個繼承自FragmentContentFragment,首先創(chuàng)建一個fragment_content布局:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text2"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</TextView>

創(chuàng)建ContentFragment類,以及實(shí)現(xiàn)selectItem()(點(diǎn)擊事件的處理)


public static class ContentFragment extends Fragment {
    public static final String ARG_DRAWER_MENU_NUMBER = "drawer_menu_number";

    public ContentFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_content, container, false);
        int i = getArguments().getInt(ARG_DRAWER_MENU_NUMBER);
        String menuItem = getResources().getStringArray(R.array.drawer_menu)[i];

        ((TextView)rootView.findViewById(R.id.text2)).setText(menuItem);
        return rootView;
    }
}
    
private void selectItem(int position){
    Fragment fragment = new ContentFragment();
    Bundle args = new Bundle();
    switch (position) {
        case 0:
            args.putInt(ContentFragment.ARG_DRAWER_MENU_NUMBER, position);
            break;
        case 1:
            args.putInt(ContentFragment.ARG_DRAWER_MENU_NUMBER, position);
            break;
        case 2:
            args.putInt(ContentFragment.ARG_DRAWER_MENU_NUMBER, position);
            break;
        case 3:
              args.putInt(ContentFragment.ARG_DRAWER_MENU_NUMBER, position);
            break;
        default:
            break;
    }
    // FragmentActivity將點(diǎn)擊的菜單列表標(biāo)題傳遞給Fragment
    fragment.setArguments(args); 

    // 插入Fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // 更新選擇后的item和title,然后關(guān)閉菜單
    mDrawerListView.setItemChecked(position, true);
    setTitle(mDrawerMenuTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerListView);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容