【问题标题】:Add a ListView or RecyclerView to new NavigationView将 ListView 或 RecyclerView 添加到新的 NavigationView
【发布时间】:2015-09-07 19:06:29
【问题描述】:

我正在使用来自 Google 的 support library 修订版 22.2.0 中的新 NavigationView。生成使用菜单资源填充的导航抽屉非常有效。

我想知道是否可以将 ListView 或 RecyclerView 添加到导航抽屉,以便可以使用我的自定义适配器代码填充它,这比菜单资源具有更大的灵活性。

这是我当前的 XML:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/content_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/main_toolbar" />

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_drawer_header"
        app:menu="@menu/menu_navigation_drawer" />


</android.support.v4.widget.DrawerLayout>

我应该在我的 XML 中的什么位置添加 ListView 或 RecyclerView?

编辑

按照 Basant 的建议,我在 NavigationView 中嵌套了一个 ListView。你失去了从菜单 res 充气的能力(据我所知),但它成功地完成了我想要它做的事情。标头 XML 不变,只是包含在 XML 中。

新代码:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/content_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/main_toolbar" />

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                android:id="@+id/navigation_drawer_header_include"
                layout="@layout/navigation_drawer_header" />

            <ListView
                android:id="@+id/navigation_drawer_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/navigation_drawer_header_include"/>

        </RelativeLayout>

    </android.support.design.widget.NavigationView>


</android.support.v4.widget.DrawerLayout>

【问题讨论】:

    标签: android listview android-recyclerview navigationview


    【解决方案1】:

    您可以将ListViewRecyclerView 嵌套在NavigationView 中。

    <?xml version="1.0" encoding="utf-8"?>
    
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">
    
        <FrameLayout
            android:id="@+id/content_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <include layout="@layout/main_toolbar" />
    
        </FrameLayout>
    
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"/>
    
            <ListView
                android:id="@+id/menuList"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    </android.support.v4.widget.DrawerLayout>
    

    注意:请记住,如果您在其中使用 ListView,则不能使用 NavigationView 的标题。您必须使用要添加的 ListView 的标题视图。不要忘记删除 app:menuapp:header 字段。

    【讨论】:

    • 您好,我已经尝试过了,但是导航抽屉在导航项不再可点击的意义上中断了。刚刚看到你的注释编辑,我现在就试试。
    • 我认为在这种情况下您不应该使用 NavigationMenu。如果需要,只需使用 ListView 或更复杂的布局来代替 NavigationMenu。这将为您提供更大的灵活性。
    • 我想使用 NavigationView 的原因是由于内置的​​稀松布处理给它更多的材料外观,我已经应用了你的建议,它似乎有效。我将更新我的帖子以显示新代码。
    • 在此处查看接受的答案:stackoverflow.com/questions/26745300/…
    • 我以前使用过它,但新的 NavigationView 内置了它,我更喜欢使用新视图,因为它是库的一部分。
    【解决方案2】:

    如果你想在 NavigationView 中添加视图,你可以这样做。这样,您就没有限制使用 ListView 在 NavigtionView 上添加 Header。

     <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="false"
    
             >
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <include layout="@layout/nav_header_main"
                    android:id="@+id/my"
                    />
                <ListView
    
                    android:layout_weight="7"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:id="@+id/list_view_inside_nav"></ListView>
            </LinearLayout>
        </android.support.design.widget.NavigationView>
    
    </android.support.v4.widget.DrawerLayout>
    

    看起来像这样

    【讨论】:

    • 这不会像导航视图那样滚动标题视图
    • 是的,您可以将它们放入垂直方向的线性布局中,然后使用参数 fillViewPort = true 放入滚动视图中
    • 它尝试过,但 AFAIK(记住)每当我打开包含回收器视图而不是列表视图的导航抽屉时,视图不显示标题,而是向下滚动到回收器视图。 @死鱼
    • @Shubham 让你在导航视图内的回收器视图上方添加标题
    • 我使用了类似的设计模式,但滚动视图中的回收器视图,但我的布局滚动滞后
    【解决方案3】:

    回应 Shubham 的评论

    这不会像导航视图那样滚动标题视图

    我通过将LinearLayout 放入NestedScrollView 中解决了这个问题。现在它随着标题正确滚动。

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <include layout="@layout/nav_header_main" />
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/nav_list"
                android:layout_width="match_parent"
                android:layout_height="@dimen/weight_based_height"
                android:layout_weight="1"
                android:nestedScrollingEnabled="false"/>
        </LinearLayout>
    
    </android.support.v4.widget.NestedScrollView>
    

    【讨论】:

      【解决方案4】:

      试试这个方法

      <android.support.design.widget.NavigationView
      android:id="@+id/navigation"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:fitsSystemWindows="true">
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
      
          <include layout="@layout/nav_header" />
      
          <ListView
              android:id="@+id/lst_menu_items"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="1" />
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-13
        • 1970-01-01
        • 2015-09-26
        • 2016-12-15
        • 2019-02-10
        • 2018-09-18
        相关资源
        最近更新 更多