您可以编写自定义适配器。您可以覆盖 getView、getItemViewType 和 getViewTypeCount 并为项目组标题设置不同的布局。就像是:
public class TestArrayAdapter extends ArrayAdapter<String> {
LayoutInflater mInflater;
private static final int
GROUP_START = 0,
ITEM = 1,
COUNT = 2;
public TestArrayAdapter(Context context, String[] objects) {
super(context, 0, objects);
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (getItemViewType(position) == GROUP_START) {
v = mInflater.inflate(R.layout.group_start, parent, false);
((TextView)v.findViewById(R.id.item_group_header)).setText(
Character.toString(getItem(position).charAt(0)));
} else {
v = mInflater.inflate(R.layout.item, parent, false);
}
((TextView)v.findViewById(R.id.text)).setText(getItem(position));
return v;
}
@Override
public int getItemViewType(int position) {
if (position == 0) return GROUP_START;
return (getItem(position).charAt(0) == getItem(position - 1).charAt(0)) ?
ITEM : GROUP_START;
}
@Override
public int getViewTypeCount() {
return COUNT;
}
}