我試圖在應用程序中實現無限列表,其中您從webservice獲得前25個結果,並在它到達列表底部時加載下一個25等,直到達到總結果。但是我迷失在無盡列表適配器如何知道我何時到達列表底部?我從cwac看了DemoAdapter和EndlessAdapterDemo例子,但無法弄清楚..或者它與我的Activity不是一個ListActivity(我認爲不是)?前25個結果加載到列表中,但之後我需要更改調用參數來調用下一個25我不使用asyntask(使用其他方法)獲取結果,或者我錯誤地解釋了在cacheInBackground中使用asnytask的指南?但是什麼時候以及如何來到無盡的適應?下面是我實現的適配器:無盡列表適配器CWAC與ActionBarActivity協同工作,以及如何觸發列表底部的加載?
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) {
}
請注意,我已經退休了該項目。 – CommonsWare
我明白了..那麼不禁要如何開始呢?我想我錯過了一些非常簡單的事情。 – cesztoszule
@CommonsWare順便說一句:我想實現無盡的listview模式,並且找不到退休背後的原因。該項目是由一些谷歌官方解決方案取代,還是剛剛達到穩定的「沒有其他要添加」的狀態?你還認爲這是該模式的推薦方法嗎? – Piovezan