我想在列表底部添加来自 Web 服务的动态数据。当我向下滚动列表以显示较旧的消息时,列表应该刷新..并加载下一页..但我的代码正在反向,即滚动顶部以加载下一页,我首先收到的是较旧的消息..这里是我的代码。如果有人可以帮助...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.conversations_activity);
mListView.setOnScrollListener(new OnScrollListener() {
int maxLoads = 30;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,visibleItemCount, int totalItemCount) {
if ((totalItemCount - 1 > maxLoads)&& ((totalItemCount - 1) % maxLoads == 0)) {
System.gc();
}
if (((firstVisibleItem + visibleItemCount) == totalItemCount)
&& !isDataLoading && isScrollingEnable) {
new LoadNextPage(ConversationsActivity.this).execute();
}
}
});
new ConversationListTask(ConversationsActivity.this).execute();
}
}
private class ConversationListTask extends BaseAsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
public ConversationListTask(Activity activity) {
super(activity);
}
@Override
public void runDoInBackground() throws RuntimeException, Exception {
isDataLoading = true;
isScrollingEnable = false;
mPageNumber = 0;
try {
listMessage = APIProcessor.listMessage(ConversationsActivity.this, "1",
mPageNumber);
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void runOnPostExecute() {
isDataLoading = false;
isScrollingEnable = true;
convoListAdapter = new ConversationListAdapter(
ConversationsActivity.this, listMessage, urlThumbImageLoader);
mListViewConversation.setAdapter(convoListAdapter);
mListViewConversation.setSelection(mListViewConversation.getAdapter()
.getCount() - 1);
}
}
class LoadNextPage extends BaseAsyncTask {
public LoadNextPage(Activity activity) {
super(activity);
setDefaultProgressDialogStatus(false);
findViewById(R.id.list_loading_view).setVisibility(View.VISIBLE);
}
@Override
public void runDoInBackground() throws RuntimeException, Exception {
isDataLoading = true;
tempList = APIProcessor.listMessage(ConversationsActivity.this, "1",
mPageNumber + 1);
}
@Override
public void runOnPostExecute() {
new LoadNextPage(ConversationsActivity.this).execute();
findViewById(R.id.list_loading_view).setVisibility(View.GONE);
isDataLoading = false;
if (tempList == null || tempList.isEmpty() || listMessage == null
|| listMessage.isEmpty()) {
}
else {
mPageNumber++;
for (int i = 0; i < tempList.size(); i++) {
listMessage.add(tempList.get(i));
}
convoListAdapter.notifyDataSetChanged();
}
}
}