简体   繁体   中英

Android DrawerLayout not Closing

I am having a problem getting an Android DrawerLayout to close.

In my implementation I am using a LinearLayout rather than a listView. The list view implementation is the one commonly be found in most of the tutorials and discussed on this site.

In my implementation I want to use the same DrawerLayout on all activities of my application. For that reason, in all of my XML layout files I use an include to cut down the amount of code one needs to store.

The functionality of the drawer and it's buttons is exactly as I want them to be. The draw slides open on a swipe, closes on a swipe and all buttons respond with the appropriate functionality. However, I cannot get the drawer to close programmatically by calling any of the following. It simply has no effect.

drawerLayout.closeDrawers();        
drawerLayout.closeDrawer(GRAVITY_START);
drawerLayout.closeDrawer(drawerLinearLayout);

I tried a few variants on this like calling the whole DrawLayout object from the veiw to no avail, I tried different gravity options. Here I just got run time errors complaining about illegal states. Why is closing the draw programmatically such a hard thing to do? Why does closeDrawers not work? How can I close it simply and cleanly?

Below is some code from one such Activity's layout.

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

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

        <include android:id="@+id/left_draw"
         android:layout_width="240dp"
         android:layout_height="match_parent"
         layout="@layout/options_draw"
         android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>

The include refers to a layout which is below. It defines a layout of 6 buttons plus two radio buttons.

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/splash_screen1"
        android:orientation="vertical"
        android:weightSum="8" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@color/t_blue"
            android:orientation="horizontal"
            android:weightSum="10" >

            <Button
                android:id="@+id/cmd_map"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="5"
                android:background="@drawable/gradient_button1"
                android:onClick="option1Selected"
                android:text="@string/drawer_cmd_map" />

             <Button
                 android:id="@+id/cmd_locdata"
                 android:layout_width="0dp"
                 android:layout_height="match_parent"
                 android:layout_margin="2dp"
                 android:layout_weight="5"
                 android:background="@drawable/gradient_button1"
                 android:onClick="option2Selected"
                 android:text="@string/drawer_cmd_locdata" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@color/t_blue"
            android:orientation="horizontal"
            android:weightSum="10" >

            <Button
                android:id="@+id/cmd_hurlist"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="5"
                android:background="@drawable/gradient_button1"
                android:onClick="option3Selected"
                android:text="@string/drawer_cmd_hurlisrt" />

             <Button
                 android:id="@+id/cmd_settings"
                 android:layout_width="0dp"
                 android:layout_height="match_parent"
                 android:layout_margin="2dp"
                 android:layout_weight="5"
                 android:background="@drawable/gradient_button1"
                 android:onClick="option4Selected"
                 android:text="@string/drawer_cmd_legend" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_weight="2"
            android:background="@drawable/bk_gradient4"
            android:orientation="vertical"
            android:weightSum="10" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="10px"
                android:gravity="center"
                android:paddingTop="5dp"
                android:text="@string/drawer_txt_datapoints"
                android:textAppearance="?android:attr/textAppearanceMedium" />

                <RadioGroup
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="10dp"
                android:id="@+id/radio_group">

                   <RadioButton
                       android:id="@+id/first_radio_button"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:checked="true"
                       android:paddingLeft="40dip"
                       android:text="@string/drawer_rb_data_option1"/>

                 <RadioButton
                     android:id="@+id/second_radio_button"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:checked="false"
                     android:paddingLeft="40dip"
                     android:text="@string/drawer_rb_data_option2" />

               </RadioGroup>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@color/t_blue"
            android:orientation="horizontal"
            android:weightSum="10" >

            <Button
                android:id="@+id/cmd_legend"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="5"
                android:background="@drawable/gradient_button1"
                android:onClick="option5Selected"
                android:text="@string/drawer_cmd_settings" />

             <Button
                 android:id="@+id/cmd_about"
                 android:layout_width="0dp"
                 android:layout_height="match_parent"
                 android:layout_margin="2dp"
                 android:layout_weight="5"
                 android:background="@drawable/gradient_button1"
                 android:onClick="option6Selected"
                 android:text="@string/drawer_cmd_about" />

        </LinearLayout>
</LinearLayout>

I am aware there is another topic on this problem ( here ), but this relates specifically to a DrawLayout with a listView. I am creating a drawerLayout with a LinearLayout so this question is different.

It turns out that in my implementation all of my activities inherited from an activity (BaseActivity) that performed all the operations common to each screen of my application. As my sliding draw was on every screen I felt this was the best place to put that logic.

The BaseActivity class was setting a layout which I defined to have a dummy layout because the child classes would override onCreate() and supply their own. This would normally work except for the requirement that the onCreate method must call super.onCreate as its first statement. Therefore it the BaseActivities layout that gets inflated and the child class inflates its own ie the BaseActivity and child activity are pointing to different layouts.

I solved this by introducing a method called setLayout in the base class which sets the layout, this is designed to be overridden by the child classes and gets over this problem.

This is useful for anyone considering inheritance in Android's activities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM