しばらく前に取り組んだプロジェクトの 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+"]";
}
}
}