Tôi đang sử dụng NavigationView mới từ bản sửa đổi 22.2.0 của support library của Google. Nó hoạt động hoàn hảo tốt để tạo ra một ngăn kéo điều hướng dân cư bằng cách sử dụng một trình đơn res. Tôi đã tự hỏi là nó có thể thêm một ListView hoặc RecyclerView vào ngăn điều hướng để nó có thể được điền bằng cách sử dụng mã bộ điều hợp tùy chỉnh của tôi, cho phép sự linh hoạt lớn hơn nhiều so với tài nguyên menu.Thêm ListView hoặc RecyclerView vào NavigationView mới
Đây là XML hiện tại của tôi:
<?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>
đâu trong XML của tôi, tôi sẽ thêm ListView hoặc RecyclerView?
EDIT
Theo đề nghị Basant, tôi lồng một ListView vào NavigationView. Bạn mất khả năng phồng từ một trình đơn res (theo như tôi biết) nhưng nó thành công trong những gì tôi muốn nó làm. XML tiêu đề không thay đổi, nó chỉ được đưa vào XML.
Code mới:
<?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>
Xin chào, tôi đã cố gắng đó nhưng vỡ nav ngăn kéo theo nghĩa các các mục điều hướng không còn có thể nhấp được nữa. Chỉ cần nhìn thấy chỉnh sửa LƯU Ý của bạn, tôi sẽ thử ngay bây giờ. – nabir
Tôi nghĩ bạn không nên sử dụng NavigationMenu trong trường hợp đó. Chỉ cần sử dụng một ListView hoặc một bố trí phức tạp hơn nếu bạn cần, thay cho một NavigationMenu. Điều đó sẽ giúp bạn linh hoạt hơn. –
Lý do tôi muốn sử dụng NavigationView là do xây dựng trong xử lý scrim để cung cấp cho nó cái nhìn vật chất hơn, tôi đã áp dụng đề xuất của bạn và có vẻ như nó hoạt động. Tôi sẽ cập nhật bài đăng của mình để hiển thị mã mới. – nabir