参考APIDEMO及http://developer.android.com/guide/components/loaders.html#app

1、Introduced in Android 3.0, loaders make it easy to
asynchronously load data in an activity or fragment. Loaders have these characteristics:

They are available to every Activity and Fragment.

They provide asynchronous loading of data.

They monitor the source of their data and deliver new results when the content changes.

They automatically reconnect to the last loader's cursor when being recreated after a configuration change. Thus, they don't need to re-query their data.

2、重要的类及接口

LoaderManager An abstract class associated with an Activity or Fragment for
managing one or more Loader instances. This
helps an application manage longer-running operations in conjunction with the Activity orFragment lifecycle;
the most common use of this is with a CursorLoader,
however applications are free to write their own loaders for loading other types of data. 



There is only one LoaderManager per
activity or fragment. But aLoaderManager can
have multiple loaders.
LoaderManager.LoaderCallbacks A callback interface for a client to interact with the LoaderManager.
For example, you use the onCreateLoader() callback
method to create a new loader.
Loader An abstract class that performs asynchronous loading of data. This is the base class for a loader. You would typically use CursorLoader,
but you can implement your own subclass. While loaders are active they should monitor the source of their data and deliver new results when the contents change.
AsyncTaskLoader Abstract loader that provides an AsyncTask to
do the work.
CursorLoader A subclass of AsyncTaskLoader that
queries the ContentResolver and
returns a Cursor.
This class implements the Loader protocol
in a standard way for querying cursors, building on AsyncTaskLoader to
perform the cursor query on a background thread so that it does not block the application's UI. Using this loader is the best way to asynchronously load data from a ContentProvider,
instead of performing a managed query through the fragment or activity's APIs.

3、一般在以下情况使用Loader:

This section describes how to use loaders in an Android application. An application that uses loaders typically includes the following:

An Activity or Fragment.

An instance of the LoaderManager.

A CursorLoader to load data backed by a ContentProvider. Alternatively, you can implement your own subclass of Loader or AsyncTaskLoader to load data from some other source.

An implementation for LoaderManager.LoaderCallbacks. This is where you create new loaders and manage your references to existing loaders.

A way of displaying the loader's data, such as a SimpleCursorAdapter.

A data source, such as a ContentProvider, when using a CursorLoader.

4、LoaderManager有一个内部接口LoaderManager.LoaderCallbacks,这个接口定义了以下方法:

onCreateLoader() — Instantiate and return a new Loader for the given ID.

onLoadFinished() — Called when a previously created loader has finished its load.

onLoaderReset() — Called when a previously created loader is being reset, thus making its data unavailable.

一般而言,用户不需要直接对Loader进行操作,而是使用此接口的方法,进行间接控制。





5、Loader的一般操作

(1)使用LoaderManager.initLoader()初始化一个Loader,它将直接调用onCreateLoader()方法。

You typically initialize a Loader within the activity's onCreate() method, or within the fragment's onActivityCreated() method. You do this as follows:

getLoaderManager().initLoader(0, null, this);

The initLoader() call ensures that a loader is initialized and active. It has two possible outcomes:

  • If the loader specified by the ID already exists, the last created loader is reused.
  • If the loader specified by the ID does not exist, initLoader() triggers the LoaderManager.LoaderCallbacks method onCreateLoader(). This is where you implement the code to instantiate and return a new loader.

(2)初始化完成后,直接调用onLoadFinished()

If at the point of this call the caller is in its started state, and the requested loader already exists and has generated its data, then the system calls onLoadFinished() immediately (during initLoader()), so you must be prepared for this to happen.

版权声明:本文为博主原创文章,未经博主允许不得转载。

Loader之一:基本原理 分类: H1_ANDROID 2013-11-16 10:29 1923人阅读 评论(0) 收藏的更多相关文章

  1. C#多线程(下) 分类: C# 线程 2015-03-09 10:41 153人阅读 评论(0) 收藏

    四.多线程的自动管理(线程池) 在多线程的程序中,经常会出现两种情况: 一种情况: 应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然后才能给予响应 这一般使用ThreadPool(线 ...

  2. 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏

    文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...

  3. iOS开发:创建真机调试证书 分类: ios相关 2015-04-10 10:22 149人阅读 评论(0) 收藏

    关于苹果iOS开发,笔者也是从小白过来的,经历过各种困难和坑,其中就有关于开发证书,生产证书,in_house证书,add_Hoc证书申请过程中的问题,以及上架发布问题.今天就着重说一下关于针对于苹果 ...

  4. 全方位分析Objcetive-C Runtime 分类: ios技术 2015-03-11 22:29 77人阅读 评论(0) 收藏

    本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 简介 与Runtime交互 ...

  5. C#中的线程(上)-入门 分类: C# 线程 2015-03-09 10:56 53人阅读 评论(0) 收藏

    1.     概述与概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线 ...

  6. 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏

    也许你习惯了使用Eclipse编译和打包Android应用.不过,对于使用html5+js开发的phonegap应用,本文建议你抛弃Eclipse,改为使用命令行模式,绝对的快速和方便. 一直以来,E ...

  7. 网站通用登录模块代码 分类: ASP.NET 2014-12-06 10:49 615人阅读 评论(0) 收藏

    1.HTML部分:     <form id="form1" runat="server">     <script src=".. ...

  8. 欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏

    Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2476 Accepted: 1001 Description ...

  9. 多校3- RGCDQ 分类: 比赛 HDU 2015-07-31 10:50 2人阅读 评论(0) 收藏

    RGCDQ Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practic ...

随机推荐

  1. C语言之文件操作06——写数据到文本文件遇0停止

    //文件 /* =============================================================== 题目:输入10个篮球运动员的身高数据(cm)保存至D盘文 ...

  2. 取消xp开机默认登陆账户

    取消xp开机默认登陆账户 建了个新用户,把以前的用户删除后重新启动电脑,始终停留在 "正在启动" 界面,网上说是 Event Log:Eventlog(系统日志纪录服务) 没有自动 ...

  3. ellipsize-TextView省略号的设定

    ellipsize主要是当TextView的文字过长的时候,我们可以让它显示省略号 用法如下: 在xml中 <!--省略号在结尾--> android:ellipsize = " ...

  4. [LuoguU41039]PION后缀自动机 树链剖分+动态开点线段树

    链接 刚开始看出题人题解都吓蒙掉了,还以为是什么难题,结果就一板子题 思路:对每一个文件名开一棵线段树,然后树剖即可 #include<bits/stdc++.h> #define REP ...

  5. 多类别分类问题由 confusion matrix 到分类准确率(accuracy)的计算

    conf_mat = confusionmat(y_true, y_pred); % 首先根据数据集上的真实 label 值,和训练算法给出的预测 label 值, % 计算 confusion ma ...

  6. 23. Node.Js Buffer类(缓冲区)-(三)文件读取实例

    转自:https://blog.csdn.net/u011127019/article/details/52513109

  7. 2.1 Producer API官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 2.1 Producer API 2.1.生产者API 我们鼓励所有新开发的程序使用 ...

  8. go reflect 调用方法

    package main import ( "fmt" "reflect" ) type A struct { } func (A) Test() { fmt. ...

  9. position记录

    1.  relative(相对定位):生成相对定位的元素,通过top,bottom,left,right的设置相对于其正常(原先本身)位置进行定位.可通过z-index进行层次分级.均是以父级的左上角 ...

  10. CF-833B The Bakery(线段树优化Dp)

      Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredie ...