2015-06-22 25 views
10

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> 

Trả lời

14

Bạn có thể chỉ tổ ListView hoặc RecyclerView bên trong NavigationView.

<?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"/> 

     <ListView 
      android:id="@+id/menuList" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 
</android.support.v4.widget.DrawerLayout> 

LƯU Ý: Hãy ghi nhớ rằng nếu bạn sử dụng sử dụng một ListView bên trong nó, bạn không thể sử dụng tiêu đề của NavigationView. Bạn sẽ phải sử dụng chế độ xem tiêu đề của ListView mà bạn đang thêm. Đừng quên xóa các trường app:menuapp:header.

+0

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

+2

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. –

+0

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

15

Nếu bạn muốn thêm chế độ xem bên trong NavigationView, bạn có thể làm điều gì đó như thế này. Bằng cách này bạn không có giới hạn thêm Tiêu đề trên NavigtionView của bạn với ListView.

<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/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="false" 

     > 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <include layout="@layout/nav_header_main" 
       android:id="@+id/my" 
       /> 
      <ListView 

       android:layout_weight="7" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:id="@+id/list_view_inside_nav"></ListView> 
     </LinearLayout> 
    </android.support.design.widget.NavigationView> 

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

Đó giống như thế này

example

+0

Thao tác này sẽ không cuộn chế độ xem tiêu đề như Chế độ xem Điều hướng – Killer

+1

có, bạn có thể đặt chúng vào bố cục tuyến tính với hướng dọc và sau đó đặt vào trong scrollview với tham số fillViewPort = true – deadfish

+1

Nó đã thử nhưng AFAIK (nhớ) bất cứ khi nào tôi mở điều hướng ngăn kéo chứa chế độ xem tái chế thay vì xem danh sách chế độ xem không hiển thị tiêu đề thay vì được cuộn xuống chế độ xem của người tái chế. @deadfish – Killer

2

Để đối phó với Shubham 's comment

This will not scroll the header view like the Navigation View does

Tôi giải quyết nó bằng cách đặt LinearLayout bên trong một NestedScrollView. Bây giờ nó cuộn đúng với tiêu đề.

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

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

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/nav_list" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/weight_based_height" 
      android:layout_weight="1" 
      android:nestedScrollingEnabled="false"/> 
    </LinearLayout> 

</android.support.v4.widget.NestedScrollView> 
1

***** thử cách này *****

<android.support.design.widget.NavigationView 
android:id="@+id/navigation" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:layout_gravity="start" 
android:fitsSystemWindows="true"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

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

    <ListView 
     android:id="@+id/lst_menu_items" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 
</LinearLayout> 

Các vấn đề liên quan