getLastVisiblePosition

getLastVisiblePosition

本文介绍了getLastVisiblePosition 返回 -1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ListView(使用 CursorAdapter)有问题.当我调用 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,一切正常.我也尝试在更改 Cursor 之前添加 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() 时,列表视图并未呈现.您可以像这样将调用添加到视图的消息队列中:

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();
    }
});

这篇关于getLastVisiblePosition 返回 -1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-08 19:50