Добавить ListView или RecyclerView в новый NavigationView
Я использую новый NavigationView из версии 22.2.0 библиотеки поддержки от Google, Он отлично работает для создания навигационного ящика, заполненного с помощью меню 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>
Ответы
Ответ 1
Вы можете просто вставить ListView
или RecyclerView
внутри 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>
ПРИМЕЧАНИЕ. Имейте в виду, что если вы используете ListView внутри него, вы не можете использовать заголовок NavigationView. Вам нужно будет использовать представление заголовка списка ListView, который вы добавляете. Не забудьте удалить поля app:menu
и app:header
.
Ответ 2
Если вы хотите добавить виды в NavigationView, вы можете сделать что-то вроде этого. Таким образом, у вас нет ограничений на добавление заголовка в ваш NavigtionView с помощью 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>
Что-то вроде этого
![example]()
Ответ 3
В ответ на комментарий Шубхам
Это не будет прокручивать представление заголовка, как это делает представление навигации
Я решил это, поместив LinearLayout
в NestedScrollView
. Теперь он правильно прокручивает заголовок.
<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>
Ответ 4
Попробуй так
<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>