加載器


Android 3.0 中引入了加載器,支持輕鬆在 Activity 或片段中異步加載數據。 加載器具有以下特徵:

  • 可用於每個 Activity 和 Fragment
  • 支持異步加載數據。
  • 監控其數據源並在內容變化時傳遞新結果。
  • 在某一配置更改後重建加載器時,會自動重新連接上一個加載器的遊標。 因此,它們無需重新查詢其數據。

Loader API 摘要


在應用中使用加載器時,可能會涉及到多個類和接口。 下表彙總了這些類和接口:

類/接口 說明
LoaderManager 一種與 Activity 或 Fragment 相關聯的的抽象類,用於管理一個或多個 Loader 實例。 這有助於應用管理與Activity 或 Fragment 生命週期相關聯的、運行時間較長的操作。它最常見的用法是與 CursorLoader 一起使用,但應用可自由寫入其自己的加載器,用於加載其他類型的數據。 

每個 Activity 或片段中只有一個 LoaderManager。但一個 LoaderManager 可以有多個加載器。
LoaderManager.LoaderCallbacks 一種回調接口,用於客戶端與 LoaderManager 進行交互。例如,您可使用 onCreateLoader() 回調方法創建新的加載器。
Loader 一種執行異步數據加載的抽象類。這是加載器的基類。 您通常會使用 CursorLoader,但您也可以實現自己的子類。加載器處於活動狀態時,應監控其數據源並在內容變化時傳遞新結果。
AsyncTaskLoader 提供 AsyncTask 來執行工作的抽象加載器。
CursorLoader AsyncTaskLoader 的子類,它將查詢 ContentResolver 並返回一個 Cursor。此類採用標準方式爲查詢遊標實現 Loader 協議。它是以 AsyncTaskLoader 爲基礎而構建,在後臺線程中執行遊標查詢,以免阻塞應用的 UI。使用此加載器是從 ContentProvider 異步加載數據的最佳方式,而不用通過片段或 Activity 的 API 來執行託管查詢。

上表中的類和接口是您在應用中用於實現加載器的基本組件。 並非您創建的每個加載器都要用到上述所有類和接口。但是,爲了初始化加載器以及實現一個 Loader 類(如 CursorLoader),您始終需要要引用 LoaderManager。 下文將爲您展示如何在應用中使用這些類和接口。

在應用中使用加載器


此部分描述如何在 Android 應用中使用加載器。使用加載器的應用通常包括:

啓動加載器

LoaderManager 可在 Activity 或 Fragment 內管理一個或多個 Loader 實例。每個 Activity 或片段中只有一個 LoaderManager

通常,您會在 Activity 的 onCreate() 方法或片段的onActivityCreated() 方法內初始化 Loader。您執行操作如下:

// Prepare the loader.  Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);

initLoader() 方法採用以下參數:

  • 用於標識加載器的唯一 ID。在此示例中,ID 爲 0。
  • 在構建時提供給加載器的可選參數(在此示例中爲 null)。
  • LoaderManager.LoaderCallbacks 實現, LoaderManager 將調用此實現來報告加載器事件。在此示例中,本地類實現 LoaderManager.LoaderCallbacks 接口,因此它會傳遞對自身的引用 this

initLoader() 調用確保加載器已初始化且處於活動狀態。這可能會出現兩種結果:

無論何種情況,給定的 LoaderManager.LoaderCallbacks 實現均與加載器相關聯,且將在加載器狀態變化時調用。如果在調用時,調用程序處於啓動狀態,且請求的加載器已存在並生成了數據,則系統將立即調用 onLoadFinished()(在 initLoader() 期間),因此您必須爲此做好準備。 有關此回調的詳細介紹,請參閱 onLoadFinished

請注意,initLoader() 方法將返回已創建的 Loader,但您不必捕獲其引用。LoaderManager 將自動管理加載器的生命週期。LoaderManager 將根據需要啓動和停止加載,並維護加載器的狀態及其相關內容。 這意味着您很少直接與加載器進行交互(有關使用加載器方法調整加載器行爲的示例,請參閱LoaderThrottle 示例)。當特定事件發生時,您通常會使用 LoaderManager.LoaderCallbacks 方法干預加載進程。有關此主題的詳細介紹,請參閱使用 LoaderManager 回調

重啓加載器

當您使用 initLoader() 時(如上所述),它將使用含有指定 ID 的現有加載器(如有)。如果沒有,則它會創建一個。但有時,您想捨棄這些舊數據並重新開始。

要捨棄舊數據,請使用 restartLoader()。例如,當用戶的查詢更改時,此 SearchView.OnQueryTextListener 實現將重啓加載器。 加載器需要重啓,以便它能夠使用修訂後的搜索過濾器執行新查詢:

public boolean onQueryTextChanged(String newText) {
    // Called when the action bar search text has changed.  Update
    // the search filter, and restart the loader to do a new query
    // with this filter.
    mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
    getLoaderManager().restartLoader(0, null, this);
    return true;
}

使用 LoaderManager 回調

LoaderManager.LoaderCallbacks 是一個支持客戶端與 LoaderManager 交互的回調接口。

加載器(特別是 CursorLoader)在停止運行後,仍需保留其數據。這樣,應用即可保留 Activity 或片段的 onStop() 和 onStart() 方法中的數據。當用戶返回應用時,無需等待它重新加載這些數據。您可使用 LoaderManager.LoaderCallbacks 方法瞭解何時創建新加載器,並告知應用何時停止使用加載器的數據。

LoaderManager.LoaderCallbacks 包括以下方法:

  • onLoaderReset():將在先前創建的加載器重置且其數據因此不可用時調用

下文更詳細地描述了這些方法。

onCreateLoader

當您嘗試訪問加載器時(例如,通過 initLoader()),該方法將檢查是否已存在由該 ID 指定的加載器。 如果沒有,它將觸發 LoaderManager.LoaderCallbacks 方法 onCreateLoader()。在此方法中,您可以創建新加載器。 通常,這將是 CursorLoader,但您也可以實現自己的 Loader 子類。

在此示例中,onCreateLoader() 回調方法創建了 CursorLoader。您必須使用其構造函數方法來構建 CursorLoader。該方法需要對 ContentProvider 執行查詢時所需的一系列完整信息。具體地說,它需要:

  • uri:用於檢索內容的 URI
  • projection:要返回的列的列表。傳遞 null 時,將返回所有列,這樣會導致效率低下
  • selection:一種用於聲明要返回哪些行的過濾器,採用 SQL WHERE 子句格式(WHERE 本身除外)。傳遞 null 時,將爲指定的 URI 返回所有行
  • selectionArgs:您可以在 selection 中包含 ?s,它將按照在 selection 中顯示的順序替換爲 selectionArgs 中的值。該值將綁定爲字串符
  • sortOrder:行的排序依據,採用 SQL ORDER BY 子句格式(ORDER BY 自身除外)。傳遞 null 時,將使用默認排序順序(可能並未排序)

例如:

 // 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

當先前創建的加載器完成加載時,將調用此方法。該方法必須在爲此加載器提供的最後一個數據釋放之前調用。 此時,您應移除所有使用的舊數據(因爲它們很快會被釋放),但不要自行釋放這些數據,因爲這些數據歸其加載器所有,其加載器會處理它們。

當加載器發現應用不再使用這些數據時,即會釋放它們。 例如,如果數據是來自 CursorLoader 的一個遊標,則您不應手動對其調用 close()。如果遊標放置在 CursorAdapter 中,則應使用 swapCursor() 方法,使舊 Cursor 不會關閉。例如:

// 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

此方法將在先前創建的加載器重置且其數據因此不可用時調用。 通過此回調,您可以瞭解何時將釋放數據,因而能夠及時移除其引用。  

此實現調用值爲 null 的swapCursor()

// 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);
}

示例


以下是一個 Fragment 完整實現示例。它展示了一個 ListView,其中包含針對聯繫人內容提供程序的查詢結果。它使用 CursorLoader 管理提供程序的查詢。

應用如需訪問用戶聯繫人(如此示例中所示),其清單文件必須包括權限 READ_CONTACTS

public static class CursorLoaderListFragment extends ListFragment
        implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor> {

    // This is the Adapter being used to display the list's data.
    SimpleCursorAdapter mAdapter;

    // If non-null, this is the current filter the user has provided.
    String mCurFilter;

    @Override public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // Give some text to display if there is no data.  In a real
        // application this would come from a resource.
        setEmptyText("No phone numbers");

        // We have a menu item to show in action bar.
        setHasOptionsMenu(true);

        // Create an empty adapter we will use to display the loaded data.
        mAdapter = new SimpleCursorAdapter(getActivity(),
                android.R.layout.simple_list_item_2, null,
                new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS },
                new int[] { android.R.id.text1, android.R.id.text2 }, 0);
        setListAdapter(mAdapter);

        // Prepare the loader.  Either re-connect with an existing one,
        // or start a new one.
        getLoaderManager().initLoader(0, null, this);
    }

    @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Place an action bar item for searching.
        MenuItem item = menu.add("Search");
        item.setIcon(android.R.drawable.ic_menu_search);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        SearchView sv = new SearchView(getActivity());
        sv.setOnQueryTextListener(this);
        item.setActionView(sv);
    }

    public boolean onQueryTextChange(String newText) {
        // Called when the action bar search text has changed.  Update
        // the search filter, and restart the loader to do a new query
        // with this filter.
        mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
        getLoaderManager().restartLoader(0, null, this);
        return true;
    }

    @Override public boolean onQueryTextSubmit(String query) {
        // Don't care about this.
        return true;
    }

    @Override public void onListItemClick(ListView l, View v, int position, long id) {
        // Insert desired behavior here.
        Log.i("FragmentComplexList", "Item clicked: " + id);
    }

    // These are the Contacts rows that we will retrieve.
    static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
        Contacts._ID,
        Contacts.DISPLAY_NAME,
        Contacts.CONTACT_STATUS,
        Contacts.CONTACT_PRESENCE,
        Contacts.PHOTO_ID,
        Contacts.LOOKUP_KEY,
    };
    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");
    }

    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);
    }

    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);
    }
}

更多

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章