参考链接:http://blog.csdn.net/way_ping_li/article/details/7995552
效果图:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/elv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
</LinearLayout>
list_group.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" >
<TextView
android:id="@+id/groupText"
android:text="group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
list_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/childImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/childText"
android:text="child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.example.test;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandableAdapter extends BaseExpandableListAdapter {
private LayoutInflater layoutInflater;
private List<String> group;
private List<List<String>> child;
public ExpandableAdapter(Context context, List<String> group,
List<List<String>> child) {
layoutInflater = LayoutInflater.from(context);
this.group = group;
this.child = child;
}
@Override
public int getGroupCount() {
return group.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return child.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return group.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return child.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//表明大組和小组id是否稳定的更改底层数据
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View view = convertView;
// if(view == null) {
view = layoutInflater.inflate(R.layout.list_group, null);
TextView textView = (TextView) view.findViewById(R.id.groupText);
textView.setText(group.get(groupPosition));
// }
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View view = convertView;
// if(view == null) {
view = layoutInflater.inflate(R.layout.list_child, null);
TextView textView = (TextView) view.findViewById(R.id.childText);
textView.setText(child.get(groupPosition).get(childPosition));
// }
return view;
}
//得到小组成员是否被选择
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
package com.example.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private ExpandableListView elv;
private ExpandableAdapter ea;
private List<String> group;
private List<List<String>> child;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
group = new ArrayList<String>();
child = new ArrayList<List<String>>();
group.add("A");
group.add("B");
group.add("C");
group.add("D");
group.add("E");
for (int i = 0; i < group.size(); i++)
{
List<String> tempList = new ArrayList<String>();
for (int j = 0; j < 3; j++) {
tempList.add(group.get(i) + j);
}
child.add(tempList);
}
elv = (ExpandableListView) findViewById(R.id.elv);
ea = new ExpandableAdapter(MainActivity.this, group, child);
elv.setAdapter(ea);
elv.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this,"你点击了" + ea.getChild(groupPosition, childPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
}
}