添加ListView或RecyclerView新NavigationView [英] Add a ListView or RecyclerView to new NavigationView

查看:429
本文介绍了添加ListView或RecyclerView新NavigationView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是新的 NavigationView 从版本由谷歌支持库。它的工作原理完全正常生成使用菜单资源填充导航抽屉里。

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

下面是我当前的XML:

 <?XML版本=1.0编码=UTF-8&GT?;< android.support.v4.widget.DrawerLayout
    机器人:ID =@ + ID / drawer_layout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:fitsSystemWindows =真
    工具:上下文=MainActivity。>    <的FrameLayout
        机器人:ID =@ + ID / content_view
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>        <包括布局=@布局/ main_toolbar/>    < /&的FrameLayout GT;    < android.support.design.widget.NavigationView
        机器人:ID =@ + ID / navigation_view
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:layout_gravity =开始
        应用:headerLayout =@布局/ navigation_drawer_header
        应用:菜单=@菜单/ menu_navigation_drawer/>
< /android.support.v4.widget.DrawerLayout>

凡在我的XML将我加入的ListView或RecyclerView?

修改

根据巴桑特的建议下,我嵌套一个ListView到NavigationView。你失去从菜单水库膨胀的能力(据我所知),但它在我想要它做成功。头XML是不变的,它只是纳入XML。

新的code:

 <?XML版本=1.0编码=UTF-8&GT?;< android.support.v4.widget.DrawerLayout
    机器人:ID =@ + ID / drawer_layout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:fitsSystemWindows =真
    工具:上下文=MainActivity。>    <的FrameLayout
        机器人:ID =@ + ID / content_view
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>        <包括布局=@布局/ main_toolbar/>    < /&的FrameLayout GT;    < android.support.design.widget.NavigationView
        机器人:ID =@ + ID / navigation_view
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:layout_gravity =开始>        < RelativeLayout的
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent>            <包括
                机器人:ID =@ + ID / navigation_drawer_header_include
                布局=@布局/ navigation_drawer_header/>            < ListView控件
                机器人:ID =@ + ID / navigation_drawer_list
                机器人:layout_width =match_parent
                机器人:layout_height =match_parent
                机器人:layout_below =@ ID / navigation_drawer_header_include/>        < / RelativeLayout的>    < /android.support.design.widget.NavigationView>
< /android.support.v4.widget.DrawerLayout>


解决方案

你可以窝在的ListView RecyclerView 里面的 NavigationView

 <?XML版本=1.0编码=UTF-8&GT?;< android.support.v4.widget.DrawerLayout
    机器人:ID =@ + ID / drawer_layout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:fitsSystemWindows =真
    工具:上下文=MainActivity。>    <的FrameLayout
        机器人:ID =@ + ID / content_view
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>        <包括布局=@布局/ main_toolbar/>    < /&的FrameLayout GT;    < android.support.design.widget.NavigationView
        机器人:ID =@ + ID / navigation_view
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:layout_gravity =开始/>        < ListView控件
            机器人:ID =@ + ID /菜单列表
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent/>
< /android.support.v4.widget.DrawerLayout>

注意:请记住,如果你使用使用它里面一个ListView,你不能使用NavigationView的标题。你将不得不使用您添加​​的ListView控件的标题视图。不要忘记删除应用程序:菜单应用程序:标题字段

I am using the new NavigationView from revision 22.2.0 of the support library by Google. It works perfectly fine to generate a navigation drawer populated using a menu res.

I was wondering is it possible to add a ListView or RecyclerView to the navigation drawer so that it can be populated using my custom adapter code, which allows for far greater flexibility than menu resources.

Here is my current 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>

Where in my XML would I add the ListView or RecyclerView?

EDIT

As per Basant's suggestion, I nested a ListView into the NavigationView. You lose the ability to inflate from a menu res (as far as I know) but it succeeds in what I want it to do. The header XML is unchanged, it is just included into XML.

New code:

<?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>

解决方案

You can just nest the ListView or RecyclerView inside the 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>

NOTE: Keep in mind that if you use use a ListView inside it, you can't use the NavigationView's header. You will have to use the header view of the ListView that you are adding. Don't forget to remove the app:menu and app:header fields.

这篇关于添加ListView或RecyclerView新NavigationView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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