The basic of Loaders

本文介绍Android框架中的Loaders组件,包括其特点、API接口及使用步骤。Loaders用于后台加载数据,可随配置变化自动更新数据,适用于Activity或Fragment。文章详细解释了LoaderManager.LoaderCallbacks的实现方法。

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:

  1. 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");
}
  1. 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);
}

  1. 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);
}
Output options: -1 List in one column -verbose Verbose listing -select val Select a single algorithm -commands List of standard commands -standard-commands List of standard commands -digest-commands List of message digest commands (deprecated) -digest-algorithms List of message digest algorithms -kdf-algorithms List of key derivation and pseudo random function algorithms -random-instances List the primary, public and private random number generator details -random-generators List of random number generators -mac-algorithms List of message authentication code algorithms -cipher-commands List of cipher commands (deprecated) -cipher-algorithms List of symmetric cipher algorithms -encoders List of encoding methods -decoders List of decoding methods -key-managers List of key managers -key-exchange-algorithms List of key exchange algorithms -kem-algorithms List of key encapsulation mechanism algorithms -signature-algorithms List of signature algorithms -asymcipher-algorithms List of asymmetric cipher algorithms -public-key-algorithms List of public key algorithms -public-key-methods List of public key methods -store-loaders List of store loaders -providers List of provider information -engines List of loaded engines -disabled List of disabled features -options val List options for specified command -objects List built in objects (OID<->name mappings) Provider options: -provider-path val Provider load path (must be before 'provider' argument if required) -provider val Provider to load (can be specified multiple times) -propquery val Property query used when fetching algorithms
06-16
(pdal-env) D:\scau_lidar_data\output>3d-tiles-tools lasTo3dtiles -i C22.las -o h Usage: 3d-tiles-tools <command> [options] Commands: convert Convert between tilesets and tileset package formats. The input and output can be one of the following: - The path of a tileset JSON file - The path of a directory that contains a 'tileset.json' file - The path of a '.3tz' file - The path of a '.3dtiles' file The input can also be the path of a ZIP file that contains the tileset data. The tileset JSON file in this ZIP is determined by the 'inputTilesetJsonFileName', which is 'tileset.json' by default. glbToB3dm Repackage the input glb as a b3dm with a basic header. glbToI3dm Repackage the input glb as a i3dm with a basic header. b3dmToGlb Extract the binary glTF asset from the input b3dm. i3dmToGlb Extract the binary glTF asset from the input i3dm. cmptToGlb Extract the binary glTF assets from the input cmpt. splitCmpt Split the input cmpt and write out its inner tiles. convertB3dmToGlb Convert a b3dm file into a glTF asset that uses glTF extensions to represent the batch- and feature table information convertPntsToGlb Convert a pnts file into a glTF asset that uses glTF extensions to represent the point properties and batch- and feature table information convertI3dmToGlb Convert an i3dm file into a glTF asset that uses glTF extensions to represent the batch- and feature table information. This conversion may be lossy if the GLB of the input i3dm contains animations. optimizeB3dm Pass the input b3dm through gltf-pipeline. To pass options to gltf-pipeline, place them after --options. (--options -h for gltf-pipeline help) optimizeI3dm Pass the input i3dm through gltf-pipeline. To pass options to gltf-pipeline, place them after --options. (--options -h for gltf-pipeline help) updateAlignment Update the alignment of the batch- and feature table and the tile dataas a whole, to meet the alignment requirements of the specification. This can be applied to B3DM, I3DM, PNTS, or CMPT tile data. gzip Gzips the input tileset directory. ungzip Ungzips the input tileset directory. combine Combines all external tilesets into a single tileset.json file. merge Merge any number of tilesets together into a single tileset. mergeJson Merge any number of tilesets together into a single tileset without copying resources to output directory. upgrade Upgrades legacy tilesets to comply to the 3D Tiles specification. By default, this will upgrade legacy tilesets to comply to 3D Tiles 1.0. These upgrades include: - The asset version will be set to '1.0' - Content that uses a 'url' will be upgraded to use 'uri'. - The 'refine' value will be converted to be in all-uppercase. - glTF 1.0 models in B3DM or I3DM will be upgraded to glTF 2.0. When specifying '--targetVersion 1.1', then the upgrades will include: - The asset version will be set to '1.1' - Content that uses a 'url' will be upgraded to use 'uri'. - The 'refine' value will be converted to be in all-uppercase. - The '3DTILES_content_gltf' extension declaration will be removed. - PNTS, B3DM, and I3DM content will be converted to glTF. pipeline Execute a pipeline that is provided as a JSON file analyze Analyze the input file, and write the results to the output directory. This will accept B3DM, I3DM, PNTS, CMPT, and GLB files (both for glTF 1.0 and for glTF 2.0), and write files into the output directory that contain the feature table, batch table, layout information, the GLB, and the JSON of the GLB tilesetToDatabase Create a sqlite database for a tileset. (Deprecated - use 'convert' instead) databaseToTileset Unpack a tileset database to a tileset folder. (Deprecated - use 'convert' instead) createTilesetJson Creates a tileset JSON file that just refers to given tile content files. If the input is a single file, then this will result in a single (root) tile with the input file as its tile content. If the input is a directory, then all content files in this directory will be used as tile content, recursively. The exact set of file types that are considered to be 'tile content' is not specified, but it will include GLB, B3DM, PNTS, I3DM, and CMPT files. Options: --version Show version number [boolean] -h, --help Show help [boolean] -f, --force Output can be overwritten if it already exists. [boolean] [default: false] --logLevel The log level. Valid values are 'trace', 'debug', 'info', 'warn', 'error', 'fatal', and 'silent' [string] [default: "info"] --logJson Whether log messages should be printed as JSON instead of pretty-printing [boolean] [default: false] Unknown arguments: i, o, lasTo3dtiles
10-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值