0

我想在列表底部添加来自 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();
            }
        }

    }
4

2 回答 2

0

我正在概述我在您的代码中看到的内容:
ConversationListTask.runOnPostExecute您检索列表项的方法上。创建一个适配器并将其设置为您的 listView。然后将列表的选择设置为最后一项。这会触发滚动到最后一个元素,并且您的 scrolledToBottom 条件变为真,然后LoadNextPage任务立即开始。正如我对您的问题所评论的那样,在您第一次调用 之后LoadNextPage,它将在其后执行时创建并执行它的另一个实例,并在每次后执行时重复它,这将导致无限调用。

首先,再次组织您的代码。如果您不想在将适配器设置为 listView 后将选择设置为底部,则从以下代码中删除此代码ConversationListTask.runOnPostExecute

mListViewConversation.setSelection(mListViewConversation.getAdapter().getCount() - 1);

并停止在 LoadNextPage 任务的每个 postExecute 上调用 LoadNextPage。

*顺便说一句,您正在正确监听 scrolledToBottom 事件onScroll。我使用类似的OnScrollListener实现,如果你愿意,可以分享。

于 2013-10-26T14:03:15.170 回答
0

你可以参考看看这个链接

可能是你想念

int currentPosition = lv.getFirstVisiblePosition();
lv.setSelectionFromTop(currentPosition + 1, 0);
于 2013-10-26T13:14:26.957 回答