ExpandableListView效果相当于一个分组的ListView,点击组,会收缩或展开该组下的子元素,如下图: ExpandableListView的用法与ListView和GridView,Gallery 类似,都是通过一个Adapter来显示. 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">
<ExpandableListView android:id="@+id/elv" android:indicatorRight="160dp"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</ExpandableListView>
<!-- indicatorRight 设置那个小图标右边缘与 ExpandableListView左边缘的间距-->
</LinearLayout>
ElvAdapter.java
package com.test;
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class ElvAdapter extends BaseExpandableListAdapter
{
ArrayList objs;
Context context;
ElvAdapter(Context context,ArrayList objs)
{
this.objs=objs;
this.context=context;
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return objs.get(groupPosition).childs.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
//子元素的View
TextView tv=new TextView(context);
tv.setText(objs.get(groupPosition).childs.get(childPosition));
tv.setLayoutParams(new ExpandableListView.LayoutParams(ExpandableListView.LayoutParams.FILL_PARENT,ExpandableListView.LayoutParams.WRAP_CONTENT));
return tv;
}
@Override
public int getChildrenCount(int groupPosition)
{
// TODO Auto-generated method stub
return objs.get(groupPosition).childs.size();
}
@Override
public Object getGroup(int groupPosition)
{
// TODO Auto-generated method stub
return objs.get(groupPosition);
}
@Override
public int getGroupCount()
{
// TODO Auto-generated method stub
return objs.size();
}
@Override
public long getGroupId(int groupPosition)
{
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
//分组的View
TextView tv=new TextView(context);
tv.setText(objs.get(groupPosition).groupName);
ExpandableListView.LayoutParams params=new ExpandableListView.LayoutParams(ExpandableListView.LayoutParams.FILL_PARENT,60);
tv.setLayoutParams(params);
return tv;
}
@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;
}
}
class ElvObject{
String groupName="";
ArrayList childs=new ArrayList();
ElvObject(String groupName,ArrayList childs)
{
this.groupName=groupName;
this.childs=childs;
}
}
注意下面还有一个ElvObject类 主程序AndroidTestActivity.java
package com.test;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
public class AndroidTestActivity extends Activity
{
/** Called when the activity is first created. */
ExpandableListView elv;
ElvAdapter adapter;
ArrayList objs=new ArrayList();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
elv=(ExpandableListView)findViewById(R.id.elv);
adapter=new ElvAdapter(this,objs);
elv.setAdapter(adapter);
ArrayList list=new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
objs.add(new ElvObject("英文",list));
ArrayList list2=new ArrayList();
list2.add("111");
list2.add("222");
list2.add("333");
list2.add("444");
objs.add(new ElvObject("数字",list2));
}
}