編輯:Android 開發實例
以下是一個可直接參考的 BaseAdapter 實作,示範了如何把「最近使用的 App 清單」呈現在 ListView 上,包含資料來源、檢視重用與版面配置三個部分。
public class RecentAdapter extends BaseAdapter {
private static class RecentViewHolder {
TextView appName;
ImageView appIcon;
TextView appSize;
}
private List<ResolveInfo> mAppList;
private LayoutInflater mInflater;
private PackageManager pm;
public RecentAdapter(Context c, List<ResolveInfo> appList, PackageManager pm) {
mAppList = appList;
this.pm = pm;
mInflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void clear() {
if (mAppList != null) {
mAppList.clear();
}
}
public void remove(int position) {
mAppList.remove(position);
this.notifyDataSetChanged();
}
@Override
public int getCount() {
return mAppList.size();
}
@Override
public Object getItem(int position) {
return mAppList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RecentViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.app_info_item, null);
holder = new RecentViewHolder();
holder.appName = (TextView) convertView.findViewById(R.id.app_name);
holder.appIcon = (ImageView) convertView.findViewById(R.id.app_icon);
holder.appSize = (TextView) convertView.findViewById(R.id.app_size);
convertView.setTag(holder);
} else {
holder = (RecentViewHolder) convertView.getTag();
}
ResolveInfo appInfo = mAppList.get(position);
if (appInfo != null) {
String labelName = appInfo.loadLabel(pm).toString();
if (labelName != null) {
holder.appName.setText(labelName);
}
Drawable icon = appInfo.loadIcon(pm);
if (icon != null) {
holder.appIcon.setImageDrawable(icon);
}
}
return convertView;
}
}
1. setTag() 用來快取檢視參考
第一次建立項目時,把子檢視打包進 ViewHolder 並用 setTag() 掛在 convertView 上;後續重用時用 getTag() 直接取出。這樣就不必每次都執行 findViewById()——而 findViewById 是整份 getView 裡最耗時的動作。
2. notifyDataSetChanged() 告訴 View 資料已變
任何資料變更(如 remove())之後都要呼叫它,否則畫面不會更新。這也是前面提到的「adapter has changed」錯誤的正解之一。
3. inflate() 載入 XML 項目版面
View convertView = mInflater.inflate(R.layout.app_info_item, null);
把項目的視覺結構交給 XML 描述,程式碼只負責填資料。這是 Android 開發中相當重要的分層觀念:版面歸版面,邏輯歸邏輯。
用 RelativeLayout 做「左圖右文、右下角放額外資訊」的排版:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight">
<ImageView android:id="@+id/app_icon"
android:layout_width="@android:dimen/app_icon_size"
android:layout_height="@android:dimen/app_icon_size"
android:layout_alignParentLeft="true"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:paddingBottom="6dip"
android:scaleType="fitCenter" />
<TextView android:id="@+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="?android:attr/textColorPrimary"
android:layout_toRightOf="@id/app_icon"
android:paddingLeft="6dip"
android:paddingTop="6dip" />
<TextView android:id="@+id/app_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_below="@+id/app_name"
android:layout_toRightOf="@id/app_icon"
android:paddingLeft="6dip"
android:paddingBottom="6dip" />
<TextView android:id="@+id/app_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_alignParentRight="true"
android:layout_below="@+id/app_name"
android:paddingRight="6dip"
android:maxLines="1" />
</RelativeLayout>
幾個細節值得注意:用 ?android:attr/listPreferredItemHeight 設定 minHeight,能讓項目高度與系統其他清單保持一致;用 ?android:attr/textAppearanceLarge 這類主題屬性而非寫死字級,則能自動貼合使用者選擇的字型大小偏好;app_size 加上 android:maxLines="1",避免過長的數字把版面撐開。
若清單項目很多,loadLabel() 與 loadIcon() 這類 PackageManager 呼叫其實不便宜。實務上可以考慮把結果快取一份,或改用非同步載入圖示,讓滾動更順。另外,getItemViewType() 與 getViewTypeCount() 也能在特定需求下進一步提升重用效率。
手把手做 Android 登入頁:表單、驗證與錯誤嘗試的處理
登入畫面是許多 App 的第一道門。使用者要嘛成功進入,要嘛被擋在門外——而這中間的體驗細節,往往決定了對這個 App 的第一印象。本文從版面配置寫到程式邏輯,做出一個具
Android SurfaceView 學習示例:為什麼遊戲和相機預覽都用它
做遊戲渲染、相機預覽或者高頻重繪的動畫時,普通 View 往往力不從心,這時候就輪到 SurfaceView 上場了。本文從它的特性講起,並附一個可以直接跑起來的示例。
Android 五種資料儲存方式全解析:SharedPreferences、檔案、SQLite、ContentProvider 與網路
資料儲存是開發中最頻繁面對的課題之一。Android 提供了多種互補的機制,本文逐一說明五種主要方式,並附上各自的關鍵程式碼與適用情境。 SharedPreferen
Android 進度 Spinner 使用指南:從 XML 到 Java 控制
Android 的載入 Spinner 常用於「不知道還要等多久」的情境。它不需要精確百分比,只用旋轉動畫提醒使用者目前有任務正在進行。 加入元件 在 XML 中加入 P