如何在导航抽屉内集成可扩展列表视图?
How to integrate expandable list view inside navigation drawer?
我为我的可扩展列表视图创建了一个项目。此外,在我的另一个主要项目中,我创建了一个导航抽屉。一切正常,因为它是自己的。不幸的是,我想在我的导航抽屉中显示这个可扩展列表视图,但我不知道从哪里开始。我觉得应该有一些小的补充(已经有适配器和东西)但说真的我不知道如何在抽屉内实现布局。我搜索了这个主题,所有示例和答案对我来说都不太清楚。
你能给我指明正确的方向吗?
制作自定义布局以使用自定义导航视图,如下面 (layout_nav_bar.xml):-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_white"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<include
android:id="@+id/nav_header"
layout="@layout/nav_header_main"
/>
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:overScrollMode="never"
android:background="@android:color/white"
android:divider="@null"
android:groupIndicator="@null" />
</LinearLayout>
将此添加到您的 DrawerLayout
<include
android:id="@+id/nav_view"
layout="@layout/layout_nav_bar"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
您可以通过在 NavigationDrawer
中添加 ExpandableListView
来实现,如下所示:
您可以使用自定义 ListView 创建它。
见下方代码activity_navigation_view.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<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="true">
<ExpandableListView
android:id="@+id/navigationmenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="192dp"
android:background="@android:color/white">
</ExpandableListView>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
并且只需为它创建适配器并像正常使用它一样使用它 ExpandableListView
。
我为我的可扩展列表视图创建了一个项目。此外,在我的另一个主要项目中,我创建了一个导航抽屉。一切正常,因为它是自己的。不幸的是,我想在我的导航抽屉中显示这个可扩展列表视图,但我不知道从哪里开始。我觉得应该有一些小的补充(已经有适配器和东西)但说真的我不知道如何在抽屉内实现布局。我搜索了这个主题,所有示例和答案对我来说都不太清楚。
你能给我指明正确的方向吗?
制作自定义布局以使用自定义导航视图,如下面 (layout_nav_bar.xml):-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_white"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<include
android:id="@+id/nav_header"
layout="@layout/nav_header_main"
/>
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:overScrollMode="never"
android:background="@android:color/white"
android:divider="@null"
android:groupIndicator="@null" />
</LinearLayout>
将此添加到您的 DrawerLayout
<include
android:id="@+id/nav_view"
layout="@layout/layout_nav_bar"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
您可以通过在 NavigationDrawer
中添加 ExpandableListView
来实现,如下所示:
您可以使用自定义 ListView 创建它。
见下方代码activity_navigation_view.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<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="true">
<ExpandableListView
android:id="@+id/navigationmenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="192dp"
android:background="@android:color/white">
</ExpandableListView>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
并且只需为它创建适配器并像正常使用它一样使用它 ExpandableListView
。