实施滚动导航抽屉的最好方式 [英] Best way of implementing a scrolling navigation drawer

查看:164
本文介绍了实施滚动导航抽屉的最好方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经加入了导航抽屉我的应用程序之一,我开始怀疑它是否会更好地从使用的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 multiple TextViews 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 TextViews.

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 ListViews in my other Activitys and Fragments 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 a ListView follows. With this configuration only the ListView 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, do listView.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 the TextViews:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆