Loader是什么,有什么作用?
顧名思義就是加載器,簡單來說,Loader做了2件事:
(1)在單獨的線程中讀取數(shù)據(jù),不會阻塞UI線程
(2)監(jiān)視數(shù)據(jù)的更新
LoaderManager是什么,有什么作用?
LoaderManager就是加載器的管理器,一個LoaderManager可以管理一個或多個Loader,一個Activity或者Fragment只能有一個LoadManager。LoaderManager管理Loader的初始化,重啟和銷毀操作。
使用Loader來加載手機中的音樂為例
1、主布局,就是一個ListView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d9d9d9"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/music_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
</RelativeLayout>
2、ListView的Item布局,主要顯示歌曲名稱和歌手信息
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/music_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:id="@+id/music_singer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
3、MainActivity,注釋比較詳細(xì),使用比較簡單
/**
* 實現(xiàn)LoaderCallbacks 重寫三個方法
*
* @author yungfan
*
*/
@SuppressLint("NewApi")
public class MainActivity extends Activity implements LoaderCallbacks<Cursor> {
private ListView listView;
// 使用SimpleCursorAdapter來填充數(shù)據(jù)
private SimpleCursorAdapter mAdapter;
// 使用CursorLoader來獲取數(shù)據(jù)
private CursorLoader loader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.music_list);
initMusic();
}
private void initMusic() {
// 這里創(chuàng)建Adapter時 注意不傳遞數(shù)據(jù)
mAdapter = new SimpleCursorAdapter(MainActivity.this, R.layout.item,
null, new String[] { MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST }, new int[] {
R.id.music_name, R.id.music_singer }, 0);
listView.setAdapter(mAdapter);
// 通過異步的方式加載數(shù)據(jù)
LoaderManager manager = getLoaderManager();
// 第一個參數(shù)為id 第二個位Bundle數(shù)據(jù) 第三個為LoaderCallbacks
manager.initLoader(0, null, this);
}
// 首先檢查指定的id是否存在,如果不存在才會觸發(fā)該方法,通過該方法才能創(chuàng)建一個loader。
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// 查詢音樂數(shù)據(jù)庫 獲取音樂數(shù)據(jù) 并排序
loader = new CursorLoader(this,
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
return loader;
}
// 完成對Ui控件的更新,如果不再使用,將自動釋放loader的數(shù)據(jù),不需要使用close();
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
4、運行結(jié)果,手機中的音樂數(shù)據(jù)被加載到列表中顯示出來,而且是按照一定的順序(數(shù)字 —>漢字拼音對應(yīng)字母升序 —> 字母升序)

music.jpg
注意:必須加上讀SD卡的權(quán)限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>