我为不久前从事的一个项目制作了一个 ArrayAdapter。基本上它所做的是检测滚动并根据您的滚动位置加载更多项目。这应该足以让你开始。仍然有 System.outs 来自测试。当然,如果您没有立即看到这是一个抽象类,因此您必须构建自己的检索方法。并且可能会根据您的需要更改此类。
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.example.epstest.http.APIClient;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.AbsListView.OnScrollListener;
import android.widget.TextView;
public abstract class AsyncArrayAdapter<T> extends ArrayAdapter<T> implements OnScrollListener {
private ArrayList<T> data;
private int dataBufferSize;
private int requestSize;
private boolean loadingData = false;
private MetaData metaData;
public AsyncArrayAdapter(ListView listView, int dataBufferSize) {
this(listView, dataBufferSize, new MetaData(0, 1));
}
public AsyncArrayAdapter(ListView listView, int dataBufferSize, MetaData metaData) {
super(listView.getContext(), 0);
data = new ArrayList<T>();
this.dataBufferSize = dataBufferSize;
this.metaData = metaData;
listView.setOnScrollListener(this);
}
@Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return position < data.size() && super.isEnabled(position);
}
@Override
public T getItem(int position) {
// TODO Auto-generated method stub
if(position >= data.size())
return null;
return data.get(position);
}
@Override
public int getCount() {
return data.size() + (loadingData ? 1 : 0);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(position < data.size())
return populateView(position, data.get(position), convertView, parent);
// if this data element is outside the view then its the loading view. get and return it
ProgressBar progBar = new ProgressBar(getContext());
progBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return progBar;
}
public void addItems(T[] items, MetaData metaData) {
this.metaData = metaData;
if(items.length >= dataBufferSize)
System.out.println("Items of length [" + items.length + "] cannot be greater than the data buffer [" + dataBufferSize + "]" );
for(int i=0;i<items.length;i++) {
if(data.size() > metaData.offset+i)
data.set(metaData.offset+i, items[i]);
else
data.add(metaData.offset+i, items[i]);
}
System.out.println("Added " + items.length + " items starting [" + metaData.offset + "] with max[" + metaData.max + "]" );
loadingData = false;
notifyDataSetChanged();
}
public void requestMoreItems() {
if(!loadingData && metaData.getMax() > data.size()) {
loadingData = true;
requestMoreItems(data.size(), requestSize);
notifyDataSetChanged();
}
}
protected abstract void requestMoreItems(int index, int numItems);
protected abstract View populateView(int index, T object, View viewToPopulate, ViewGroup parent);
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem + visibleItemCount >= totalItemCount - 1) {
requestMoreItems();
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
public MetaData getMetaData() {
return metaData;
}
public ArrayList<T> getData() {
return data;
}
public void setData(ArrayList<T> data) {
this.data = data;
}
public static class MetaData implements Serializable {
/**
*
*/
private static final long serialVersionUID = -217672841165264339L;
private int offset;
private int max;
public MetaData(int offset, int max) {
this.offset = offset;
this.max = max;
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
@Override
public String toString() {
return "MetaData[offset="+offset+", max="+max+"]";
}
}
}