实施滚动导航抽屉的最好方式 [英] Best way of implementing a scrolling navigation drawer
问题描述
我已经加入了导航抽屉我的应用程序之一,我开始怀疑它是否会更好地从使用的ListView
切换到多个的TextView
S为抽屉式导航栏列表项。纵观<一个href=\"http://www.google.com/design/spec/patterns/navigation-drawer.html#navigation-drawer-content\">Google在导航抽屉内容(特别是关于滚动一节)设计指南,我注意到,它可能看起来多漂亮的TextView
秒。
目前,我使用的是的ListView
和的ImageView
在我的抽屉式导航(它看起来有点像<一href=\"http://material-design.storage.googleapis.com/publish/material_v_3/material_ext_publish/0Bx4BSt6jniD7LWFrRENhVVJrYzQ/patterns_navdrawer_selection1.png\">this.然而,当我在我的抽屉式导航滚动(我这样做是通过打开我的设备景观在目前还没有在我的名单足够的项目还),仅的ListView
卷轴,和的ImageView
保持原样。我希望它能够scoll更像<一个href=\"http://material-design.storage.googleapis.com/publish/material_v_3/material_ext_publish/0Bx4BSt6jniD7MFMyT2ZxcTZXb3c/patterns_navdrawer_scrolling2.png\">this,其中的ImageView
也滚动与的ListView
。
此外,我发现我的的ListView
在我的抽屉式导航不具备的连锁反应<一个href=\"http://material-design.storage.googleapis.com/publish/material_v_3/material_ext_publish/0Bx4BSt6jniD7LWFrRENhVVJrYzQ/patterns_navdrawer_selection1.png\">as此图像中显示虽然其他的ListView
在我的其他活动
和片段
就做。
什么是我所面临的问题,我怎么能去解决这些?
更新:
在谷歌的I / O应用(2014),似乎有一个的LinearLayout
在的<一个底部href=\"https://github.com/google/iosched/blob/master/android/src/main/res/layout/navdrawer.xml\">navigation抽屉布局我认为这是负责显示的产品清单。可能有人解释这是如何将工作?
仅在ListView卷轴,和ImageView的保持,因为它是
块引用>这听起来像你的抽屉里包含一个
的ImageView
上方,然后一个的ListView
如下。有了这个配置只在的ListView
将滚动(因为它是那滚动的唯一视图)。您需要添加
的ImageView
作为标题它总是在列表的开头。作为一个评论所说,做listView.addHeaderView
。
似乎有在导航的底部的LinearLayout
抽屉里的布局我认为这是负责项目列表
图所示。可能有人解释这是如何将工作?
块引用>他们用
的LinearLayout
作为一个容器来容纳所有的的TextView
取值:私人无效createNavDrawerItems(){
mDrawerItemsListContainer =(ViewGroup中)findViewById(R.id.navdrawer_items_list);
...
INT I = 0;
对于(INT的itemId:mNavDrawerItems){
mNavDrawerItemViews [I] = makeNavDrawerItem(的itemId,mDrawerItemsListContainer);
mDrawerItemsListContainer.addView(mNavDrawerItemViews [I]);
++我;
}
}我相信他们使用
的LinearLayout
和编程膨胀所有项目的目的就是为了能够使用分隔符项容易:私人浏览makeNavDrawerItem(最终诠释的itemId,容器的ViewGroup){
...
如果(==的itemId NAVDRAWER_ITEM_SEPARATOR){
layoutToInflate = R.layout.navdrawer_separator;
}否则如果(==的itemId NAVDRAWER_ITEM_SEPARATOR_SPECIAL){
layoutToInflate = R.layout.navdrawer_separator;
}其他{
layoutToInflate = R.layout.navdrawer_item;
}
...
返回视图。
}在
的ListView
你必须创建一个单独的项目类型,使用的分隔符的布局存在,这有可能得到更多的繁琐。乍一看,然而,这code似乎只是重新发明轮子,因为所有的这是可能的一个
的ListView
。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 multipleTextView
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 multipleTextView
s.At the moment, I am using a
ListView
andImageView
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 theListView
scrolls, and theImageView
stays as it is. I want it to be able to scoll more like this, where theImageView
is also scrolled with theListView
.Additionally, I found that my
ListView
in my navigation drawer does not have the ripple effects as shown in this image although otherListView
s in my otherActivity
s andFragment
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?解决方案only the ListView scrolls, and the ImageView stays as it is
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
.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?
They use the
LinearLayout
as a container to hold all theTextView
s:private void createNavDrawerItems() { mDrawerItemsListContainer = (ViewGroup) findViewById(R.id.navdrawer_items_list); ... int i = 0; for (int itemId : mNavDrawerItems) { mNavDrawerItemViews[i] = makeNavDrawerItem(itemId, mDrawerItemsListContainer); mDrawerItemsListContainer.addView(mNavDrawerItemViews[i]); ++i; } }
I believe the reason they use a
LinearLayout
and inflate all the items programmatically is to be able to use separator items easily:private View makeNavDrawerItem(final int itemId, ViewGroup container) { ... if (itemId == NAVDRAWER_ITEM_SEPARATOR) { layoutToInflate = R.layout.navdrawer_separator; } else if (itemId == NAVDRAWER_ITEM_SEPARATOR_SPECIAL) { layoutToInflate = R.layout.navdrawer_separator; } else { layoutToInflate = R.layout.navdrawer_item; } ... return view; }
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
.这篇关于实施滚动导航抽屉的最好方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!