我正在使用Google支持库的修订版 22.2.0 中的新NavigationView。它可以很好地生成使用菜单 res 填充的导航抽屉。
我想知道是否可以将 ListView 或 RecyclerView 添加到导航抽屉,以便可以使用我的自定义适配器代码填充它,这比菜单资源具有更大的灵活性。
这是我当前的 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/content_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/main_toolbar" />
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_drawer_header"
app:menu="@menu/menu_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
我将在我的 XML 中的哪个位置添加 ListView 或 RecyclerView?
编辑
根据 Basant 的建议,我将 ListView 嵌套到 NavigationView 中。你失去了从菜单 res 充气的能力(据我所知),但它成功地完成了我想要它做的事情。标头 XML 没有改变,它只是包含在 XML 中。
新代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/content_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/main_toolbar" />
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/navigation_drawer_header_include"
layout="@layout/navigation_drawer_header" />
<ListView
android:id="@+id/navigation_drawer_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/navigation_drawer_header_include"/>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>