The basic of Loaders
来源:互联网 发布:centos 7 安装后配置 编辑:程序博客网 时间:2024/05/16 00:43
Loaders 具有以下特性:
1. 对于每个Activity或者Fragment可用,在每个Activity或者Fragment中,可以具有多个Loaders,但只有一个LoaderManager来管理。
2. 获取数据时是异步的。
3. Loaders可以检测数据的改变,并自动传递该数据。
4. 当配置改变需要重新构造Loaders时,Loaders可以利用先前的数据结果,没必要重新获取数据。
关于Loaders的API接口:
1. LoaderManager;
2. LoaderManager.LoaderCallbacks;
3. Loader;
4. AsyncTaskLoader;
5. CursorLoader;
如何使用Loaders Steps:
首先,你需要一个Activity或者Fragment,然后implement LoaderManager.LoaderCallbacks;
Step 2, 在你的Activity 的onCreaate()或者Fragment的onActivityCreated()方法中,调用
// Prepare the loader. Either re-connect with an existing one,// or start a new one.getLoaderManager().initLoader(0, null, this);
initLoader(para1,para2,para3),para1是你的Loaders的Id,系统用此参数判断你的Loaders的版本,如果不一样则重新获取数据,如果一样则获取数据或者使用上一次的数据结果。para2是一些参数,可以为null;para3,是LoaderManager.LoaderCallbacks的实例。
默认情况下,LoaderManager.LoaderCallbacks可以追随Loaders的状态,当需要创建Loaders时,callback onCreateLoader()被调用,当已存在Loaders实例时,如果无需重新创建,当你调用完initLoader后,callback onLoadFinished立刻调用。需要指出的是,initLoader会返回一个Loader 的reference,但是你无需指向该Loader实例,我们使用LoaderManager.LoaderCallback来与Loader实例交互。
当你已经通过Loader获取了数据时,突然你需要摒弃旧的数据,此时你可以调用:
getLoaderManager().restartLoader(0, null, this);
如何使用LoaderManager.LoaderCallbacks:
- onCreateLoader(),对于一个确定的Id,创建一个Loaders的实例
// If non-null, this is the current filter the user has provided.String mCurFilter;...public Loader<Cursor> onCreateLoader(int id, Bundle args) { // This is called when a new Loader needs to be created. This // sample only has one Loader, so we don't care about the ID. // First, pick the base URI to use depending on whether we are // currently filtering. Uri baseUri; if (mCurFilter != null) { baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter)); } else { baseUri = Contacts.CONTENT_URI; } // Now create and return a CursorLoader that will take care of // creating a Cursor for the data being displayed. String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.HAS_PHONE_NUMBER + "=1) AND (" + Contacts.DISPLAY_NAME + " != '' ))"; return new CursorLoader(getActivity(), baseUri, CONTACTS_SUMMARY_PROJECTION, select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");}
- onLoadFinished,当成功获取数据时,调用此方法。当重新获取了数据后,你直接的旧数据需要摒弃,当使用Cursor时,你无需使用close,系统会处理这些,你只需要让新数据替换旧数据,如:
// This is the Adapter being used to display the list's data.SimpleCursorAdapter mAdapter;...public void onLoadFinished(Loader<Cursor> loader, Cursor data) { // Swap the new cursor in. (The framework will take care of closing the // old cursor once we return.) mAdapter.swapCursor(data);}
- onLoaderReset ,当先前调用的Loaders实例reset时,该回调方法调用来摒弃数据。
// This is the Adapter being used to display the list's data.SimpleCursorAdapter mAdapter;...public void onLoaderReset(Loader<Cursor> loader) { // 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);}
- The basic of Loaders
- The basics of Java class loaders
- The basic of Fragment
- The basic of Java
- the basic of RXSwift
- The Basic Links of SCM
- Basic of the Unix Philosophy
- The basic concepts of OFDM
- lualatex:The basic structure of the book
- Object-oriented of the three basic characteristics
- The 13 Basic Principles of Gameplay Design
- Structure of the Basic MDX Query
- The basic principles of design pattern
- the basic ability of human being
- The basic knowledge of deep learning
- Text Editor: The Basic of Basics
- the basic operation of sequence in python
- Loaders
- PAT甲级1004
- redis的使用场景
- Eclipse导入项目常见问题----jdk版本问题(有个红色感叹号)01
- 数学——vijos进制转换
- Java排序算法(转)
- The basic of Loaders
- Spark集群安装
- 朴素贝叶斯分类
- [UE4]实现存档功能
- Hibernate批量更新和批量删除批量添加 1(转)
- 初窥Linux 之 我最常用的命令
- 工程的ld文件
- C语言数据结构——串的堆分配
- vue最简单的例子