可展開式清單

main.xml:定義Activity的介面佈局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<ExpandableListView

android:id="@+id/mExpandableListView"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

</LinearLayout>

group.xml:定義一級清單的佈局方式

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal" >

<TextView

android:id="@+id/group_tv"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="10px"

android:paddingLeft="60px"

android:paddingTop="10px"

android:textSize="26sp" />

</LinearLayout>

child.xml:定義二級清單的佈局方式:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

<ImageView

android:id="@+id/child_iv"

android:layout_width="70px"

android:layout_height="70px"

android:layout_gravity="center_vertical"

android:paddingBottom="5px"

android:paddingLeft="30px"

android:paddingTop="2px"

android:src="@drawable/icon" />

<TextView

android:id="@+id/child_tv"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_gravity="center_vertical"

android:paddingBottom="5px"

android:paddingLeft="30px"

android:paddingTop="10px"

android:textSize="30sp" />

</LinearLayout>

Activity代碼:

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseExpandableListAdapter;

import android.widget.ExpandableListView;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;

public class TestExpandableListView extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ExpandableListView elv = (ExpandableListView) findViewById(R.id.mExpandableListView);

// 準備一級清單中顯示的資料:2個一級清單,分別顯示"group1"和"group2"

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

Map<String, String> group1 = new HashMap<String, String>();

group1.put("group", "group1");

Map<String, String> group2 = new HashMap<String, String>();

group2.put("group", "group2");

groups.add(group1);

groups.add(group2);

// 準備第一個一級清單中的二級清單資料:兩個二級清單,分別顯示"childData1"和"childData2"

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

Map<String, String> child1Data1 = new HashMap<String, String>();

child1Data1.put("child", "child1Data1");

Map<String, String> child1Data2 = new HashMap<String, String>();

child1Data2.put("child", "child1Data2");

child1.add(child1Data1);

child1.add(child1Data2);

// 準備第二個一級清單中的二級清單資料:一個二級清單,顯示"child2Data1"

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

Map<String, String> child2Data1 = new HashMap<String, String>();

child2Data1.put("child", "child2Data1");

child2.add(child2Data1);

// 用一個list物件保存所有的二級清單資料

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

childs.add(child1);

childs.add(child2);

ExpandableAdapter viewAdapter = new ExpandableAdapter(this, groups,

childs);

elv.setAdapter(viewAdapter);

}

// 自訂的ExpandListAdapter

class ExpandableAdapter extends BaseExpandableListAdapter {

private Context context;

List<Map<String, String>> groups;

List<List<Map<String, String>>> childs;

/*

* 構造函數: 參數1:context物件 參數2:一級清單資料來源 參數3:二級清單資料來源

*/

public ExpandableAdapter(Context context,

List<Map<String, String>> groups,

List<List<Map<String, String>>> childs) {

this.groups = groups;

this.childs = childs;

this.context = context;

}

public Object getChild(int groupPosition, int childPosition) {

return childs.get(groupPosition).get(childPosition);

}

public long getChildId(int groupPosition, int childPosition) {

return childPosition;

}

// 獲取二級清單的View物件

public View getChildView(int groupPosition, int childPosition,

boolean isLastChild, View convertView, ViewGroup parent) {

@SuppressWarnings("unchecked")

String text = ((Map<String, String>) getChild(groupPosition,

childPosition)).get("child");

LayoutInflater layoutInflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// 獲取二級清單對應的佈局檔, 並將其各元素設置相應的屬性

LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(

R.layout.child, null);

TextView tv = (TextView) linearLayout.findViewById(R.id.child_tv);

tv.setText(text);

ImageView imageView = (ImageView) linearLayout

.findViewById(R.id.child_iv);

imageView.setImageResource(R.drawable.icon);

return linearLayout;

}

public int getChildrenCount(int groupPosition) {

return childs.get(groupPosition).size();

}

public Object getGroup(int groupPosition) {

return groups.get(groupPosition);

}

public int getGroupCount() {

return groups.size();

}

public long getGroupId(int groupPosition) {

return groupPosition;

}

// 獲取一級清單View物件

public View getGroupView(int groupPosition, boolean isExpanded,

View convertView, ViewGroup parent) {

String text = groups.get(groupPosition).get("group");

LayoutInflater layoutInflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// 獲取一級清單佈局檔,設置相應元素屬性

LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(

R.layout.group, null);

TextView textView = (TextView) linearLayout

.findViewById(R.id.group_tv);

textView.setText(text);

return linearLayout;

}

public boolean hasStableIds() {

return false;

}

public boolean isChildSelectable(int groupPosition, int childPosition) {

return false;

}

}

}

資料來源:http://fecbob.pixnet.net/blog/post/35999655-%5Bandroid%5D-expandablelistview%E7%B0%A1%E5%96%AE%E7%94%A8%E6%B3%95

只展開一組

ExpandableListView elv = (ExpandableListView) findViewById(R.id.mExpandableListView);

elv.setOnGroupExpandListener(new OnGroupExpandListener() {

@Override

public void onGroupExpand(int groupPosition) {

for (int i = 0; i < GroupCount; i++) {

if (groupPosition != i) {

elv.collapseGroup(i);

}

}

}

});

資料來源:http://blog.csdn.net/android_007/article/details/7321245