对于我的设置,我使用:
我想要的
我想使用 ExpandableListView
作为菜单项的 actionLayout
源。我将把这个菜单项放在一个普通菜单中。我将指向我的导航抽屉以通过 app:menu
属性使用此菜单来填充自身。
我想在常规菜单项旁边使用 ExpandableListView
,并作为操作布局。
我的问题
ExpandableListView
显示在导航抽屉中,但不正确,因为未显示所有项目,而且我无法滚动查看所有项目。
在这张图片中你可以看到只显示了第 3 组,当我点击它时,它似乎展开了,但屏幕上没有显示任何孩子。
我目前的情况
我的activity_main.xml
:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.drawerlayout.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
android:id="@+id/nav_host_fragment" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/navdrawer_header"
app:menu="@menu/navdrawer_menu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
如您所见,在 NavigationView
中,我声明了 app:menu="@menu/navdrawer_menu"
。我想用这个 navdrawer_menu
来完全填充导航抽屉。
我的navdrawer_menu.xml
:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="@+id/group1">
<item
android:id="@+id/item1"
android:title="Item 1" />
<item
android:id="@+id/item2"
android:title="Item 2" />
</group>
<group android:id="@+id/group2">
<item
android:id="@+id/expandable_list_menu"
android:title="Title"
app:actionLayout="@layout/expandable_listview_menu_item_layout" />
</group>
</menu>
我的expandable_listview_menu_item_layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/expandable_list_view_for_actionLayout">
</ExpandableListView>
</LinearLayout>
我的尝试
我尝试更改 XML 中的各种设置,例如布局的方向、各种组件的宽度和高度、以不同的形式声明 showAsAction
、删除其他测试菜单项和组.. .
但似乎没有任何效果。我想我遗漏了一些非常明显的东西。
这不是适配器
也许问题可能出在适配器本身上,但我在主片段中创建了一个按钮,该按钮导航到使用完全相同的代码填充 ExpandableListView
的测试片段,并且它工作得很好。所以我的适配器代码不是问题。
作为参考,以下是该片段如何证明适配器确实有效。
StackOverflow 中的其他答案
我查过其他类似的答案,但他们没有给我想要的。
我知道我可以自己在导航抽屉 XML 中声明一个 ExpandableListView
,并在其中添加自定义视图,并相应地以编程方式设置图标,但使用在 {{ 中声明的简单菜单app:menu
的 1}} 属性对我来说似乎是最好的解决方案。
与布局编辑器一起使用更简单,我可以更轻松地教非程序员。
不过,如果那真的是唯一的办法,我也愿意接受。我最关心的是能够在导航抽屉中在常规菜单项旁边有一个 NavigationView
。我不太清楚这方面的最佳实践是什么。在线搜索只会让我比开始之前更加困惑。希望得到一些帮助。