Tell me how to make a dynamic list with items in NavigationView. I tried to do through ListView simply by placing the ListView in the markup of the NavigationView template and filling it through the adapter, but the application crashes with the error:

Standard template in NavigationView widget inside NavigationDrawer:

<?xml version="1.0" encoding="utf-8"?> <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="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer"/> </android.support.v4.widget.DrawerLayout> 

Menu items are defined in the menu resource file and assigned to NavigationView in the line:

 app:menu="@menu/activity_main_drawer" 

Here is the file @ menu / activity_main_drawer:

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_camera" android:icon="@drawable/ic_menu_camera" android:title="Import"/> <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_menu_gallery" android:title="Gallery"/> <item android:id="@+id/nav_slideshow" android:icon="@drawable/ic_menu_slideshow" android:title="Slideshow"/> <item android:id="@+id/nav_manage" android:icon="@drawable/ic_menu_manage" android:title="Tools"/> </group> <item android:title="Communicate"> <menu> <item android:id="@+id/nav_share" android:icon="@drawable/ic_menu_share" android:title="Share"/> <item android:id="@+id/nav_send" android:icon="@drawable/ic_menu_send" android:title="Send"/> </menu> </item> </menu> 

Instead of these items, I tried to create a dynamically created ListView just by inserting it there:

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <ListView android:id = "@+id/listView" android:layout_width = "match_parent" android:layout_height = "match_parent"/> </menu> 

And initialize via the adapter from onCreate ():

 ListView listView = ( ListView ) findViewById( R.id.listView ); final String[] sets = new String[] { "one", "two", "three", "four", "five" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, sets ); listView.setAdapter( adapter ); 

The application crashes with an error. In fact, it is clear that while NavigationView is not open, the ListView does not exist and it will not be possible to initialize a non-existing element when creating activity.

  • you need to put your ListView right on the markup, wherever you have NavigationDrawer itself. - Vladyslav Matviienko
  • @metalurgus, in a standard template with NavigationDrawer that offers AndroidStudio, I simply deleted the default menu items and pasted there a ListView. Actually, as I understand it, this is the very markup NavigationDrawer. Or is there some other markup? - Evgeny Kuznetsov
  • see the answer. Markup is a file of your layout - Vladyslav Matviienko

1 answer 1

 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Сюда вставьте ваш основной layout --> <FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- а это ListView, в котором будет меню--> <ListView android:id="@+id/listView " android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="right" android:choiceMode="singleChoice" android:divider="@color/list_divider" android:dividerHeight="1dp" android:listSelector="@drawable/list_selector" android:background="@color/list_background"/> </android.support.v4.widget.DrawerLayout> 

Then just get a ListView using findViewById(R.id.listView) , and work with it as you would with a regular ListView

This, if not strange, is described in the official documentation . You should first look there.

  • It looks like I asked a little wrong question, confused in terms. In NavigationDrawer, the NavigationView widget is used - that’s what I need to place a dynamic list in, rather than replace it with a list. I apologize, I did not understand right away. - Evgeny Kuznetsov
  • @ Yevgeny Kuznetsov, well, show that where used. But somehow it is not clear, and not obvious ... - Vladyslav Matviienko
  • updated the question. - Evgeny Kuznetsov