I have been adding a navigation drawer to one of my apps, and I started to wonder whether or not it would be better to switch from using a ListView
to multiple TextView
s for the navigation drawer list items. Looking at the Google Design Guidelines on Navigation Drawer content (specifically the section on 'Scrolling'), I noticed that it may look nicer with multiple TextView
s.
At the moment, I am using a ListView
and ImageView
in my navigation drawer (it looks a little like this. However, when I scroll in my navigation drawer (I do this by turning my device landscape as there are not enough items in my list yet), only the ListView
scrolls, and the ImageView
stays as it is. I want it to be able to scoll more like this, where the ImageView
is also scrolled with the ListView
.
Additionally, I found that my ListView
in my navigation drawer does not have the ripple effects as shown in this image although other ListView
s in my other Activity
s and Fragment
s do.
What are the issues I am facing and how could I go about resolving these?
Update:
In Google's I/O App (2014), there seems to be a LinearLayout
at the bottom of the navigation drawer layout which I think is responsible for the list of items shown. Could someone explain how this would work?
As of 29th May 2015 (after Google I/O 2015), you can use the Android Design Support Library to add a
NavigationView
to your app(s). The Android Developer Blogspot article states the following:It sounds like your drawer contains an
ImageView
at the top and then aListView
follows. With this configuration only theListView
will scroll (because it's the only view that's scrollable).You need to add the
ImageView
as a header which is always at the beginning of the list. As one of the comments suggested, dolistView.addHeaderView
.They use the
LinearLayout
as a container to hold all theTextView
s:I believe the reason they use a
LinearLayout
and inflate all the items programmatically is to be able to use separator items easily:In a
ListView
you'd have to create a separate item type and use the divider's layout there, which could possibly get more cumbersome.At first glance, however, this code just seems to be re-inventing the wheel as all of this is possible with a
ListView
.Implementing scrollable Navigation Drawer using android.support.v4.widget.DrawerLayout and NavigationView could be even simpler than it is described at: http://android-developers.blogspot.ru/2015/05/android-design-support-library.html
That article suggests adding each element of your application's Navigation Drawer as a Menu Item. This is cool and definitely a way to go for most of developers. But what if you already has a Navigation Drawer implemented inside e.g. Linear Layout?
It appears that you can easily make your old good layout scrollable: just set it as a "app:headerLayout" of the NavigationView. No more changes are needed! So, in a final solution you will have:
A layout of your Activity, similar to the above blog post, but without an "app:menu="@menu/drawer" attribute e.g. this:
And a layout for all your old Drawer content in the "drawer_header.xml" file, migrated without any changes to this scrollable Drawer, E.g. this:
For full working example see this activity layout: https://github.com/andstatus/andstatus/blob/master/app/src/main/res/layout/timeline.xml and this commit, where I migrated to a scrollable Navigation Drawer: https://github.com/andstatus/andstatus/commit/a80b299de714bdd65cacb138ffb31adc3ea23a98