NavigationView中的ListView不滚动

问题描述

我一直在尝试使NavigationView可滚动-以便能够查看其中的所有项目。之前,我曾使用菜单项来填充我的NavigationView,但NavigationView并未滚动。在网上进行大量搜索之后,使NavigationView可滚动的最流行的建议似乎是在NavigationView中使用ListView。我试过了,但是我的NavigationView仍然无法滚动。

这是我的XML:

<androidx.drawerlayout.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">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="#1F262F"
        android:fitsSystemWindows="true"
        android:paddingTop="24dp"
        android:paddingBottom="0dp"
        app:itemIconTint="#FFFFFF"
        app:itemTextColor="#FFFFFF">

        <ListView
            android:id="@+id/navList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.navigation.NavigationView>

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

</androidx.drawerlayout.widget.DrawerLayout>

请提出一个解决方案,以便我可以向下滚动NavigationView来查看其中的所有项目。 请帮忙!

解决方法

您尝试使用ScrollView吗?

另外,请注意ListView现在有layout_width="wrap_content"

<androidx.drawerlayout.widget.DrawerLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >
            
            <ListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </ScrollView>            
    </com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>