zoukankan      html  css  js  c++  java
  • Android Loader详解三:重启与回调

    重启装载器

    当你使用initLoader()时,如果指定ID的装载器已经存在,则它使用这个装载器.如果不存在呢,它将创建一个新的.但是有时你却是想丢弃旧的然后开始新的数据.

    要想丢弃旧数据,你应使用restartLoader().例如,下面这个SearchView.OnQueryTextListener的实现在用户查询发生改变时重启了装载器,装载器于是需重启从而能使用新的搜索过虑来进行一次新的查询.

    public boolean onQueryTextChanged(String newText) {
        // 当动作栏的搜索字串发生改时被调用.
        // 更新搜索过虑,然后重新启动装载利用这个新过虑进行新的查询.
        mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
        getLoaderManager().restartLoader(0, null, this);
        return true;
    }

    使用LoaderManager的回调

    LoaderManager.LoaderCallbacks是一个回调接口,它使得客户端可以与LoaderManager进行交互.

    装载器,一般指的是CursorLoader,我们希望在它停止后依然保持数据.这使得应用可以在activityfragment的 onStop() onStart() 之间保持数据,所以当用户回到一个应用时,它们不需等待数据加载.你使用LoaderManager.LoaderCallbacks 方法们,在需要时创建新的装载器,并且告诉应用什么时候要停止使用装载器的数据.

    LoaderManager.LoaderCallbacks 包含以下方法们:

    • onCreateLoader() —跟据传入的ID,初始化并返回一个新的装载器.

    • onLoadFinished() —当一个装载器完成了它的装载过程后被调用.

    • onLoaderReset() —当一个装载器被重置而什其数据无效时被调用.

    onCreateLoader

    当你试图去操作一个装载器时(比如,通过initLoader()),会检查是否指定ID的装载器已经存在.如果它不存在,将会触发LoaderManager.LoaderCallbacks 的方法onCreateLoader().这是你创建一个新装载器的地方.通常这个装载器是一个CursorLoader,但是你也可以实现你自己的装载器.

    在下面的例子中,回调方法onCreateLoader() 创建一个CursorLoader.你必须使用构造方法来建立CursorLoader ,构造方法需要向ContentProvider执行一次查询的完整信息作为参数,它尤其需要:

    • uri —要获取的内容的URI

    • projection —要返回的列组成的列被.传入null 将会返回所有的列,但这是低效的.

    • selection —一个过滤器,表明哪些行将被返回.格式化成类似SQLWHERE 语句的样子(除了没有WHERE).传入null 将返回所有的行.

    • selectionArgs —你可以在selection 中包含一些'?',它将被本参数的值替换掉.这些值出现的顺序与'?'selection中出现的顺序一至.值将作为字符串.

    • sortOrder —如何为行们排序.格式化成类似于SQLORDER BY 语句的样字(除了没有ORDERBY).传入null将使用默认顺序,默认顺序可能是无顺序.

    例子:

     // If non-null, this is the current filter the user has provided.
    String mCurFilter;
    ...
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // 这里是在需要创建新装载器时被调用的.
        // 我们只是简单的拥有一个装载器,所以我们不需要关心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");
    }
  • 相关阅读:
    [NPM] Avoid Duplicate Commands by Calling one NPM Script from Another
    [Algorithm] Dynamic programming: Find Sets Of Numbers That Add Up To 16
    [React] Refactor a Class Component with React hooks to a Function
    [Algorithm] Construct a Binary Tree and Binary Search
    设计模式(装饰者模式)
    IOS设计模式之二(门面模式,装饰器模式)
    IOS设计模式之三(适配器模式,观察者模式)
    linux内核源码阅读之facebook硬盘加速flashcache之五
    IOS设计模式之四(备忘录模式,命令模式)
    DRP总结
  • 原文地址:https://www.cnblogs.com/sishuiliuyun/p/3434084.html
Copyright © 2011-2022 走看看