拉到刷新返回列表的第一个位置

时间:2014-11-19 12:47:52

标签: java android android-asynctask pull-to-refresh

我有一个拉动刷新的问题,我使用com.handmark.pulltorefresh。当我看到列表的最后一个元素时,我需要在此列表中添加新元素。我做到了:

view = inflater.inflate(R.layout.fragment_generalnews, container,
                    false);
            mPullRefreshListView = (PullToRefreshListView) view
                    .findViewById(R.id.pull_refresh_list2);

....
// Add an end-of-list listener
            mPullRefreshListView
                    .setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

                        @Override
                        public void onLastItemVisible() {
                            if (GeneralActivity.razr <= 7.0) {
                                pagenumber += 1;
                                Log.d("Menu", "Page2 = " + pagenumber);
                                newpage = "page/" + pagenumber + "/";
                                final int position = mPullRefreshListView.getRefreshableView().getFirstVisiblePosition()+1;
                                new MyTask().execute();
                                Log.d("List", "position of list = " + position);
                                mPullRefreshListView.onRefreshComplete();
                                mPullRefreshListView.post(new Runnable() {

                                    @Override
                                    public void run() {
                                        // TODO Auto-generated method stub
                                        mPullRefreshListView.getRefreshableView().setSelection(position);
                                        kfulist.setSelection(position);
                                    }
                                });
                                Log.d("List", "List " + kfunews.toString());
                            }
                        }
                    });

onPostExecute中的MyTask()我刷新适配器:

kfulist = mPullRefreshListView.getRefreshableView();
                    kfulist.setAdapter(newsadapter);
                    kfulist = mPullRefreshListView.getRefreshableView();

                    // Need to use the Actual ListView when registering
                    // for Context Menu
                    registerForContextMenu(kfulist);
                    newsadapter.notifyDataSetChanged();

当我到达列表的末尾时,它会被更新并添加新元素,但随后返回到列表的第一个元素。我无法理解为什么。

1 个答案:

答案 0 :(得分:0)

我们在这里讨论 AsyncTask 。由于它是异步的,当mPullRefreshListView.onRefreshComplete()在执行任务后附加,此任务实际上尚未完成。

您应该在onPostExecute中添加您想要影响UI的所有内容(在UI线程中执行onPostExecute

试试这个:

public void onPostExecute(**whatever the parameter**)

{         kfulist = mPullRefreshListView.getRefreshableView();         kfulist.setAdapter(newsadapter);         //没用         // kfulist = mPullRefreshListView.getRefreshableView();

    // Need to use the Actual ListView when registering
    // for Context Menu
    registerForContextMenu(kfulist);
    newsadapter.notifyDataSetChanged();

    Log.d("List", "position of list = " + position);
    mPullRefreshListView.onRefreshComplete();
    mPullRefreshListView.post(new Runnable()
    {
        @Override
        public void run()
        {
            mPullRefreshListView.getRefreshableView().setSelection(position);
            kfulist.setSelection(position);
        }
    });
    Log.d("List", "List " + kfunews.toString());     

    // [...]
}