0
我需要你的幫助,與我目前的項目。要求是讓listview分頁,並在向下滾動時加載其他數據,就像Facebook和Twitter應用一樣。Android listview無限滾動使用Retrofit
這是我到目前爲止已經試過:
MainActivity
listView.setOnScrollListener(new InfiniteScrollListener() {
public boolean onLoadMore(int page, int totalItemsCount) {
// Triggered only when new data needs to be appended to the list
// Add whatever code is needed to append new items to your AdapterView
customLoadMoreDataFromApi(page);
// or customLoadMoreDataFromApi(totalItemsCount);
return true; // ONLY if more data is actually being loaded; false otherwise.
}
});
private void getData() {
if(ConnectionDetector.hasNetworkConnection(getActivity())) {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(HOME_DOMAIN)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
WebServices service = restAdapter.create(WebServices.class);
service.getRecords(
"1", // Row
"10", // Count
new Callback<World>() {
@Override
public void success(Som som, Response response) {
dataList = Parser.getResponseBody(response);
adapter = new SomAdapter(getActivity(), dataList);
listView.setAdapter(adapter);
}
@Override
public void failure(RetrofitError retrofitError) {
Log.e(TAG, "" + retrofitError.toString());
}
});
} else {
Toast.makeText(getActivity(),
getActivity().getResources().getString(R.string.connection_error),
Toast.LENGTH_SHORT).show();
}
}
// Append more data into the adapter
public void customLoadMoreDataFromApi(int offset) {
// This method probably sends out a network request and appends new data items to your adapter.
// Use the offset value and add it as a parameter to your API request to retrieve paginated data.
// Deserialize API response and then construct new objects to append to the adapter
getData();
moreProgress.setVisibility(View.VISIBLE);
Log.e(TAG, "MORE PROGRESS");
}
InfiniteScrollListener:
public abstract class InfiniteScrollListener implements AbsListView.OnScrollListener {
private int bufferItemCount = 10;
private int currentPage = 0;
private int itemCount = 0;
private boolean isLoading = true;
public InfiniteScrollListener(int bufferItemCount) {
this.bufferItemCount = bufferItemCount;
}
public abstract void loadMore(int page, int totalItemsCount);
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Do Nothing
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
if (totalItemCount < itemCount) {
this.itemCount = totalItemCount;
if (totalItemCount == 0) {
this.isLoading = true; }
}
if (isLoading && (totalItemCount > itemCount)) {
isLoading = false;
itemCount = totalItemCount;
currentPage++;
}
if (!isLoading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + bufferItemCount)) {
loadMore(currentPage + 1, totalItemCount);
isLoading = true;
}
}
}
這顯示是滾動時的進度條。這工作,但我也需要附加到列表視圖下一個獲取的數據。我該怎麼做?我需要你的建議,因爲這是我第一次在Android中進行分頁。謝謝!
我有一個自定義適配器。我需要使分頁增加10.你可以給樣品addAll方法嗎?謝謝! –
您必須在dataList和notifyDataSetChanges()中添加數據; dataList.add(結果數據); adapter.notifyDataSetChanged(); –
@aelimill我的意思是自定義適配器的方法? –