如何在使用新的Loader API时保持ListView的位置? [英] How to maintain ListView position when using the new Loader APIs?

查看:225
本文介绍了如何在使用新的Loader API时保持ListView的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Honeycomb中,引入了 Loader API作为向应用程序提供数据的正确方法通过在背景螺纹做重的举。在我的应用程序,我正在努力用 Loader 替换所有 Cursor ,返回游标 s。由于 Cursor.requery()现在已折旧,建议只调用 restartLoader ,并允许工作再次在后台线程上执行,然后 changeCursor onLoadFinished 中返回

In Honeycomb the Loader APIs were introduced as the proper way to provide data to an application by doing the heavy lifting on a background thread. In my application I'm working to replace all my Cursors with Loaders that return Cursors. Since Cursor.requery() is depreciated now, it is recommended to just call restartLoader and allow the work to again be done on a background thread and then changeCursor when it returns in onLoadFinished.

所有这些工作奇妙,除了ListView不需要维护它的滚动位置,当我想重新查询数据,使用 Cursor.requery() this used工作,因为它是与更新的数据相同的Cursor实例。

All of this works wonderfully except that the ListView doesn't maintain it's scroll position when I want to requery the data, using Cursor.requery() this used to work because it was the same Cursor instance with updated data.

如何在不松动滚动位置的情况下刷新加载程序?

How can I refresh my Loader without loosing the scroll position?

推荐答案

p> Loader 类本身不知道数据集何时更改,而是特定于 CursorLoader 的实现。现在在我的应用程序中,我需要刷新的数据是在本地SQLite数据库(没有ContentProvider),所以我不得不修改 CursorLoader 使用我的本地数据库,如Dianne Hackborne 开发人员群组中提及的

The Loader class by itself doesn't know when the dataset changes and is implementation specific to CursorLoader currently. Now inside my application the data I needed to refresh is inside a local SQLite database (no ContentProvider) so I had to modify the CursorLoader to use my local database instead, as Dianne Hackborne mentioned on the developer group.

不幸的是,只是从loadInBackground中返回Cursor不允许 ContentObserver 正确地通知Loader当数据改变时,这是因为 AbstractCursor 只调用 notifyChanged()

Unfortunately just returning a Cursor from loadInBackground doesn't allow the ContentObserver to properly notify the Loader when the data changes, this is because AbstractCursor only calls notifyChanged() when requery() is called on the Cursor.

由于 ContentObserver 不会通知 Loader 数据已更改,我最终根据需要调用 Loader.onContentChanged()自己。在这一点,一切似乎是工作,但我会更新这个答案,如果任何重大问题出现。

Since the ContentObserver will never notify the Loader that the data has changed, I ended up calling Loader.onContentChanged() myself as needed. At this point it everything seems to be working but I'll update this answer if any significant issues arrise.

这篇关于如何在使用新的Loader API时保持ListView的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆