【问题标题】:How to make navigation drawers play nicely with recyclerviews?如何使导航抽屉与 recyclerviews 配合得很好?
【发布时间】:2018-06-26 19:28:33
【问题描述】:

我有一个带有回收器视图和导航抽屉的活动。

当抽屉在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>

【问题讨论】:

    标签: android xml android-recyclerview kotlin navigation-drawer


    【解决方案1】:

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

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

    【讨论】:

    • 将 recyclerview 的高度设置为 0 没有任何可见的结果,并且将抽屉布局的高度设置为大于 recyclerview 的高度会导致 recyclerview 再次无法移动。我正在使用 android:elevation="4dp" 设置海拔。将高度设置为 0dp 也可以,但 -1dp 会停止移动,所以我不确定卡片视图是否真的升高了?
    • 我不是指 recyclerView 本身的提升,我是指在 viewHolder 布局内的卡片的提升。如果您使用app:cardElevation=0dp,请查看是否有任何变化。
    • 抽屉在屏幕底部的图像以及recyclerview的后面,所以我想我需要提升导航?更改卡片高度也没有帮助。
    【解决方案2】:

    所以我已经解决了这个问题,当您向项目添加抽屉布局时,您需要将之前在活动中的所有内容放入 content_frame 中,否则它只会将抽屉视为正常元素,在其后面吃输入。

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

    【讨论】:

      猜你喜欢
      • 2020-10-01
      • 2015-05-27
      • 2012-07-07
      • 2014-04-07
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      相关资源
      最近更新 更多