使用场景:
济南IT培训的老师说:app显示数据,一般步骤是:查找数据,异步加载,更新UI; 然而当数据源有更新时怎么及时更新到界面上时,可能需要在Activity或Fragment的相应生命周期中及时处理数据的变化,有时可能会造成额外的开销,有时可能是极为复杂的,耗时且容易出问题的;有时甚至仅仅是查找数据这块就需要耗费我们大部分精力,anr或其他异常问题以及采用查找的机制、性能常常困扰着我们(从网络,数据库,ContentProvider和Files加载数据,究竟选择 thread + handler,还是AsyncTask,抑或是下面这个?)
Google在android3.0之后提供了Loader机制(3.0之前有support包),极大方便我们的开发;
| Loader简介
关于Loader,查看源码及官方文档可知, A class that performs asynchronous loading of data. While Loaders are active they should monitor the source of their data and deliver new results when the contents change Loader 是用来实现异步加载数据的类.当Loaders处于活动状态时,将会监听数据源的变化并且当内容发生改变时传递新的结果; Loader的衍生有以下这些:
public class CursorLoader extends AsyncTaskLoader<Cursor> {
public abstract class AsyncTaskLoader<D> extends Loader<D> {
public class Loader<D> {
简要说明下,首先在Activity或Fragment的相应生命周期中(分别是onCreate和onActivityCreated)得到LoaderManager实例,并利用LoaderManager实例初始化Loader(装载器),通过实现LoaderManager的LoaderCallback接口,在Loader相应生命周期进行处理,而此时Loader的生命周期即与Activity或Fragment进行关联,并实时监听各个生命周期,在AsyncTaskLoader或CursorLoader中将会调用后台加载数据方法,并在加载完数据或数据发生变化,甚至是Loader被重置发生变化时及时在相应回调方法进行处理,此时一个加载数据的完整流程完成;
Loader的生命周期相关:
onStartLoading(),
onStopLoading(),
onForceLoad(),
onReset().
以及其他
LoaderManager.LoaderCallback回调相关:
onCreateLoader, Instantiate and return a new Loader for the given ID. 根据initLoader()方法中制定的id重用或重建一个Loader,因此只有在Loader不存在的情况下才会调用 -onLoadFinished Called when a previously created loader has finished its load. 之前创建的Loader完成了加载操作时调用
onLoaderReset Called when a previously created loader is being reset, and thus making its data unavailable. 之前创建的Loader被重置时调用,此时它的数据也变得不可用;

| Loader源码解读 后续添加,此篇注重Loader的理解和使用,下面是两个官方实例,有助于对Loader的理解,当然更深入的理解还是要深入源码进行研究; 如Loader基于Observer pattern的设计等,对深入的理解还是很有帮助的;
| 实例1
来自:https://developer.android.com/reference/android/app/LoaderManager.html, 功能:here is the full implementation of a Fragment that displays a ListView containing the results of a query against the contacts content provider. It uses a CursorLoader to manage the query on the provider. 一个Fragment实例,它展示了一个包含ContentProvider加载联系人数据的查询结果的ListView.它使用了一个CursorLoader来管理provider上的查询操作;
该实例,使用了CursorLoader从ContentProvider中查找联系人数据并实时更新; 需要注意的是,SearchView 的OnQueryTextChange方法调用了loaderManager,并执行了loaderManager的restartLoader方法,它对Loader的生命周期的影响在于,已存在的Loader需要重启并使用新的过滤条件重新查询数据;
| 实例2
来自:https://developer.android.com/reference/android/content/AsyncTaskLoader.html 功能:Here is an example implementation of an AsyncTaskLoader subclass that loads the currently installed applications from the package manager. This implementation takes care of retrieving the application labels and sorting its result set from them, monitoring for changes to the installed applications, and rebuilding the list when a change in configuration requires this (such as a locale change). 下面这个例子使用了一个AsyncTaskLoader来从packageManager中加载当前手机上安装的应用.能够获取应用名称,排序,监听安装应用的变化以及党配置变化需要时修改应用列表等;
上述实例,自定义了一个继承AsyncTaskLoader的装载器,用来加载手机上的应用列表,通过监听广播 PackageIntentReceiver的变化捕获系统app的数据,并自定义一个 InterestingConfigChanges来监听用户自定义配置的变化. 需要注意的是 自定义的Loader,onReleaseResources方法,需要及时释放资源,比如: // For a simple List<> there is nothing to do. For something // like a Cursor, we would close it here. 如果数据源是这样的 :List<>,不用做任何操作;如果是Cursor,需要在这里close掉;比如实例1中的simpleCursorAdapter的swapCursor方法,传入了一个null, // This is called when the last Cursor provided to onLoadFinished() // above is about to be closed. We need to make sure we are no // longer using it. mAdapter.swapCursor(null);
更多济南IT培训相关咨询,请扫描下方二维码