简体   繁体   English

单击抽屉外部时,Android导航抽屉不会关闭(主活动中的片段)

[英]Android navigation drawer does not close when click outside the drawer (a fragment in main activity)

For some reason, the navigation drawer does not close when tapping outside the drawer.Its another fragment with ListView in MainActivity: 出于某种原因,当在抽屉外部敲击时导航抽屉不会关闭。在MainActivity中使用ListView的另一个片段:

在此输入图像描述

When tapping the fragment on right it does not close drawer instead it acts like as if the fragment occupies the whole screen and the click listener is still active in the fragment. 当点击右侧的片段时,它不会关闭抽屉,而是就像片段占据整个屏幕并且点击监听器在片段中仍处于活动状态一样。

activity_main.xml: activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_orange_dark"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:title="Apartment Guide"
    app:titleTextColor="@android:color/white" />

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    tools:context=".MainActivity">

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/header"
        app:itemTextColor="@android:color/darker_gray"
        app:menu="@menu/drawer_menu" />

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

<FrameLayout
    android:id="@+id/fragment_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar">

</FrameLayout>

MainActivity.java: MainActivity.java:

    @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mDrawerLayout = findViewById(R.id.drawer);
    fragmentContent = findViewById(R.id.fragment_content);
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_opened, R.string.drawer_closed) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getSupportActionBar().setTitle(R.string.drawer_opened);
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            getSupportActionBar().setTitle(R.string.drawer_closed);
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
            fragmentContent.setTranslationX(slideOffset * drawerView.getWidth());
            mDrawerLayout.bringChildToFront(drawerView);
            mDrawerLayout.requestLayout();
        }
    };
    mToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));

    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    if (savedInstanceState == null)
        selectDrawerItem(null);

    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            selectDrawerItem(menuItem);
            return true;
        }
    });
}

Not sure where I'm doing wrong as closing drawer when tapping outside the drawer area should be the default behavior in drawerLayouts. 在抽屉区域外点击时,不确定我做错了什么作为关闭抽屉应该是drawerLayouts中的默认行为。

This happens because you NavigationDrawer is not capturing the view because the activity elements are not included in it, and to have it working properly, you have to make the NavigationDrawer is the main root of your Activity layout and the RelativeLayout is a child of it, like the following: 发生这种情况是因为NavigationDrawer没有捕获视图,因为活动元素未包含在其中,并且要使其正常工作,您必须使NavigationDrawer成为Activity布局的主根,而RelativeLayout是它的子元素,如下:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    tools:context=".MainActivity">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_orange_dark"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:title="Apartment Guide"
            app:titleTextColor="@android:color/white" />

        <FrameLayout
            android:id="@+id/fragment_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar">

        </FrameLayout>

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/header"
        app:itemTextColor="@android:color/darker_gray"
        app:menu="@menu/drawer_menu" />

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

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

相关问题 Android Navigation Drawer Activity 调用来自 Fragment 的 Click 事件 - Android Navigation Drawer Activity call Click event from Fragment 片段或活动中的导航抽屉? - Navigation Drawer in Fragment or Activity? Android导航抽屉片段 - Android Navigation Drawer Fragment 使用导航抽屉在主活动中加载的片段中的选项卡/滑动视图 - Tabs/Swipe view in fragment loaded in main activity using navigation drawer 菜单项在导航抽屉中单击,目的在于承载片段的活动 - Menu item click in a navigation drawer intent to activity hosting a fragment 当MainActivity(主活动)通过导航抽屉扩展BaseActivity时,其上的按钮不会注册点击 - Buttons on MainActivity do not register click when it(Main Activity) extends a BaseActivity with Navigation Drawer 如何从导航抽屉的片段移动到 Android Studio 上的活动? - How to move from a fragment of a navigation drawer to an Activity on Android Studio? 如何将数据从活动传递到导航抽屉android中的片段 - How to pass data from an activity to a fragment inside a navigation drawer android 刷新导航抽屉片段Android吗? - Refresh Navigation Drawer Fragment Android? Android Studio上带有片段的导航抽屉 - Navigation drawer with fragment on Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM