您的位置:首页 > 移动开发 > Android开发

Android ExpandableListActivity的简单介绍及小例子

2014-12-12 15:18 337 查看
  Android中常常要用到ListView,但也经常要用到ExpandableListView,ListView是显示列表,而ExpandableListView显示的是分类的列表;

  下面用一个例子来说明:







还可以点击触发事件;





代码如下:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xiaozhang.listactivitytest.MainActivity" >

<ExpandableListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" />

</RelativeLayout>


第一层列表group.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingLeft="12dp"
android:paddingRight="12dp" >

<TextView
android:id="@+id/groupTo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingLeft="30dp"
android:paddingTop="10dp"
android:shadowColor="#40000000"
android:shadowDx="0"
android:shadowDy="8"
android:shadowRadius="1"
android:textSize="16sp"
android:textStyle="bold" />

</RelativeLayout>


第二层列表child.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingLeft="12dp"
android:paddingRight="12dp" >

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/icon"
android:paddingTop="12dp" />

<TextView
android:id="@+id/childTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="60dp"
android:paddingTop="10dp"
android:textSize="16sp" />

</RelativeLayout>


MainActivity.java

package com.xiaozhang.listactivitytest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;

public class MainActivity extends ExpandableListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

List<Map<String, String>> groups = new ArrayList<Map<String, String>>();

// 第一层列表数据
Map<String, String> group1 = new HashMap<String, String>();
Map<String, String> group2 = new HashMap<String, String>();
Map<String, String> group3 = new HashMap<String, String>();
Map<String, String> group4 = new HashMap<String, String>();
group1.put("group", "湘北高中");
group2.put("group", "岭南高中");
group3.put("group", "翔阳高中");
group4.put("group", "海南高中");

groups.add(group1);
groups.add(group2);
groups.add(group3);
groups.add(group4);

// 第二层列表数据
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();

// 第二层列表的第一个子列表数据
List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
Map<String, String> child1Data1 = new HashMap<String, String>();
child1Data1.put("child", "樱木花道");
Map<String, String> child1Data2 = new HashMap<String, String>();
child1Data2.put("child", "流川枫");
Map<String, String> child1Data3 = new HashMap<String, String>();
child1Data3.put("child", "宫城良田");
Map<String, String> child1Data4 = new HashMap<String, String>();
child1Data4.put("child", "三井寿");
child1.add(child1Data1);
child1.add(child1Data2);
child1.add(child1Data3);
child1.add(child1Data4);

// 第二层列表的第二个子列表数据
List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
Map<String, String> child2Data1 = new HashMap<String, String>();
child2Data1.put("child", "仙道彰");
Map<String, String> child2Data2 = new HashMap<String, String>();
child2Data2.put("child", "鱼住纯");
Map<String, String> child2Data3 = new HashMap<String, String>();
child2Data3.put("child", "福田吉兆");
child2.add(child2Data1);
child2.add(child2Data2);
child2.add(child2Data3);

// 第二层列表的第三个子列表数据
List<Map<String, String>> child3 = new ArrayList<Map<String, String>>();
Map<String, String> child3Data1 = new HashMap<String, String>();
child3Data1.put("child", "藤真健司");
Map<String, String> child3Data2 = new HashMap<String, String>();
child3Data2.put("child", "花形透");
Map<String, String> child3Data3 = new HashMap<String, String>();
child3Data3.put("child", "长谷川一志");
child3.add(child3Data1);
child3.add(child3Data2);
child3.add(child3Data3);

// 第二层列表的第四个子列表数据
List<Map<String, String>> child4 = new ArrayList<Map<String, String>>();
Map<String, String> child4Data1 = new HashMap<String, String>();
child4Data1.put("child", "牧绅一");
Map<String, String> child4Data2 = new HashMap<String, String>();
child4Data2.put("child", "神宗一郎");
Map<String, String> child4Data3 = new HashMap<String, String>();
child4Data3.put("child", "清田信长");
Map<String, String> child4Data4 = new HashMap<String, String>();
child4Data4.put("child", "高砂一马");
child4.add(child4Data1);
child4.add(child4Data2);
child4.add(child4Data3);
child4.add(child4Data4);

// 把子列表数据放入第二层列表中
childs.add(child1);
childs.add(child2);
childs.add(child3);
childs.add(child4);

SimpleExpandableListAdapter listAdapter = new SimpleExpandableListAdapter(
this, groups, R.layout.group, new String[] { "group" },
new int[] { R.id.groupTo }, childs, R.layout.child,
new String[] { "child" }, new int[] { R.id.childTo });
setListAdapter(listAdapter);

}

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
this,
parent.getItemAtPosition(groupPosition)
+ " "
+ parent.getItemAtPosition(childPosition
+ groupPosition + 1), Toast.LENGTH_LONG).show();

return super.onChildClick(parent, v, groupPosition, childPosition, id);
}
}


注意:

(1)@id/android:list,是系统自带的ID,如果要使用ListActivity或我们使用的ExpandableListActivity,就必须要使用@id/android:list;

  而ListActivity会根据id自动查找ListView的引用;如在 Activity 中使用 setListAdapter(adapter) 时就默认设置到了这个list上。如果按一般控件的写法 <ListView android:id="@+id/myListView" …… />,则需要 findViewById 先得到控件对像,再调用对像的 setListAdapter(adapter);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: