废话少说,先看效果:
布局文件如下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 我们会自己定义listview的显示方式(在另外一个布局文件里边)不用默认的方式
如果自定义listview的显示方式这里这个android:id="@id/android:list" 必须这样写 -->
<!-- android:drawSelectOnTop="false"此属性用来设置listview上的背景颜色会不会
挡住(覆盖)内容 , 如果这是为false就表示不会覆盖掉 -->
<ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
prjoinfo_group_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff5689"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tableRow_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dip"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip" >
<TextView
android:id="@+id/textView_groupTitle"
android:layout_width="130dp"
android:layout_height="match_parent"
android:background="#00ff00"
android:gravity="left"
android:paddingLeft="30dp"
android:singleLine="false"
android:textStyle="bold" />
<TextView
android:id="@+id/textView_groupSecondTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:background="#00ff00"
android:gravity="left"
android:singleLine="false"
android:textStyle="bold" />
</TableRow>
</TableLayout>
projinfo_child_item.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="wrap_content" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:background="#ff5689"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dip"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip" >
<TextView
android:id="@+id/textView_childTitle"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#00ffff"
android:gravity="left"
android:singleLine="false"
android:textStyle="bold" />
<TextView
android:id="@+id/textView_childSecondTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:background="#00ffff"
android:gravity="left"
android:singleLine="false"
android:textStyle="bold" />
</TableRow>
</TableLayout>
</LinearLayout>
最关键是上代码了:
package com.example.expandablelistviewtest;
import java.util.ArrayList;
import java.util.List;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class MainActivity extends ExpandableListActivity
{
List<String> group; // 组列表
List<List<String>> child; // 子列表
ContactsInfoAdapter adapter; // 数据适配器
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置为无标题
setContentView(R.layout.activity_main);
// getExpandableListView().setBackgroundResource(R.drawable.default_bg);
initializeData();
//getExpandableListView().setGroupIndicator(null);//不显示上下箭头
getExpandableListView().setAdapter(new ContactsInfoAdapter());
getExpandableListView().setCacheColorHint(0); // 设置拖动列表的时候防止出现黑色背景
}
/**
* 初始化组、子列表数据
*/
private void initializeData()
{
group = new ArrayList<String>();
child = new ArrayList<List<String>>();
addInfo("Andy", new String[]
{ "male", "138123***", "GuangZhou" });
addInfo("Fairy", new String[]
{ "female", "138123***", "GuangZhou" });
addInfo("Jerry", new String[]
{ "male", "138123***", "ShenZhen" });
addInfo("Tom", new String[]
{ "female", "138123***", "ShangHai" });
addInfo("Bill", new String[]
{ "male", "138231***", "ZhanJiang" });
}
/**
* 模拟给组、子列表添加数据
*
* @param g
* -group
* @param c
* -child
*/
private void addInfo(String g, String[] c)
{
group.add(g);
List<String> childitem = new ArrayList<String>();
for (int i = 0; i < c.length; i++)
{
childitem.add(c[i]);
}
child.add(childitem);
}
class ContactsInfoAdapter extends BaseExpandableListAdapter
{
// -----------------Child----------------//
@Override
public Object getChild(int groupPosition, int childPosition)
{
return child.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition)
{
return child.get(groupPosition).size();
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
String string = child.get(groupPosition).get(childPosition);
return getChildView(string);
}
// ----------------Group----------------//
@Override
public Object getGroup(int groupPosition)
{
return group.get(groupPosition);
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public int getGroupCount()
{
return group.size();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
String string = group.get(groupPosition);
return getGenericView(string);
}
// 创建组/子视图
public View getGenericView(String s)
{
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View view = inflater.inflate(R.layout.prjoinfo_group_item, null);
((TextView) view.findViewById(R.id.textView_groupTitle)).setText("标的企业评估核准或备案情况");
((TextView) view.findViewById(R.id.textView_groupSecondTitle)).setText(s);
return view;
}
public View getChildView(String s)
{
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View view = inflater.inflate(R.layout.projinfo_child_item, null);
((TextView) view.findViewById(R.id.textView_childTitle)).setText("标的企业评估核准或备案情况");
((TextView) view.findViewById(R.id.textView_childSecondTitle)).setText(s);
return view;
}
@Override
public boolean hasStableIds()
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return true;
}
}
}