更新 SimpleCursorAdapter,同时保持 ListView 中的滚动位置
我的问题:每当我通过其(自定义)SimpleCursorAdapter 更新其内容时,我的 ListView 都会将其滚动位置重置到顶部。我希望 ListView 在更新时保持其滚动位置。
我首先每次创建一个新的适配器实例并使用 ListView.setAdapter()
,但根据 此讨论 我应该更新适配器而不是重置它。我找不到一种方法可以使 SimpleCursorAdapter 正常工作。
以下是我尝试过的一些方法,省略了方法参数:
- 使用新光标的 SimpleCursorAdapter.changeCursorAndColumns()
成功更新了适配器,但仍然重置了滚动位置。
- SimpleCursorAdapter.changeCursor()
的行为方式相同。
- SimpleCursorAdapter.swapCursor()
在 api 11 之前不可用,我正在为 api 8 进行开发。
- SimpleCursorAdapter.notifyDataSetChanged()
不会更新 ListView。 (也许我还缺少另一个步骤。)
- ListView.invalidate()
不会更新 ListView。
- SimpleCursorAdapter.requery()
使 ListView 为空,已被弃用。
- 我熟悉 ListView.setSelection()
解决方案,但我不希望我的 ListView 在更新时根本移动。这会将其捕捉到列表中的某个位置。
这看起来应该是一个常见且简单的问题。有人处理过吗?
My problem: My ListView resets its scroll position to the top whenever I update its contents through its (customized) SimpleCursorAdapter. I would like the ListView to maintain its scroll position when updated.
I started out by creating a new adapter instance every time and using ListView.setAdapter()
, but according to this discussion I should be updating the adapter instead of resetting it. I can't find a way to do this for SimpleCursorAdapter that works correctly.
Here are some things I've tried, omitting the method arguments:
- SimpleCursorAdapter.changeCursorAndColumns()
with a fresh cursor successfully updates the adapter but still resets the scroll position.
- SimpleCursorAdapter.changeCursor()
acts the same way.
- SimpleCursorAdapter.swapCursor()
isn't available until api 11, and I am developing for api 8.
- SimpleCursorAdapter.notifyDataSetChanged()
does not update the ListView. (Maybe there is another step I'm missing.)
- ListView.invalidate()
does not update the ListView.
- SimpleCursorAdapter.requery()
makes the ListView blank, and is deprecated.
- I'm familiar with the ListView.setSelection()
solution, but I don't want my ListView to move at all when it is updated. This snaps it to a position in the list.
This seems like it should be a common and simple problem. Has anybody dealt with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了类似的问题:只要光标的内容发生变化,我的 CursorAdapter 就会重置滚动位置。
我没有意识到每次数据更改时我实际上都设置了一个新的列表适配器。我认为光标本身会通知适配器其内容的更改,但在我的情况下,是 ContentProvider 触发 ListFragment 中的 LoaderManager.LoaderCallbacks.onLoadFinished() 。在该回调中,我现在使用 CursorAdapter.changeCursor() 而不是创建新适配器,一切正常。
我希望这有助于解决您的问题。
I've had a similar problem: My CursorAdapter reset the scroll position whenever the contents of the cursor changed.
I didn't realize that I actually did set a new list adapter each time the data changed. I thought the cursor himself would notify the adapter about changes to it's content but in my case it is the ContentProvider that triggers LoaderManager.LoaderCallbacks.onLoadFinished() in my ListFragment. In that callback I now use CursorAdapter.changeCursor() instead of creating a new adapter and everything works just fine.
I hope this helps solving your problem.
在异步任务的执行后执行中使用以下代码是否对我有用,
CursorAdapter 是全局变量
Did the same in post execute of Async Task used following code worked for me
with CursorAdapter being a global variable