Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android BaseAdapter 完整範例:附 ViewHolder 與 XML 項目版面

編輯:Android 開發實例

以下是一個可直接參考的 BaseAdapter 實作,示範了如何把「最近使用的 App 清單」呈現在 ListView 上,包含資料來源、檢視重用與版面配置三個部分。

Adapter 本體

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 開發中相當重要的分層觀念:版面歸版面,邏輯歸邏輯。

項目版面:app_info_item.xml

用 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() 也能在特定需求下進一步提升重用效率。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved