android - 在 DrawerLayout 中滚动时标题后面的项目列表

标签 android header drawerlayout navigationview

有人可以帮我解决这个问题吗?我不明白为什么当我向下滚动时我的项目列表在我的 DrawerLayout 标题后面。

<?xml version="1.0" encoding="utf-8"?>
<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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/activity_MainActivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#6b6b6b"
            android:fitsSystemWindows="true"
            android:theme="@style/NavigationTheme"
            app:headerLayout="@layout/format_appdrawerheader"
            app:itemBackground="@drawable/nav_view_item_background"
            app:itemTextColor="@drawable/nav_view_item_textcolor"
            app:menu="@menu/category_menu">

            <include layout="@layout/format_appdrawerheader" />

        </android.support.design.widget.NavigationView>

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

谢谢你的帮助

编辑:用图片会更容易理解。

所以,当我的应用程序启动时,我的 NavigationView 看起来像那样。深色部分是标题,浅色部分是用菜单制作的列表。 向下滚动时,只有较轻的部分在移动(这正是我想要的),但它在标题下方滑动。因此,如果我像第二张图片(红色框)那样单击标题中的空白区域,它会选择后面的项目,在本例中为“项目 1”。

enter image description here enter image description here

最佳答案

编辑2

图片很有帮助。

您可以尝试将下一行添加到您的 format_appdrawerheader.xml 的顶部:

android:clickable="true"

它应该捕获标题中的点击事件,防止它继续到标题后面的 View 。

如果您使用 Android Studio 中的默认 Navigation Drawer Activity,下面我将展示该行的放置位置。您可能需要根据您的应用对其进行定制。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:clickable="true">
...

注意最后一行

希望对您有所帮助。请告诉我。

编辑

另一种解决方案是删除这一行:

<include layout="@layout/format_appdrawerheader" />

它已经被引用了几行:

app:headerLayout="@layout/format_appdrawerheader"

原始答案

我还没有测试过,但根据您的描述判断,您可能需要将 NoActionBar 主题添加到您的 styles.xml 和 AndroidManifest.xml。

样式.xml:

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
</style>

AndroidManifest.xml:

<activity
            android:name=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

请告诉我这是否有帮助,或者如果您遇到错误。我不知道您是如何设置 MainActivity.java 的。如果您遇到与 ActionBar 有关的 NullPointerException,则必须对您的项目进行额外的修改。如果您在 Android Studio 中启动一个新项目,您可以查看默认 NavigationDrawer Activity 中的代码。除了 styles.xml 和 AndroidManifest.xml 之外,您还可以查看工具栏在 MainActivity.java 中的引用方式以及 app_bar_main.xml 的设置方式。

关于android - 在 DrawerLayout 中滚动时标题后面的项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58455813/

相关文章:

android - 列索引顺序 SQLite 创建表

java - Android属性动画: how to increase view height?

javascript - 设置视口(viewport)、初始比例和设备宽度不会根据设备宽度调整大小

html - 导航栏 - 试图让导航栏出现在页面顶部

Android:具有动态 actionBar 颜色和 DrawerLayout 的透明状态栏

android - 如何创建符合 Material 指南的 ListView 项目

python - 如何获取c头文件中定义的列表?

android - 工具栏填满整个屏幕

android - 使用 Fragment Manager 和 DrawerLayout 管理多个层次结构

javascript - 在 native react 中以不同方式对齐两个 child