简体   繁体   English

Android DrawerLayout 未关闭

[英]Android DrawerLayout not Closing

I am having a problem getting an Android DrawerLayout to close.我在关闭 Android DrawerLayout 时遇到问题。

In my implementation I am using a LinearLayout rather than a listView.在我的实现中,我使用的是 LinearLayout 而不是 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.在我的实现中,我想对我的应用程序的所有活动使用相同的 DrawerLayout。 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.出于这个原因,在我所有的 XML 布局文件中,我使用包含来减少需要存储的代码量。

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.我尝试了一些变体,比如从视图调用整个 DrawLayout 对象无济于事,我尝试了不同的重力选项。 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?为什么 closeDrawers 不起作用? How can I close it simply and cleanly?我怎样才能简单干净地关闭它?

Below is some code from one such Activity's layout.下面是来自一个这样的 Activity 布局的一些代码。

<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.它定义了 6 个按钮和两个单选按钮的布局。

<?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.我知道关于这个问题还有另一个主题( 这里),但这特别与带有 listView 的 DrawLayout 相关。 I am creating a drawerLayout with a LinearLayout so this question is different.我正在创建一个带有 LinearLayout 的 drawerLayout 所以这个问题是不同的。

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.事实证明,在我的实现中,我的所有活动都从一个活动 (BaseActivity) 继承,该活动执行我的应用程序的每个屏幕的所有通用操作。 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. BaseActivity 类正在设置我定义为具有虚拟布局的布局,因为子类将覆盖 onCreate() 并提供自己的布局。 This would normally work except for the requirement that the onCreate method must call super.onCreate as its first statement.除了 onCreate 方法必须调用 super.onCreate 作为其第一条语句的要求外,这通常会起作用。 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.因此,它的 BaseActivity 布局被膨胀,子类膨胀它自己,即 BaseActivity 和子活动指向不同的布局。

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.我通过在设置布局的基类中引入一个名为 setLayout 的方法解决了这个问题,它被设计为被子类覆盖并解决了这个问题。

This is useful for anyone considering inheritance in Android's activities.这对于考虑在 Android 活动中继承的任何人都很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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