如何让导航抽屉与 recyclerviews 兼容?

How to make navigation drawers play nicely with recyclerviews?

我有一个 activity 带有 recyclerview 和导航抽屉。

当抽屉在 recyclerview 前面并且我打开它时它仍然出现在所有内容的后面。

但是当抽屉在 recyclerview 后面时,recyclerview 没有任何输入并且抽屉工作正常。

Broken Layering

<?xml version="1.0" encoding="utf-8"?>

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true" />

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

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/footer"
    android:layout_below="@id/header">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

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

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
</RelativeLayout>

您需要检查视图的 elevation 属性。这允许视图更改它们的 z 位置,因此它们被绘制在其他视图之上并投影,尽管它们在视图层次结构中处于下方。

您的 recyclerView 项目似乎使用 cardView,默认高度为 4dp,而 navigationDrawer 则更少(我认为 2dp?)。

所以我已经解决了这个问题,当您将 drawer_layout 添加到您的项目时,您需要将之前在 activity 中的所有内容放入 content_frame 或者它只会将抽屉视为一个普通元素,吃掉它后面的输入。

https://developer.android.com/training/implementing-navigation/nav-drawer