我需要你对我当前项目的帮助。要求是使listview分页,并在向下滚动时加载其他数据,就像Facebook和Twitter应用程序一样。
这是我到目前为止所尝试的:
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中进行分页。谢谢!
答案 0 :(得分:1)
你做的一切都是正确的。但是,您只需添加新获取的数据
,而不是创建新的SomAdapter@Override
public void success(Som som, Response response) {
dataList = Parser.getResponseBody(response);
if (adapter == null) {
adapter = new SomAdapter(getActivity(),dataList);
listView.setAdapter(adapter);
} else {
adapter.addAll(dataList);
}
}
如果您制作自定义适配器而不是扩展ArrayAdapter,请确保您的addAll方法具有notifyDataSetChanged
。
更改getData()以获取param偏移量和分页请求逻辑。