Android: 在 DrawerMenu 中扩展子菜单

Android: expanding Submenu in DrawerMenu

我想在我的 Android 应用程序中实现 Drawermenu 选项:

  1. 简介
  2. 设置
  3. 信息

对于 Drawermenu 的实现有很多教程,而且都非常清楚,但是我找不到任何关于在我的 Reddit 应用程序中添加与此类似的子菜单的内容:

点击 "My Subscriptions" 我的订阅列表会展开。

这真的有那么难实现吗? 如有任何想法或建议,我们将不胜感激

我的抽屉是这样设置的:

布局XML:

<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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="#ffeeeeee"/>
</android.support.v4.widget.DrawerLayout>

主要活动:

package com.example.simon.drawtest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    private ListView mDrawerList;
    private ArrayAdapter<String> mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        addDrawerItems();
    }
    private void addDrawerItems() {
        String[] osArray = {"Profile", "Settings", "Info"};
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
        mDrawerList.setAdapter(mAdapter);
    }
}

谢谢大家!

您需要将 ListView 替换为 ExpandableListView. This is a good tutorial 才能开始。