返回列表 發帖

Android App - Effective List Adapter

Effective List Adaptor 是指在清單中的每一個單項包含文字及圖片


建立新程式, package 名為 sample.effectiveAdaptor, 在 EffectiveAdapterActivity.java 檔案加入
  1. package sample.effectiveAdaptor;

  2. import android.app.ListActivity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.TextView;
  10. import android.widget.ImageView;
  11. import android.graphics.BitmapFactory;
  12. import android.graphics.Bitmap;

  13. /**
  14. * Demonstrates how to write an efficient list adapter. The adapter used in this example binds
  15. * to an ImageView and to a TextView for each row in the list.
  16. *
  17. * To work efficiently the adapter implemented here uses two techniques:
  18. * - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary
  19. * - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary
  20. *
  21. * The ViewHolder pattern consists in storing a data structure in the tag of the view returned by
  22. * getView(). This data structures contains references to the views we want to bind data to, thus
  23. * avoiding calls to findViewById() every time getView() is invoked.
  24. */
  25. public class EffectiveAdapterActivity extends ListActivity {

  26.     private static class EfficientAdapter extends BaseAdapter {
  27.         private LayoutInflater mInflater;
  28.         private Bitmap mIcon1;
  29.         private Bitmap mIcon2;

  30.         public EfficientAdapter(Context context) {
  31.             // Cache the LayoutInflate to avoid asking for a new one each time.
  32.             mInflater = LayoutInflater.from(context);

  33.             // Icons bound to the rows.
  34.             mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1);
  35.             mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2);
  36.         }

  37.         /**
  38.          * The number of items in the list is determined by the number of speeches
  39.          * in our array.
  40.          *
  41.          * @see android.widget.ListAdapter#getCount()
  42.          */
  43.         public int getCount() {
  44.             return DATA.length;
  45.         }

  46.         /**
  47.          * Since the data comes from an array, just returning the index is
  48.          * sufficent to get at the data. If we were using a more complex data
  49.          * structure, we would return whatever object represents one row in the
  50.          * list.
  51.          *
  52.          * @see android.widget.ListAdapter#getItem(int)
  53.          */
  54.         public Object getItem(int position) {
  55.             return position;
  56.         }

  57.         /**
  58.          * Use the array index as a unique id.
  59.          *
  60.          * @see android.widget.ListAdapter#getItemId(int)
  61.          */
  62.         public long getItemId(int position) {
  63.             return position;
  64.         }

  65.         /**
  66.          * Make a view to hold each row.
  67.          *
  68.          * @see android.widget.ListAdapter#getView(int, android.view.View,
  69.          *      android.view.ViewGroup)
  70.          */
  71.         public View getView(int position, View convertView, ViewGroup parent) {
  72.             // A ViewHolder keeps references to children views to avoid unnecessary calls
  73.             // to findViewById() on each row.
  74.             ViewHolder holder;

  75.             // When convertView is not null, we can reuse it directly, there is no need
  76.             // to reinflate it. We only inflate a new View when the convertView supplied
  77.             // by ListView is null.
  78.             if (convertView == null) {
  79.                 convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

  80.                 // Creates a ViewHolder and store references to the two children views
  81.                 // we want to bind data to.
  82.                 holder = new ViewHolder();
  83.                 holder.text = (TextView) convertView.findViewById(R.id.text);
  84.                 holder.icon = (ImageView) convertView.findViewById(R.id.icon);

  85.                 convertView.setTag(holder);
  86.             } else {
  87.                 // Get the ViewHolder back to get fast access to the TextView
  88.                 // and the ImageView.
  89.                 holder = (ViewHolder) convertView.getTag();
  90.             }

  91.             // Bind the data efficiently with the holder.
  92.             holder.text.setText(DATA[position]);
  93.             holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

  94.             return convertView;
  95.         }

  96.         static class ViewHolder {
  97.             TextView text;
  98.             ImageView icon;
  99.         }
  100.     }

  101.     @Override
  102.     public void onCreate(Bundle savedInstanceState) {
  103.         super.onCreate(savedInstanceState);
  104.         setListAdapter(new EfficientAdapter(this));
  105.     }

  106.     private static final String[] DATA = new String[] {
  107.        "明治 青提朱古力橡皮糖96克", "萬樂珠 薄荷味37.5克",
  108.        "瑞士糖 黑加侖味條裝49克", "瑞士糖 草莓口味條裝49克",
  109.        "益達 香橙薄荷味袋裝28克", "爽浪 無糖香口珠 橙薑味28克",
  110.        "爽浪 無糖香口珠 檸蜜味28克", "爽浪 無糖香口珠 西柚薄味28克",
  111.        "爽浪無糖香口珠 超涼薄荷味28克", "珍寶珠 迷你珍寶珠 5支裝 30克",
  112.        "利口樂潤喉糖 檸檬香草50克", "利口樂潤喉糖 接骨木花香草50克",
  113.        "利口樂潤喉糖 黑加侖子香草50克", "得力素 檸檬糖10pc"
  114.      };
  115. }
複製代碼


在 list_item_icon_text.xml 加入以下內容:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="horizontal"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent">

  6.     <ImageView android:id="@+id/icon"
  7.         android:layout_width="48dip"
  8.         android:layout_height="48dip" />

  9.     <TextView android:id="@+id/text"
  10.         android:layout_gravity="center_vertical"
  11.         android:layout_width="0dip"
  12.         android:layout_weight="1.0"
  13.         android:layout_height="wrap_content" />

  14. </LinearLayout>
複製代碼

再在 res 建立新資料夾
drawable , 把自己創迼的 icon48x48_1.png 及 icon48x48_1.png 加入
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊
Bill Tang     MSN:billtang@openplatform.com.hk
Openplatform Technology Co.,Ltd. 資訊坊科技有限公司  
無線工程施工、方案設計、無線產品、天饋材料、終端設備綜合供應商
Tel: 852-27491011  Fax: 852-81483532

返回列表