【问题标题】:List of items behind header when scrolling in DrawerLayout在 DrawerLayout 中滚动时标题后面的项目列表
【发布时间】:2019-10-18 17:45:39
【问题描述】:

有人可以帮我解决这个问题吗?当我向下滚动时,我不明白为什么我的项目列表在 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”。

【问题讨论】:

  • 有什么问题?当滚动时,项目位于标题后面是正常行为。你还想要什么?
  • 问题不在于项目在标题后面的事实,问题在于即使它们不可见,它们仍然可以点击。

标签: android header drawerlayout navigationview


【解决方案1】:

编辑2

图片很有帮助。

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

android:clickable="true"

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

如果您使用 Android Studio 中的默认导航抽屉活动,我将在下面展示该行的放置位置。您可能需要根据您的应用对其进行定制。

<?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。

styles.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 的设置方式。

【讨论】:

  • 第一部分不起作用。如果我删除“包含布局”行,当我滚动时我的所有布局都在移动(我只希望底部移动)。如果我删除“app headerLayout”行,所有包含类别的底部部分都从抽屉顶部开始,在标题后面。通过使用这两行,标题不会移动,并且类别部分在标题之后开始。这很好用。除了,当我向下滚动时,底部滑到标题后面。如果我单击标题中的空白区域,它会捕获标题后面不可见的类别。
  • 我刚刚发现了这一点,但滚动条从我的标题开始,这是无用的。它应该从底部开始以代表现实。我会加一些截图,这样就容易理解了。
  • 对于第二部分,我不能声明“NoActionBar”,我的 Activity 中有一个,它是我应用程序的战略部分。我不认为操作栏与我的问题有任何关系(或者我可能对操作栏的工作方式一无所知,鉴于我在这方面的技能,这是可能的......)
  • 图片很有帮助。请尝试我的新解决方案。
猜你喜欢
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 2022-12-06
  • 2011-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多