我正在尝试在应用程序中实现无限列表,您可以从 Web 服务获得前 25 个结果,当它到达列表底部时,它会加载下一个 25,依此类推,直到达到总结果。但是我迷失了无尽的列表适配器如何知道我何时到达列表的底部?我查看了 cwac 中的 DemoAdapter 和 EndlessAdapterDemo 示例,但无法弄清楚.. 或者它与我的 Activity 不是 ListActivity (我认为不是)有关?前 25 个结果已加载到列表中,但之后我需要更改调用参数以调用下一个 25 我不使用 asyntask(使用其他东西)来获取结果,或者我误解了在 cacheInBackground 中使用 asnytask 的指南?但是无尽的适配器何时以及如何发挥作用?这是我对适配器的实现:
public class SearchResultEndlessAdapter extends EndlessAdapter {
private static final String TAG = SearchResultEndlessAdapter.class.getSimpleName();
private RotateAnimation rotate = null;
private LayoutInflater inflater;
private View pendingView = null;
private List<KeywordSearchResults> newKsearchResultList;
private int limit = 0;
public SearchResultEndlessAdapter(SearchResultListAdapter adapter, Context ctx) {
super(adapter);
this.inflater = LayoutInflater.from(ctx);
rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(600);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
}
/**
* Set the new list that is loaded from the webservice
*
* @param newKsearchResultList
* List<KeywordSearchResults>
*/
public void setNewData(List<KeywordSearchResults> newKsearchResultList) {
this.newKsearchResultList = new ArrayList<KeywordSearchResults>(newKsearchResultList);
}
/**
* The value of total results
*
* @param limit
* int
*/
public void setLimit(int limit) {
this.limit = limit;
}
@Override
protected void appendCachedData() {
LogService.log(TAG, "appendCachedData");
if (getWrappedAdapter().getCount() < limit) {
SearchResultListAdapter srListAdapter = (SearchResultListAdapter) getWrappedAdapter();
for (KeywordSearchResults kws : newKsearchResultList) {
srListAdapter.add(kws);
}
}
}
@Override
protected boolean cacheInBackground() throws Exception {
LogService.log(TAG, "cacheInBackground");
return (getWrappedAdapter().getCount() < limit);
}
@Override
protected View getPendingView(ViewGroup parent) {
LogService.log(TAG, "getPendingView");
View row = inflater.inflate(R.layout.row, null);
pendingView = row.findViewById(android.R.id.text1);
pendingView.setVisibility(View.GONE);
pendingView = row.findViewById(R.id.throbber);
pendingView.setVisibility(View.VISIBLE);
pendingView.startAnimation(rotate);
startProgressAnimation();
return (row);
}
public void startProgressAnimation() {
if (pendingView != null) {
pendingView.startAnimation(rotate);
}
}
}
以及 myActivity 中扩展 ActionBarActivity 的部分:
//in oncreate
searchResultEndlessAdapter = new SearchResultEndlessAdapter(searchResultadapter,this);
searchResultEndlessAdapter.setRunInBackground(false);
mListView.setAdapter(searchResultEndlessAdapter);
mListView.setOnItemClickListener(this);
mListView.setOnScrollListener(this);
//callbacks of scroll
@Override
public void onScroll(AbsListView view, int firstVisible, int visibleCount, int totalCount) {
//something needs to be done here with endlessAdapter ?
}
@Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
}