發現Tag在View中還是很有作用的屬性,API中這樣描述的:
Tags
Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.
Tag不像ID是用標示view的。Tag從本質上來講是就是相關聯的view的額外的信息。它們經常用來存儲一些view的數據,這樣做非常方便而不用存入另外的單獨結構。
下面這個例子是從eoe中看到的,給我很大啟發:
[java]
- public class FileListAdapter extends BaseAdapter {
-
-
-
- private LayoutInflater mInflater;
-
- private Bitmap mIcon_folder;
-
- private Bitmap mIcon_file;
-
- private Bitmap mIcon_image;
-
- private Bitmap mIcon_audio;
-
- private Bitmap mIcon_video;
-
- private Bitmap mIcon_apk;
-
- private List<String> items;
-
- private List<String> paths;
-
- private List<String> sizes;
-
- private int isZoom = 0;
-
-
-
- // MyAdapter的構造器
-
- public FileListAdapter(Context context, List<String> it, List<String> pa,
-
- List<String> si, int zm) {
-
- mInflater = LayoutInflater.from(context);
-
- items = it;
-
- paths = pa;
-
- sizes = si;
-
- isZoom = zm;
-
- mIcon_folder = BitmapFactory.decodeResource(context.getResources(),
-
- R.drawable.folder); // 文件夾的圖標
-
- mIcon_file = BitmapFactory.decodeResource(context.getResources(),
-
- R.drawable.file); // 文件的圖文件
-
- mIcon_image = BitmapFactory.decodeResource(context.getResources(),
-
- R.drawable.image); // 圖片的圖文件
-
- mIcon_audio = BitmapFactory.decodeResource(context.getResources(),
-
- R.drawable.audio); // 音頻的圖文件
-
- mIcon_video = BitmapFactory.decodeResource(context.getResources(),
-
- R.drawable.video); // 視頻的圖文件
-
- mIcon_apk = BitmapFactory.decodeResource(context.getResources(),
-
- R.drawable.apk); // apk文件
-
- }
-
-
-
- @Override
-
- public int getCount() {
-
- return items.size();
-
- }
-
-
-
- @Override
-
- public Object getItem(int position) {
-
- return items.get(position);
-
- }
-
-
-
- @Override
-
- public long getItemId(int position) {
-
- return position;
-
- }
-
-
-
- @Override
-
- public View getView(int position, View convertView, ViewGroup par) {
-
- Bitmap bitMap = null;
-
- ViewHolder holder = null;
-
- if (convertView == null) {
-
- // 使用自定義的list_items作為Layout
-
- convertView = mInflater.inflate(R.layout.file_list_items, null);
-
- // 初始化holder的text與icon
-
- holder = new ViewHolder();
-
- holder.f_title = ((TextView) convertView.findViewById(R.id.f_title));
-
- holder.f_text = ((TextView) convertView.findViewById(R.id.f_text));
-
- holder.f_icon = ((ImageView) convertView.findViewById(R.id.f_icon));
-
- convertView.setTag(holder);
-
- } else {
-
- holder = (ViewHolder) convertView.getTag();
-
- }
-
- File f = new File(paths.get(position).toString());
-
- // 設置文件或文件夾的文字與icon
-
- holder.f_title.setText(f.getName());
-
- String f_type = MyUtil.getMIMEType(f, false);
-
- if (f.isDirectory()) {
-
- holder.f_icon.setImageBitmap(mIcon_folder);
-
- holder.f_text.setText("");
-
- } else {
-
- holder.f_text.setText(sizes.get(position));
-
- if ("image".equals(f_type)) {
-
- if (isZoom == 1) {
-
- bitMap = MyUtil.fitSizePic(f);
-
- if (bitMap != null) {
-
- holder.f_icon.setImageBitmap(bitMap);
-
- } else {
-
- holder.f_icon.setImageBitmap(mIcon_image);
-
- }
-
- } else {
-
- holder.f_icon.setImageBitmap(mIcon_image);
-
- }
-
- bitMap = null;
-
- } else if ("audio".equals(f_type)) {
-
- holder.f_icon.setImageBitmap(mIcon_audio);
-
- } else if ("video".equals(f_type)) {
-
- holder.f_icon.setImageBitmap(mIcon_video);
-
- } else if ("apk".equals(f_type)) {
-
- holder.f_icon.setImageBitmap(mIcon_apk);
-
- } else {
-
- holder.f_icon.setImageBitmap(mIcon_file);
-
- }
-
- }
-
- return convertView;
-
- }
-
-
-
- /**
-
- * 不單獨寫get set可以提高效率 class ViewHolder
-
- * */
-
- private class ViewHolder {
-
- TextView f_title;
-
- TextView f_text;
-
- ImageView f_icon;
-
- }
-
- }