Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

android - Is it possible to use something other than a listview as sliding drawer in drawerlayout

I would like to have for example a LinearLayout or a RelativeLayout sliding from the left of the screen instead of a lone ListView.

I tried to use à LinearLayout with android:layout_gravity="start" and i had this error at runtime:

ClassCastException: android.widget.LinearLayout$LayoutParams cannot 
be cast to android.support.v4.widget.DrawerLayout$LayoutParams

here's the layout file:

<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"
    android:background="@android:color/white"
    >

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

    <LinearLayout 
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical">

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

        <ListView 
            android:id="@+id/left_drawer"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="@android:color/white"
        />

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

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This will work if you move both the android:id="@+id/left_drawer" (or create a new id) and set the gravity.

The id move (or new one) is so the reference is correct so you call closeDrawer() on it and not the child views.

But most importantly, the DrawerLayout requires that element to have a android:layout_gravity set on it, as you mentioned.

Finally, you need to call close closeDrawer() on the base view, the one with the required gravity.

Example:

<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"
    android:background="@android:color/white">

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

    <LinearLayout 
        android:id="@+id/left_drawer"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical">

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

        <ListView 
            android:id="@+id/left_drawer_child"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="@android:color/white" />

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

In code:

DrawerLayout mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout)
LinearLayout mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);
ListView mDrawerListChild = (ListView) findViewById(R.id.left_drawer_child);

...

mDrawer.closeDrawer(mDrawerLinear);

(This is basically what @Karakuri posted, but with a more complete explanation and example.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...