LoaderManager使用具体解释(一)---没有Loader之前的世界
Lockwood的努力。让我们看到如此精彩的文章。
第一部分 没有Loader之前的世界
曾经情况
并且该“managed cursors”方式在activity配置变化(configuration changed,横竖屏切换、键盘弹出等)时。并不会保持数据。
在这些情况下会又一次requry()数据,可是实际上是没有必要、低效。并且会导致方向切换呆滞和卡顿。
Managed Cursors的问题
以下提供的代码是在一个ListActivity里面加载数据使用的是Android3.0之前的APIs。该活动从ContentProvider里面查询数据,而且管理返回的cursor。
查询结果用SimpleCursorAdapter包装,而且显示在listview中。
代码精炼例如以下:
public class SampleListActivity extends ListActivity {
private static final String[] PROJECTION = new String[] {"_id", "text_column"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Performs a "managed query" to the ContentProvider. The Activity
// will handle closing and requerying the cursor.
//
// WARNING!! This query (and any subsequent re-queries) will be
// performed on the UI Thread!!
Cursor cursor = managedQuery(
CONTENT_URI, // The Uri constant in your ContentProvider class
PROJECTION, // The columns to return for each data row
null, // No where clause
null, // No where clause
null); // No sort order
String[] dataColumns = { "text_column" };
int[] viewIDs = { R.id.text_view };
// Create the backing adapter for the ListView.
//
// WARNING!! While not readily obvious, using this constructor will
// tell the CursorAdapter to register a ContentObserver that will
// monitor the underlying data source. As part of the monitoring
// process, the ContentObserver will call requery() on the cursor
// each time the data is updated. Since Cursor#requery() is performed
// on the UI thread, this constructor should be avoided at all costs!
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this, // The Activity context
R.layout.list_item, // Points to the XML for a list item
cursor, // Cursor that contains the data to display
dataColumns, // Bind the data in column "text_column"...
viewIDs); // ...to the TextView with id "R.id.text_view"
// Sets the ListView's adapter to be the cursor adapter that was
// just created.
setListAdapter(adapter);
}
}
上面的代码有3个问题。假设你读懂了上面讲的内容。那么開始两个问题不难读懂。
可是使用这样的方式导致每次activity的状态从stopped返回时都须要又一次查询数据,这一般会导致UI线程卡顿。让activity替我们管理cursor所冒的风险大于便捷性。
该构造方法问题是,当有改变时,将导致SimpleCursorAdapter自己主动查询。
更详细来说。CursorAdapter会在数据上注冊一个ContentObserver监听器。当监听的数据变化时会requery数据。
我们应该使用标准的构造函数(假设你尝试使用CursorLoader来加载适配器数据,确保最后一个參数传入值为0)。
假设你不能理解第三条,没有关系,这只不过个小错误。
更大的设备。7~10寸的平板的应用更复杂、交互很多其它、有很多其它的界面布局。兴许将介绍Fragment,fragment使应用更动态化,很多其它的事件驱动。一个简单的。单线程的方法来加载数据显然已经不再合适。所以这就是Loader和LoaderManager在Android3.0诞生的背景。
Android3.0,Loaders, LoaderManager
Loaders确保全部的cursor操作是异步的。从而排除了UI线程中阻塞的可能性。
并且,当通过LoaderManager来管理,Loaders还能够在activity实例中保持当前的cursor数据,也就是不须要又一次查询(比方,当由于横竖屏切换须要又一次启动activity时)。
还有额外的优点,当数据改变时。Loaders能够非常聪明的自己主动检測底层数据的更新和又一次检索。
总结
还有一方面。Loaders能够通过将数据加载工作交给单独的后台进程。将明显的提高用户体验。
LoaderManager使用具体解释(一)---没有Loader之前的世界的更多相关文章
- LoaderManager使用详解(一)---没有Loader之前的世界
来源: http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html 感谢作者Alex ...
- LoaderManager使用具体解释(三)---实现Loaders
这篇文字将介绍Loader<D>类,而且介绍自己定义Loader的实现.这是本系列的第三篇文章. 一:Loaders之前世界 二:了解LoaderManager 三:实现Loaders 四 ...
- LoaderManager使用具体解释(二)---了解LoaderManager
了解LoaderManager 这篇文章将介绍LoaderManager类,这是该系列的第二篇文章. 一:Loaders之前世界 二:了解LoaderManager 三:实现Loaders 四:实例: ...
- LoaderManager使用具体解释(四)---实例:AppListLoader
实例:AppListLoader 这篇文章将是我的第四篇,也就是最后一篇该系列的文章.请在评论里面告诉我他们是否实用.前面几篇文章的链接例如以下: 一:Loaders之前世界 二:了解LoaderMa ...
- [Android] Android 用于异步加载 ContentProvider 中的内容的机制 -- Loader 机制 (LoaderManager + CursorLoader + LoaderManager.LoaderCallbacks)
Android 用于异步加载 ContentProvider 中的内容的机制 -- Loader 机制 (LoaderManager + CursorLoader + LoaderManager.Lo ...
- LoaderManager使用详解(三)---实现Loaders
这篇文字将介绍Loader<D>类,并且介绍自定义Loader的实现.这是本系列的第三篇文章. 一:Loaders之前世界 二:了解LoaderManager 三:实现Loaders ...
- LoaderManager使用详解(二)---了解LoaderManager
了解LoaderManager 这篇文章将介绍LoaderManager类,这是该系列的第二篇文章. 一:Loaders之前世界 二:了解LoaderManager 三:实现Loaders 四:实 ...
- Loader之二:CursorLoader基本实例
参考APIDEMO:sdk\samples\android-19\content\LoaderCursor 1.创建主布局文件,里面只包含一个Fragment. <FrameLayout xml ...
- Android 之异步加载LoaderManager
LoaderManager: Loader出现的背景: Activity是我们的前端页面展现,数据库是我们的数据持久化地址,那么正常的逻辑就是在展示页面的渲染页面的阶段进行数据库查询.拿到数据以后才展 ...
随机推荐
- TensorFlow的学习
1.先判断python的版本(因为有些python版本自带pip,可以参考我写的对pip的认识的博客文章),选择是否安装pip,然后安装更新tensorflow如:sudo pip install - ...
- 给网站设置ICO图标
方法一: 直接在站点根目录下放入名为:favicon.ico 的图标文件(必须要为 ICO 文件,BMP 及其他格式的图片文件不行).还有将 favicon.ico 中的 favico ...
- kill&&pkill&&killall---删除执行中的程序
命令功能: 发送指定的信号到相应进程.不指定型号将发送SIGTERM(15)终止指定进程.如果无法终止该程序可用“-KILL” 参数,其发送的信号为SIGKILL(9) ,将强制结束进程 使用ps命令 ...
- CODEVS——T 3013 单词背诵
http://codevs.cn/problem/3013/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 De ...
- CODEVS——T2744 养鱼喂妹纸
http://codevs.cn/problem/2744/ 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Descr ...
- .Net 开源控件 NPlot使用小结
NPlot是一款非常难得的.Net平台下的图表控件,能做各种曲线图,柱状图,饼图,散点图,股票图等,而且它免费又开源,使用起来也非常符合程序员的习惯.授权方式为BSD许可证. 下载链接: http:/ ...
- ArcGIS “Error HRESULT E_FAIL has been returned from a call to a COM component.” 异常的解决
错误提示内容: {System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been ret ...
- 「微信小程序」有哪些冲击与机会?
昨天晚上相信大家的朋友圈被「微信小程序」刷屏了,这影响力赶上了国务院出台新政策一样,足以说明微信在中国的影响力之大. 然后今天公号后台一大堆人问我怎么看这件事,不少人非常忧虑,仿佛自己将要失业一样. ...
- jQuery03
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- go语言函数作为参数传递
go语言函数作为参数传递,目前给我的感觉几乎和C/C++一致.非常的灵活. import "fmt" import "time" func goFunc1(f ...