Android导航抽屉菜单无法点击?

时间:2016-06-21 07:23:48

标签: android navigation-drawer

我有一个布局。它包含抽屉菜单。我想在打开此菜单时禁用后台视图。我尝试了很多方法,但他们没有工作。 (setClickable,requestDisallowInterceptTouchEvent)。如何解决这个问题?伙计们,我快要疯了。

HomeFragment.java

 @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        .....
        .....
        setMenu();

}

    private void setDrawerLayout() {
            drawerLayout = (DrawerLayout) getActivity().findViewById(R.id.DrawerLayout);

            ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, getToolBar(),
                    R.string.openDrawer, R.string.closeDrawer) {

                @Override
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);


                }

                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);

                }

            };
            mDrawerToggle.setDrawerIndicatorEnabled(false);

            final Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_menu_button___a, getActivity().getTheme());

            mDrawerToggle.setHomeAsUpIndicator(drawable);

            mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (drawerLayout.isDrawerVisible(GravityCompat.START)) {
                        drawerLayout.closeDrawer(GravityCompat.START);
                    } else {
                        drawerLayout.openDrawer(GravityCompat.START);
                    }
                }
            });
            drawerLayout.setDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggled
            mDrawerToggle.syncState();
        }

activity_home.xml

   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activityRoot"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <include layout="@layout/toolbar"></include>
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/DrawerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:layout_below="@+id/appBarLayout"
            android:elevation="7dp">

            <FrameLayout
                android:id="@+id/frameLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/tabs">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <android.support.v4.view.ViewPager
                        android:id="@+id/viewpager"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:layout_behavior="@string/appbar_scrolling_view_behavior"
                        android:layout_above="@+id/tabs" />

                    <android.support.design.widget.TabLayout
                        android:id="@+id/tabs"
                        style="@style/MyCustomTabLayout"
                        android:layout_width="match_parent"
                        android:layout_height="?actionBarSize"
                        android:layout_alignParentBottom="true"
                        android:background="@drawable/bottom_bar_background"
                        app:tabMode="fixed"
                        app:tabPaddingEnd="0dp"
                        app:tabPaddingStart="0dp"
                        app:tabTextAppearance="@style/MyCustomTabText" />
                </RelativeLayout>

            </FrameLayout>

            <include
                layout="@layout/navigation_drawer_menu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/tabs"
                android:layout_gravity="start"
                android:layout_marginBottom="?actionBarSize" />
        </android.support.v4.widget.DrawerLayout>


    </RelativeLayout>

navigation_drawer_menu.xml

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollIndicators="right">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingRight="@dimen/drawer_menu_padding_right">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#414040"></LinearLayout>
    </LinearLayout>

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/scrollView"
        android:layout_above="@+id/editShelfButton"
        android:layout_marginBottom="@dimen/drawer_menu_background_padding">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/orderLayout">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/orderImage"
                    android:src="@drawable/menu_header_green"
                    android:scaleType="fitXY" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="53dp"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/order"
                    android:textColor="@android:color/white"
                    android:id="@+id/orderText"
                    android:paddingLeft="@dimen/content_title_padding"
                    android:layout_centerVertical="true"
                    android:paddingTop="7dp" />

            </RelativeLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/sortTypeList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/drawer_menu_background_padding"
                android:layout_marginBottom="@dimen/drawer_menu_background_padding"
                android:paddingRight="@dimen/form_padding_low" />

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/LibraryLayout"
                >

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView3"
                    android:src="@drawable/menu_header_orange"
                    android:scaleType="fitXY" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="53dp"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/my_library"
                    android:textColor="@android:color/white"
                    android:id="@+id/libraryText"
                    android:layout_centerVertical="true"
                    android:paddingLeft="@dimen/content_title_padding"
                    android:paddingTop="7dp" />
            </RelativeLayout>
            <android.support.v7.widget.RecyclerView
                android:id="@+id/libraryShelfList"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/drawer_menu_background_padding"
                android:paddingRight="@dimen/form_padding_low" />


        </LinearLayout>
    </ScrollView>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editShelfButton"
        android:background="#c80808"
        android:layout_marginRight="@dimen/drawer_menu_padding_right"
        android:text="@string/edit_shelves"
        android:layout_alignParentBottom="true"
        android:textColor="@android:color/white" />

</RelativeLayout>

    </RelativeLayout>

fragment_home.xml

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/homeFrame"
    xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_height="match_parent">
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/libraryProductList"
                    android:scrollbars="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
        </LinearLayout>

</FrameLayout>

1 个答案:

答案 0 :(得分:-1)

确定。我找到了解决方案。我将一个蒙版按钮放入framelayout。当我打开菜单时,我将其显示为

<强> activity_home.xml

  <FrameLayout
                android:id="@+id/frameLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/tabs">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <android.support.v4.view.ViewPager
                        android:id="@+id/viewpager"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:layout_behavior="@string/appbar_scrolling_view_behavior"
                        android:layout_above="@+id/tabs" />

                    <android.support.design.widget.TabLayout
                        android:id="@+id/tabs"
                        style="@style/MyCustomTabLayout"
                        android:layout_width="match_parent"
                        android:layout_height="?actionBarSize"
                        android:layout_alignParentBottom="true"
                        android:background="@drawable/bottom_bar_background"
                        app:tabMode="fixed"
                        app:tabPaddingEnd="0dp"
                        app:tabPaddingStart="0dp"
                        app:tabTextAppearance="@style/MyCustomTabText" />
                </RelativeLayout>

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/maskButton"
                    android:background="@null"
                    android:visibility="gone" />

            </FrameLayout>

HomeFragment.java

 mask = (Button) getActivity().findViewById(R.id.maskButton);


        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, getToolBar(),
                R.string.openDrawer, R.string.closeDrawer) {

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                mask.setVisibility(View.VISIBLE);

            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                mask.setVisibility(View.GONE);

            }

        };