編輯:關於 Android 程式設計
專輯列表用於展示歌曲所屬的專輯,同樣採用 ListView 呈現,並需要定義對應的 Adapter。本文分為 Activity、布局與適配器三部分說明。
AlbumActivity 通過 ContentResolver 查詢 MediaStore 中的音頻數據,先收集所有不重複的專輯名,再建立專輯與對應藝術家的映射,最後綁定 Adapter:
public class AlbumActivity extends Activity {
private String[] albums;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.albums);
setListData();
}
private void setListData() {
listView = (ListView) findViewById(R.id.albums_listview_id);
listView.setCacheColorHint(0x00000000); // 避免滑動時畫面錯亂
Cursor c = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME },
null, null, MediaStore.Audio.Media.ALBUM);
if (c == null || c.getCount() == 0) {
// 存儲列表為空,彈出提示
return;
}
// 用 HashSet 收集不重複的專輯名
c.moveToFirst();
HashSet<String> set = new HashSet<String>();
for (int i = 0, num = c.getCount(); i < num; i++) {
set.add(c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
c.moveToNext();
}
albums = set.toArray(new String[0]);
// 建立「專輯 → 藝術家」映射
Cursor c1 = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM },
null, null, MediaStore.Audio.Media.ALBUM);
HashMap<String, String> map = new HashMap<String, String>();
c1.moveToFirst();
for (int j = 0, num1 = c1.getCount(); j < num1; j++) {
map.put(c1.getString(1), c1.getString(0));
c1.moveToNext();
}
listView.setAdapter(new AlbumListAdapter(this, albums, map));
listView.setOnItemClickListener(new AlbumsItemClickListener());
}
class AlbumsItemClickListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
Intent intent = new Intent();
intent.setClass(AlbumActivity.this, SongsOfAlbumActivity.class);
intent.putExtra("albums", albums[position]); // 傳遞所選專輯名
startActivity(intent);
}
}
}
albums.xml 提供一個 ListView 作為專輯列表容器:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/albums_listview_id"
android:background="@drawable/listbg"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
albumslist.xml 定義單個列表項的樣式,左側為專輯圖標,右側豎排顯示專輯名與藝術家:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/Albumsitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingRight="5dip" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/album"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:id="@+id/mysinger"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:textColor="#ffffff"
android:textSize="13dp" />
</LinearLayout>
</LinearLayout>
AlbumListAdapter 繼承 BaseAdapter,並借助 ViewHolder 模式提升列表滾動性能:
public class AlbumListAdapter extends BaseAdapter {
private Context myContext;
private String[] albums;
private HashMap<String, String> myMap;
public AlbumListAdapter(Context context, String[] str1, HashMap<String, String> map) {
myContext = context;
albums = str1;
myMap = map;
}
public int getCount() { return albums.length; }
public Object getItem(int position) { return position; }
public long getItemId(int position) { return position; }
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View vi = convertView;
if (convertView == null) {
holder = new ViewHolder();
vi = LayoutInflater.from(myContext).inflate(R.layout.albumslist, null);
holder.album = (TextView) vi.findViewById(R.id.album);
holder.artist = (TextView) vi.findViewById(R.id.mysinger);
holder.Albumsitem = (ImageView) vi.findViewById(R.id.Albumsitem);
vi.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// 專輯名過長時截斷顯示
if (albums[position].length() > 15) {
holder.album.setText(albums[position].substring(0, 12) + "...");
} else {
holder.album.setText(albums[position]);
}
// 未知藝術家的處理
if ("sdcard".equals(albums[position])) {
holder.artist.setText("未知藝術家");
} else {
holder.artist.setText(myMap.get(albums[position]));
}
holder.Albumsitem.setImageResource(R.drawable.album);
return vi;
}
class ViewHolder {
public TextView album, artist;
public ImageView Albumsitem;
}
}
整個專輯列表遵循了「查詢媒體庫 → 去重收集專輯 → 建立映射 → 綁定適配器」的思路,並通過 ViewHolder 與 setCacheColorHint 等技巧兼顧了性能與流暢度。點擊某個專輯後,可將專輯名傳入 SongsOfAlbumActivity 以展示該專輯下的歌曲。
Android 平台移植 FFmpeg 踩坑筆記:編譯、畫面變形與 Seek 實作
軟體產業本質上是站在巨人肩膀上往前走的行業,把基礎建設重做一遍往往沒有意義。本文的移植工作同樣以 havlenapetr 的 FFmpeg Android 移植版本為基礎
《Android Studio 實用指南》4.27 使用演示模式
本書目前已上傳至百度閱讀,在百度中搜索「Android Studio 實用指南」即可找到本書。 什麼是演示模式? 顧名思義,當你想向他人演示自己的代碼時,就會用到這個演示
用 Sencha Touch 打包 Android APK 的完整流程
什麼是 Sencha Touch ExtJS 團隊把既有的 ExtJS 與 JQTouch、Raphaël 整合,推出了專為觸控裝置打造的 Sencha Touch 框架
Android 源碼解析(十九):Dialog 的加載與繪制流程
前面兩篇我們拆解了 Activity 的布局加載與繪制流程,對系統界面的顯示機制算是有了整體認識。其實 Android 中所有顯示控件的流程都大同小異——Dialog、P