getLastVisiblePosition返回-1getLastVisiblePosition

2023-09-06 01:51:46 作者:归隐

我有我的的ListView (使用 CursorAdapte r)的一个问题。当我打电话 getListView()。getLastVisiblePosition()我收到 1 。这是一个问题,因为我的列表填充项。此外, getListView()。getFirstVisiblePosition()总是返回0,不管我在哪里滚动的名单上。任何想法?

I'm having a problem with my ListView (using CursorAdapter). When I call getListView().getLastVisiblePosition() I am receiving -1. This is a problem since my list is populated with items. Additionally, getListView().getFirstVisiblePosition() always returns 0, no matter where I am scrolled on the list. Any ideas?

它是与startManagingCursor

It has something to do with startManagingCursor

    @Override
    public void changeCursor(Cursor cursor) {
        super.changeCursor(cursor);
        MyActivity.this.mCursor = cursor;
        //startManagingCursor(MyActivity.this.mCursor);
    }

如果我注释掉startManagingCursor,一切工作正常。我也试图加入 stopManagingCursor()修改前光标,仍然有同样的问题。

If I comment out startManagingCursor, everything works fine. I've also tried adding stopManagingCursor() before changing the Cursor and still have the same problem.

推荐答案

这是因为你调用getListView()。getLastVisiblePosition()的那一刻ListView控件不呈现。您可以添加调用视图的消息队列是这样的:

This is because the moment you call getListView().getLastVisiblePosition() the listview is not rendered. You can add the call to the view's message queue like this:

listview.post(new Runnable() {
    public void run() {
        listview.getLastVisiblePosition();
    }
});