Android - Open/Close 一个不仅仅是 ListView 的抽屉

Android - Open/Close a drawer with more than just a ListView

我想在按下操作栏主页按钮时通过代码 open/close 一个抽屉,所以我在 onOptionsItemSelected 中做了以下操作:

case android.R.id.home:
    if (mDrawerLayout.isDrawerOpen(mDrawerListView)) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
    } else {
        mDrawerLayout.openDrawer(GravityCompat.START);
    }
    return true;

问题是,我的抽屉不仅仅是一个 ListView,而是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/mainPhoto"
        android:layout_width="match_parent"
        android:contentDescription="@null"
        android:layout_height="128dp" />

    <ListView
        android:id="@+id/lvOptions"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="3dp"
        android:background="#cccc" />

</LinearLayout>

并在 activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.example.example.DrawerFragment"
        tools:layout="@layout/drawer_fragment" />

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

所以当上面的代码执行时,它给了我一个转换错误:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams

我试图给 LinearLayout 一个 id 并使用它,但它没有帮助,或者我做错了什么......请指教

检查 drawer 是否打开 gravity 对我有用,而不是检查 view。这是我的代码:

public void openMenu(int gravity){
    if (mDrawerLayout.isDrawerOpen(gravity))
        mDrawerLayout.closeDrawer(gravity);
    else
        mDrawerLayout.openDrawer(gravity);

}