Reputation: 1496
I have a navigation drawer in my android app. I can show a menu in navigation drawer with navigationView, but I want to show a listview or recyclerview in the drawer. I need so because drawer menu is not the same for every user. the users choose the newspapers which they want to read. for example:
user A's listview will be like this : Washington Post, New York Times, Guardian, Independent.
user B's listview : Daily Mail, Washington Post, Financial Times.
How can i add listview on navigationView?
activity_main.xml :
<android.support.v4.widget.DrawerLayout >
...
<android.support.design.widget.CoordinatorLayout>
...
<android.support.design.widget.AppBarLayout>
...
<android.support.v7.widget.Toolbar/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_drawer"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/menu_drawer"
app:layout="@layout/custom_row"/>
menu_drawer.xml :
<group
android:id="@+id/group_1"
android:checkableBehavior="single">
<item
android:id="@+id/nav_first_fragment"
android:icon="@drawable/ic_number_0"
android:title="First" />
<item
android:id="@+id/nav_second_fragment"
android:icon="@drawable/ic_number_1"
android:title="Second" />
<item
android:id="@+id/nav_third_fragment"
android:icon="@drawable/ic_number_2"
android:title="Third" />
</group>
<group
android:id="@+id/group_2"
android:checkableBehavior="single">
<item
android:id="@+id/navigation_item_11"
android:icon="@drawable/ic_number_0"
android:title="My Newspapers">
<menu>
<item
android:id="@+id/navigation_item_1"
android:icon="@drawable/ic_number_0"
android:title="@string/navigation_item_1" />
<item
android:id="@+id/navigation_item_2"
android:icon="@drawable/ic_number_1"
android:title="@string/navigation_item_2" />
<item
android:id="@+id/navigation_item_3"
android:icon="@drawable/ic_number_2"
android:title="@string/navigation_item_3" />
<item
android:id="@+id/navigation_item_4"
android:icon="@drawable/ic_number_3"
android:title="@string/navigation_item_4" />
<item
android:id="@+id/navigation_item_5"
android:icon="@drawable/ic_number_4"
android:title="@string/navigation_item_5" />
</menu>
</item>
</group>
custom_row.xml :
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_superhero"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="4dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="4dp"
android:text="Superhero"
android:textColor="@color/colorTextSecondary">
Upvotes: 3
Views: 9700
Reputation: 131
You can use this code, but remember you need use adapter to populate your ListView
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/nav_header_main" />
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:textColor="#424242"/>
</LinearLayout>
</android.support.design.widget.NavigationView>
You have this result:
Upvotes: 11
Reputation: 67189
A NavigationView
is nothing more than a wrapper for RecyclerView
that populates the list from a menu XML resource and provides a Material-compliant drawer out of the box. There really isn't any special logic that makes it work in a navigation drawer.
If you want a more dynamic navigation drawer, you should simple replace the NavigationView
in your layout XML with a RecyclerView
and populate it like you would any other RecyclerView
.
Upvotes: 2