情境:
我想顯示sd卡的/DCIM/Camera目錄中的所有圖片到一個列表中,這個列表中的每一項左邊是圖片的縮圖,右邊是檔案名稱。
分析:
這個應用是很典型的 資料繫結 加 自訂顯示.
如何搞定?
1. 自訂欄表樣式
image_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <ImageView android:id="@+id/item_thumbnail" android:layout_height="48dip" android:layout_width="48dip" /> <TextView android:id="@+id/item_file_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:minHeight="?android:attr/listPreferredItemHeight" android:gravity="center_vertical" android:paddingLeft="5dip" /></LinearLayout>
2. 自訂ArrayAdapter
ImageListAdapter
public class ImageListAdapter extends ArrayAdapter<File>{ private int resource; public ImageListAdapter(Context context, int resourceId, List<File> objects) { super(context, resourceId, objects); // 記錄下來稍後使用 resource = resourceId; } public View getView(int position, View convertView, ViewGroup parent) { LinearLayout imageListView; // 擷取資料 File file = getItem(position); String fileName = file.getName(); Bitmap bitmap = getBitmapFromFile(file); // 系統顯示列表時,首先執行個體化一個適配器(這裡將執行個體化自訂的適配器)。 // 當手動完成適配時,必須手動映射資料,這需要重寫getView()方法。 // 系統在繪製列表的每一行的時候將調用此方法。 // getView()有三個參數, // position表示將顯示的是第幾行, // covertView是從布局檔案中inflate來的布局。 // 我們用LayoutInflater的方法將定義好的image_item.xml檔案提取成View執行個體用來顯示。 // 然後將xml檔案中的各個組件執行個體化(簡單的findViewById()方法)。 // 這樣便可以將資料對應到各個組件上了。 // if(convertView == null) { imageListView = new LinearLayout(getContext()); // 看一下android文檔中關於LayoutInflater的定義吧 // This class is used to instantiate layout XML file into its corresponding View objects. // It is never be used directly -- use getLayoutInflater() or getSystemService(String) // to retrieve a standard LayoutInflater instance that is already hooked up to the current // context and correctly configured for the device you are running on. . For example: String inflater = Context.LAYOUT_INFLATER_SERVICE; LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater); vi.inflate(resource, imageListView, true); // 這個每次select images時都被調用 Log.d("Adapter", "convertView is null now"); } else { // 很奇怪基本沒被調用過 imageListView = (LinearLayout)convertView; Log.d("Adapter", "convertView is not null now"); } // 填充自訂資料 ImageView imageView = (ImageView) imageListView.findViewById(R.id.item_thumbnail); TextView textView = (TextView) imageListView.findViewById(R.id.item_file_name); textView.setText(fileName); imageView.setImageBitmap(bitmap); return imageListView; } // 從檔案擷取Bitmap用於填充 private Bitmap getBitmapFromFile(File file) { Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); return bitmap; } }
3. 綁定資料
private void bindFilesToList(File[] files) { List<File> fileList = new ArrayList<File>(); for(File file : files) { fileList.add(file); } ImageListAdapter adapter = new ImageListAdapter(ImageFilesListActivity.this, R.layout.image_item, fileList); setListAdapter(adapter); }
基本就是這樣了