如何在Android导航抽屉中创建下拉菜单?

11

我希望能够创建一个具有可展开/选择项和不可展开项的导航抽屉。对于类似问题,StackOverflow上的共识是使用 ExpandableListView 来解决(但这甚至可能不适用于我的想法)。大多数人所问的是如何像 GMail 应用程序中的标签一样将项目分开,而不是我正在尝试做的...

...基本上是在这里进行了概述 HERE enter image description here

以及此处SIMILARLY(虽然所有项都是下拉列表)。而不是这个答案

2个回答

3

在 DrawerLayout 中使用可扩展的 ExpandableListView,如下所示:

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

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_commentary_behind_nav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top"
        android:text="Frame text" />
</FrameLayout>
<!-- The navigation drawer -->
    <ExpandableListView
        android:id="@+id/left_drawer2"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="multipleChoice"
        android:dividerHeight="0dp"
        />

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

然后在代码中这样初始化:

    private DrawerLayout drawer;
    private ExpandableListView drawerList;
    private CheckBox checkBox;
    private ActionBarDrawerToggle actionBarDrawerToggle;


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawer_layout);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout2);
        drawerList = (ExpandableListView) findViewById(R.id.left_drawer2);
        drawerList.setAdapter(new NewAdapter(this, groupItem, childItem));
    }

1
什么是groupItemchildItem?NewAdapter是什么,它如何处理groupItemchildItem以使一些项可扩展?你的答案不完整。 - Ojonugwa Jude Ochalifu
groupItem是一个数组,其中包含每个组选项的字符串。上面图像中的“TopView 2”。childItem是一个矩阵/哈希映射,其中包含在选择每个组选项后要下拉的项目列表。NewAdapter在填充每个项目的持有者时将groupItem和childItem数据集的值分配给视图。 - AlleyOOP

2
您应该有一个类,其中包含所有已实现的ExpandableListAdapter方法,以及ChildItemsInfo类和GroupItemsInfo类,MainActivity具有组项及其子项的单击侦听器。

...现在更具体地说...

您可以将此放置在MyExpandableListAdapter类内部的getGroupView()中。

    View ind = convertView.findViewById(R.id.group_indicator);
    View ind2 = convertView.findViewById(R.id.group_indicator2);
    if (ind != null)
    {
        ImageView indicator = (ImageView) ind;
        if (getChildrenCount(groupPosition) == 0)
        {
            indicator.setVisibility(View.INVISIBLE);
        }
        else
        {
            indicator.setVisibility(View.VISIBLE);
            int stateSetIndex = (isExpanded ? 1 : 0);

            /*toggles down button to change upwards when list has expanded*/
            if(stateSetIndex == 1){
                ind.setVisibility(View.INVISIBLE);
                ind2.setVisibility(View.VISIBLE);
                Drawable drawable = indicator.getDrawable();
                drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
            }
            else if(stateSetIndex == 0){
                ind.setVisibility(View.VISIBLE);
                ind2.setVisibility(View.INVISIBLE);
                Drawable drawable = indicator.getDrawable();
                drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
            }
        }
    }

至于布局视图,这是我的group_items.xml文件的外观

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/group_heading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:textSize="15sp"
    android:textStyle="bold"/>

<ImageView
    android:id="@+id/group_indicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/arrow_down_float"
    android:layout_alignParentRight="true"
    android:paddingRight="20dp"
    android:paddingTop="20dp"/>

<ImageView
    android:id="@+id/group_indicator2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/arrow_up_float"
    android:layout_alignParentRight="true"
    android:visibility="gone"
    android:paddingRight="20dp"
    android:paddingTop="20dp"/>

您是否觉得不够清晰?随时发表评论。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接