你可以使用MergeAdapter将数据合并到你的ListView中。这是我修改并完全测试过的版本。
public class MergeAdapter extends BaseAdapter implements SectionIndexer {
protected ArrayList<ListAdapter> pieces = new ArrayList<ListAdapter>();
protected String noItemsText;
public MergeAdapter() {
super();
}
public void addAdapter(ListAdapter adapter) {
pieces.add(adapter);
adapter.registerDataSetObserver(new CascadeDataSetObserver());
}
public Object getItem(int position) {
for (ListAdapter piece : pieces) {
int size = piece.getCount();
if (position < size) {
return (piece.getItem(position));
}
position -= size;
}
return (null);
}
public void setNoItemsText(String text){
noItemsText = text;
}
public ListAdapter getAdapter(int position) {
for (ListAdapter piece : pieces) {
int size = piece.getCount();
if (position < size) {
return (piece);
}
position -= size;
}
return (null);
}
public int getCount() {
int total = 0;
for (ListAdapter piece : pieces) {
total += piece.getCount();
}
if(total == 0 && noItemsText != null){
total = 1;
}
return (total);
}
@Override
public int getViewTypeCount() {
int total = 0;
for (ListAdapter piece : pieces) {
total += piece.getViewTypeCount();
}
return (Math.max(total, 1));
}
@Override
public int getItemViewType(int position) {
int typeOffset = 0;
int result = -1;
for (ListAdapter piece : pieces) {
int size = piece.getCount();
if (position < size) {
result = typeOffset + piece.getItemViewType(position);
break;
}
position -= size;
typeOffset += piece.getViewTypeCount();
}
return (result);
}
@Override
public boolean areAllItemsEnabled() {
return (false);
}
@Override
public boolean isEnabled(int position) {
for (ListAdapter piece : pieces) {
int size = piece.getCount();
if (position < size) {
return (piece.isEnabled(position));
}
position -= size;
}
return (false);
}
public View getView(int position, View convertView, ViewGroup parent) {
for (ListAdapter piece : pieces) {
int size = piece.getCount();
if (position < size) {
return (piece.getView(position, convertView, parent));
}
position -= size;
}
if(noItemsText != null){
TextView text = new TextView(parent.getContext());
text.setText(noItemsText);
return text;
}
return (null);
}
public long getItemId(int position) {
for (ListAdapter piece : pieces) {
int size = piece.getCount();
if (position < size) {
return (piece.getItemId(position));
}
position -= size;
}
return (-1);
}
public int getPositionForSection(int section) {
int position = 0;
for (ListAdapter piece : pieces) {
if (piece instanceof SectionIndexer) {
Object[] sections = ((SectionIndexer) piece).getSections();
int numSections = 0;
if (sections != null) {
numSections = sections.length;
}
if (section < numSections) {
return (position + ((SectionIndexer) piece)
.getPositionForSection(section));
} else if (sections != null) {
section -= numSections;
}
}
position += piece.getCount();
}
return (0);
}
public int getSectionForPosition(int position) {
int section = 0;
for (ListAdapter piece : pieces) {
int size = piece.getCount();
if (position < size) {
if (piece instanceof SectionIndexer) {
return (section + ((SectionIndexer) piece)
.getSectionForPosition(position));
}
return (0);
} else {
if (piece instanceof SectionIndexer) {
Object[] sections = ((SectionIndexer) piece).getSections();
if (sections != null) {
section += sections.length;
}
}
}
position -= size;
}
return (0);
}
public Object[] getSections() {
ArrayList<Object> sections = new ArrayList<Object>();
for (ListAdapter piece : pieces) {
if (piece instanceof SectionIndexer) {
Object[] curSections = ((SectionIndexer) piece).getSections();
if (curSections != null) {
for (Object section : curSections) {
sections.add(section);
}
}
}
}
if (sections.size() == 0) {
return (null);
}
return (sections.toArray(new Object[0]));
}
private class CascadeDataSetObserver extends DataSetObserver {
@Override
public void onChanged() {
notifyDataSetChanged();
}
@Override
public void onInvalidated() {
notifyDataSetInvalidated();
}
}
}
如果需要在每个适配器前添加一些标题,您还需要使用ListTitleAdapter。
public class ListTitleAdapter extends BaseAdapter {
Context context;
String text;
BaseAdapter parentAdapter;
public ListTitleAdapter(Context c, String textToShow) {
this(c, textToShow, null);
}
public ListTitleAdapter(Context c, String textToShow, BaseAdapter dependentAdapter) {
super();
context = c;
text = textToShow;
if(dependentAdapter != null){
parentAdapter = dependentAdapter;
}
}
public int getCount() {
if(parentAdapter != null){
if(parentAdapter.getCount() == 0){
return 0;
}
}
return 1;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layout = new LinearLayout(context);
TextView textView = new TextView(context);
textView.setText(text);
layout.addView(textView);
return layout;
}
}
这里是一个使用这两个类的小例子。
MergeAdapter mergeAdapter = new MergeAdapter()
mergeAdapter.addAdapter(new ListTitleAdapter(context, "Title1", someAdapter1))
mergeAdapter.addAdapter(someAdapter1)
mergeAdapter.addAdapter(new ListTitleAdapter(context, "Title2", someAdapter2))
mergeAdapter.addAdapter(someAdapter2)
mergeAdapter.addAdapter(new ListTitleAdapter(context, "Title3", someAdapter3))
mergeAdapter.addAdapter(someAdapter3)
mergeAdapter.setNoItemsText("Nothing to display. This list is empty.")
((ListView)findViewById(R.id.list)).setAdapter(mergeAdapter)