以下是expandexample.java. 拷贝后即可运行:
package com.xx.xx.xxxxx
import java.util.ArrayList;
import java.util.List;
import Android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class pdfOutlineList extends Activity {
private ExpandableListView mElv;
private LinearLayout mLayout;
private List<String> mGroupArray;
private List<List<String>> mChildArray;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLayout = new LinearLayout(this);
mElv = new ExpandableListView(this);
mGroupArray = new ArrayList<String>();
mChildArray = new ArrayList<List<String>>();
mGroupArray.add("第一行");
mGroupArray.add("第二行");
List<String> tempArray = new ArrayList<String>();
tempArray.add("第一条");
tempArray.add("第二条");
tempArray.add("第三条");
for(int index = 0; index <mGroupArray.size(); ++index)
{
mChildArray.add(tempArray);
}
mElv.setAdapter(new ExpandableAdapter(this));
mLayout.addView(mElv);
setContentView(mLayout);
}
public class ExpandableAdapter extends BaseExpandableListAdapter
{
Activity activity;
public ExpandableAdapter(Activity a)
{
activity = a;
}
public Object getChild(int groupPosition, int childPosition)
{
return mChildArray.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public int getChildrenCount(int groupPosition)
{
return mChildArray.get(groupPosition).size();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
String string = mChildArray.get(groupPosition).get(childPosition);
return getGenericView(string);
}
public Object getGroup(int groupPosition)
{
return mGroupArray.get(groupPosition);
}
public int getGroupCount()
{
return mGroupArray.size();
}
public long getGroupId(int groupPosition)
{
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
String string = mGroupArray.get(groupPosition);
return getGenericView(string);
}
public TextView getGenericView(String string)
{
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView text = new TextView(activity);
text.setLayoutParams(layoutParams);
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(36, 0, 0, 0);
text.setText(string);
return text;
}
public boolean hasStableIds()
{
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
}