效果如图:
package com.example.expandablelistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import org.apache.http.MethodNotSupportedException;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private MyExpandableListAdapter mExpandableListAdapter;
private List<HashMap<String,Object>> list;
List<String> list_child;
private final String GROUP = "group";
private final String CHILD = "child";
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<HashMap<String,Object>>();
//分组的标签
String[] groups = {"A","B","C","D","E","F"};
Random rand = new Random();
for(int i=0;i<groups.length;i++){
HashMap<String,Object> map = new HashMap<String, Object>();
map.put(GROUP, groups[i]);
list_child = new ArrayList<String>();
for(int j=0;j<5;j++){
list_child.add("联系人"+j);
}
map.put(CHILD, list_child);
list.add(map);
}
edit = (EditText) findViewById(R.id.edit);
ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandablelistview);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addChild(2,2);
}
});
mExpandableListAdapter = new MyExpandableListAdapter(this);
elv.setAdapter(mExpandableListAdapter);
// 演示
// 展开0组
elv.expandGroup(0);
// 收起1组
elv.collapseGroup(1);
// 展开2组
elv.expandGroup(2);
elv.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView arg0, View arg1, int arg2, long arg3) {
// Android默认是返回false。
// 如果返回true,那么,不管是点击已展开的分组还是未展开的分组,都不会相应展开或者收缩的,也就是说这个ExpandableListView将成为一个‘死’的ListView
return false;
}
});
}
private void addChild(int pos,int index){
HashMap<String, Object> map = list.get(pos);
List<String> list = (List<String>) map.get(CHILD);
list.add(index, "xlg");
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter{
private Context mcontext;
private LayoutInflater inflater;
public MyExpandableListAdapter(Context context) {
this.mcontext = context;
inflater = LayoutInflater.from(context);
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
List<String> child = (List<String>) list.get(groupPosition).get(CHILD);
return child.size();
}
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition).get(GROUP);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
List<String> child = (List<String>) list.get(groupPosition).get(CHILD);
return child.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);
TextView text = (TextView) convertView.findViewById(android.R.id.text1);
text.setText(getGroup(groupPosition)+"");
text.setBackgroundColor(Color.RED);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);
TextView text = (TextView) convertView.findViewById(android.R.id.text1);
text.setText(getChild(groupPosition, childPosition)+"");
text.setBackgroundColor(Color.GRAY);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
}
需要的布局文件:
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.expandablelistview.MainActivity" >
<ExpandableListView
android:id="@+id/expandablelistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#e0e0e0">
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"
android:background="@android:color/holo_red_dark"/>
</LinearLayout>
</LinearLayout>