Loader是什么疏遏,有什么作用?
顧名思義就是加載器救军,簡(jiǎn)單來(lái)說(shuō)财异,Loader做了2件事:
(1)在單獨(dú)的線程中讀取數(shù)據(jù),不會(huì)阻塞UI線程
(2)監(jiān)視數(shù)據(jù)的更新
LoaderManager是什么唱遭,有什么作用戳寸?
LoaderManager就是加載器的管理器,一個(gè)LoaderManager可以管理一個(gè)或多個(gè)Loader拷泽,一個(gè)Activity或者Fragment只能有一個(gè)LoadManager庆揩。LoaderManager管理Loader的初始化俐东,重啟和銷毀操作。
使用Loader來(lái)加載手機(jī)中的音樂為例
1订晌、主布局虏辫,就是一個(gè)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ì)奕枢,使用比較簡(jiǎn)單
/**
* 實(shí)現(xiàn)LoaderCallbacks 重寫三個(gè)方法
*
* @author yungfan
*
*/
@SuppressLint("NewApi")
public class MainActivity extends Activity implements LoaderCallbacks<Cursor> {
private ListView listView;
// 使用SimpleCursorAdapter來(lái)填充數(shù)據(jù)
private SimpleCursorAdapter mAdapter;
// 使用CursorLoader來(lái)獲取數(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í) 注意不傳遞數(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();
// 第一個(gè)參數(shù)為id 第二個(gè)位Bundle數(shù)據(jù) 第三個(gè)為L(zhǎng)oaderCallbacks
manager.initLoader(0, null, this);
}
// 首先檢查指定的id是否存在娄昆,如果不存在才會(huì)觸發(fā)該方法,通過該方法才能創(chuàng)建一個(gè)loader缝彬。
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// 查詢音樂數(shù)據(jù)庫(kù) 獲取音樂數(shù)據(jù) 并排序
loader = new CursorLoader(this,
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
return loader;
}
// 完成對(duì)Ui控件的更新萌焰,如果不再使用,將自動(dòng)釋放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扒俯、運(yùn)行結(jié)果,手機(jī)中的音樂數(shù)據(jù)被加載到列表中顯示出來(lái)一疯,而且是按照一定的順序(數(shù)字 —>漢字拼音對(duì)應(yīng)字母升序 —> 字母升序)
music.jpg
注意:必須加上讀SD卡的權(quán)限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>