ExpandableListView是android中可以实现下滑子菜单效果的一个控件
在布局文件(layout)目录下创建三个新的 .xml 文件(android xml layout file),为父级菜单列表布局 、子级菜单列表布局 、主布局文件
(我的分别命名为nodes、childs、activity_main)
nodes 和 childs 中拖入 TextView 用来显示内容
activity_main 中拖入控件 ExpendableListView
nodes.xml
<TextView
android:id="@+id/tv_node"
android:layout_width="fill_parent"
android:layout_height="44sp"
android:text="@string/hello_world"
android:textSize="20sp"
android:gravity="left"
android:layout_marginLeft="36sp"
android:layout_marginTop="16sp" />
childs.xml
<TextView
android:id="@+id/tv_node_nick"
android:layout_width="wrap_content"
android:layout_height="54sp"
android:layout_weight="1"
android:text="@string/hello_world"
android:textSize="20sp"
android:gravity="left"
android:layout_marginLeft="40sp"
android:layout_marginTop="17sp"
/>
<TextView
android:id="@+id/tv_node_name"
android:layout_width="wrap_content"
android:layout_height="54sp"
android:layout_weight="1"
android:text="@string/hello_world"
android:textSize="18sp"
android:gravity="right"
android:layout_marginRight="5sp"
android:layout_marginTop="17sp" />
activity_main.xml
<ExpandableListView
android:id="@+id/explv_qq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ExpandableListView>
在java 文件中写入代码用于和布局文件交互起来
java 代码块
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建父容器
List<Map<String, String>> nodes = new ArrayList<Map<String,String>>();
Map<String, String> map1 = new HashMap<String, String>();
map1.put("node", "艾欧尼亚");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("node", "洛克萨斯");
Map<String, String> map3 = new HashMap<String, String>();
map3.put("node", "德玛西亚");
nodes.add(map1);
nodes.add(map2);
nodes.add(map3);
// 创建子容器
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();
List<Map<String, String>> listCh1 = new ArrayList<Map<String,String>>();
Map<String, String> child1_1 = new HashMap<String, String>();
child1_1.put("ch_nick", "九尾妖狐");
child1_1.put("name", "阿狸");
Map<String, String> child1_2 = new HashMap<String, String>();
child1_2.put("ch_nick", "刀锋意志");
child1_2.put("name", "艾瑞莉亚");
Map<String, String> child1_3 = new HashMap<String, String>();
child1_3.put("ch_nick", "琴瑟仙女");
child1_3.put("name", "娑娜");
Map<String, String> child1_4 = new HashMap<String, String>();
child1_4.put("ch_nick", "天启者");
child1_4.put("name", "卡尔玛");
listCh1.add(child1_1);
listCh1.add(child1_2);
listCh1.add(child1_3);
listCh1.add(child1_4);
List<Map<String, String>> listCh2 = new ArrayList<Map<String,String>>();
Map<String, String> child2_1 = new HashMap<String, String>();
child2_1.put("ch_nick", "放逐之刃");
child2_1.put("name", "锐雯");
Map<String, String> child2_2 = new HashMap<String, String>();
child2_2.put("ch_nick", "刀锋之影");
child2_2.put("name", "泰隆");
Map<String, String> child2_3 = new HashMap<String, String>();
child2_3.put("ch_nick", "盲僧");
child2_3.put("name", "李青");
Map<String, String> child2_4 = new HashMap<String, String>();
child2_4.put("ch_nick", "魔蛇之拥");
child2_4.put("name", "卡西奥佩娅");
listCh2.add(child2_1);
listCh2.add(child2_2);
listCh2.add(child2_3);
listCh2.add(child2_4);
List<Map<String, String>> listCh3 = new ArrayList<Map<String,String>>();
Map<String, String> child3_1 = new HashMap<String, String>();
child3_1.put("ch_nick", "龙血武姬 ");
child3_1.put("name", "希瓦娜");
Map<String, String> child3_2 = new HashMap<String, String>();
child3_2.put("ch_nick", "暗夜猎手");
child3_2.put("name", "薇恩");
Map<String, String> child3_3 = new HashMap<String, String>();
child3_3.put("ch_nick", "光辉女郎");
child3_3.put("name", "拉克丝");
Map<String, String> child3_4 = new HashMap<String, String>();
child3_4.put("ch_nick", "哨兵之殇");
child3_4.put("name", "加里奥");
listCh3.add(child3_1);
listCh3.add(child3_2);
listCh3.add(child3_3);
listCh3.add(child3_4);
childs.add(listCh1);
childs.add(listCh2);
childs.add(listCh3);
// 创建适配器
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this,
nodes, R.layout.nodes, new String[]{"node"}, new int[]{R.id.tv_node},
childs, R.layout.childs, new String[]{"ch_nick","name"}, new int[]{R.id.tv_node_nick,R.id.tv_node_name});
// 找到控件
ExpandableListView listView = (ExpandableListView) findViewById(R.id.explv_qq);
// 绑定适配器
listView.setAdapter(adapter);
}
注意:子容器的格式是 List<List<Map<String, String>>>