Android適配器之——BaseAdapter(例子)

來源:互聯網
上載者:User
  1. public
    class RecentAdapter
    extends BaseAdapter {
  2.  
  3.     private
    class RecentViewHolder { 
  4.         TextView appName; 
  5. ImageView appIcon;
  6.         TextView appSize; 
  7.     } 
  8.  
  9.     private List<ResolveInfo> mAppList; 
  10.     private LayoutInflater mInflater; 
  11.     private PackageManager pm; 
  12.  
  13.     public RecentAdapter(Context c, List<ResolveInfo> appList, 
  14.             PackageManager pm) { 
  15.         mAppList = appList; 
  16.         this.pm = pm; 
  17.         mInflater = (LayoutInflater) c 
  18.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  19.     } 
  20.      
  21.     public
    void clear(){ 
  22.         if(mAppList!=null){ 
  23.             mAppList.clear(); 
  24.         } 
  25.     } 
  26.  
  27.     public
    int getCount() { 
  28.         return mAppList.size(); 
  29.     } 
  30.  
  31.     @Override 
  32.     public Object getItem(int position) { 
  33.         return mAppList.get(position); 
  34.     } 
  35.  
  36.     @Override 
  37.     public
    long getItemId(int position) { 
  38.         // TODO Auto-generated method stub 
  39.         return position; 
  40.     } 
  41.  
  42.     public View getView(int position, View convertView, ViewGroup
    parent) { 
  43.         RecentViewHolder holder; 
  44.         if (convertView ==
    null) { 
  45.             convertView = mInflater.inflate(R.layout.app_info_item,
    null); 
  46.             holder = new RecentViewHolder(); 
  47.             holder.appName = (TextView) convertView.findViewById(R.id.app_name); 
  48.             holder.appIcon = (ImageView) convertView 
  49.                     .findViewById(R.id.app_icon); 
  50.             holder.appSize = (TextView) convertView.findViewById(R.id.app_size); 
  51.             convertView.setTag(holder); 
  52.         } else
  53.             holder = (RecentViewHolder) convertView.getTag(); 
  54.         } 
  55.         ResolveInfo appInfo = mAppList.get(position); 
  56.         if (appInfo !=
    null) { 
  57.             String labelName = appInfo.loadLabel(pm).toString(); 
  58.             if (labelName !=
    null) { 
  59.                 holder.appName.setText(labelName); 
  60.             } 
  61.  
  62.             Drawable icon = appInfo.loadIcon(pm); 
  63.             if (icon !=
    null) { 
  64.                 holder.appIcon.setImageDrawable(icon); 
  65.             } 
  66.         } 
  67.         return convertView; 
  68.     } 
  69.      
  70.     public
    void remove(int position){ 
  71.         mAppList.remove(position); 
  72.         this.notifyDataSetChanged(); 
  73.     } 
  74.  

其中兩個注意點為:

setTag 用View設定儲存資料

notifyDataSetChanged() 告訴View資料更改並重新整理

View convertView = mInflater.inflate(R.layout.app_info_item, null)  載入XML Item 示圖

app_info_item.xml檔案樣本

 

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  4.     android:layout_width="fill_parent" android:layout_height="wrap_content" 
  5.     android:layout_gravity="center_vertical" android:minHeight="?android:attr/listPreferredItemHeight"> 
  6.  
  7.     <ImageView android:id="@+id/app_icon" android:layout_width="@android:dimen/app_icon_size" 
  8.         android:layout_height="@android:dimen/app_icon_size" 
  9.         android:layout_alignParentLeft="true" android:paddingLeft="6dip" 
  10.         android:paddingTop="6dip" android:paddingBottom="6dip" 
  11.         android:scaleType="fitCenter" /> 
  12.  
  13.     <TextView android:id="@+id/app_name" android:layout_width="wrap_content" 
  14.         android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" 
  15.         android:textColor="?android:attr/textColorPrimary" 
  16.         android:layout_toRightOf="@id/app_icon" android:paddingLeft="6dip" 
  17.         android:paddingTop="6dip" /> 
  18.  
  19.     <TextView android:id="@+id/app_description" 
  20.         android:layout_width="wrap_content" android:layout_height="wrap_content" 
  21.         android:textAppearance="?android:attr/textAppearanceSmall" 
  22.         android:layout_below="@+id/app_name" android:layout_toRightOf="@id/app_icon" 
  23.         android:paddingLeft="6dip" android:paddingBottom="6dip" /> 
  24.     <TextView android:id="@+id/app_size" android:layout_width="wrap_content" 
  25.         android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" 
  26.         android:layout_alignParentRight="true" android:layout_below="@+id/app_name" 
  27.         android:paddingRight="6dip" android:maxLines="1" /> 
  28.  
  29. </RelativeLayout> 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.