Android开发教程:View Tag 的使用


发现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]
  1. public class FileListAdapter extends BaseAdapter {  
  2.   
  3.   
  4.   
  5.         private LayoutInflater mInflater;  
  6.   
  7.         private Bitmap mIcon_folder;  
  8.   
  9.         private Bitmap mIcon_file;  
  10.   
  11.         private Bitmap mIcon_image;  
  12.   
  13.         private Bitmap mIcon_audio;  
  14.   
  15.         private Bitmap mIcon_video;  
  16.   
  17.         private Bitmap mIcon_apk;  
  18.   
  19.         private List<String> items;  
  20.   
  21.         private List<String> paths;  
  22.   
  23.         private List<String> sizes;  
  24.   
  25.         private int isZoom = 0;  
  26.   
  27.   
  28.   
  29.         // MyAdapter的构造器   
  30.   
  31.         public FileListAdapter(Context context, List<String> it, List<String> pa,  
  32.   
  33.                         List<String> si, int zm) {  
  34.   
  35.                 mInflater = LayoutInflater.from(context);  
  36.   
  37.                 items = it;  
  38.   
  39.                 paths = pa;  
  40.   
  41.                 sizes = si;  
  42.   
  43.                 isZoom = zm;  
  44.   
  45.                 mIcon_folder = BitmapFactory.decodeResource(context.getResources(),  
  46.   
  47.                                 R.drawable.folder); // 文件夹的图标   
  48.   
  49.                 mIcon_file = BitmapFactory.decodeResource(context.getResources(),  
  50.   
  51.                                 R.drawable.file); // 文件的图文件   
  52.   
  53.                 mIcon_image = BitmapFactory.decodeResource(context.getResources(),  
  54.   
  55.                                 R.drawable.image); // 图片的图文件   
  56.   
  57.                 mIcon_audio = BitmapFactory.decodeResource(context.getResources(),  
  58.   
  59.                                 R.drawable.audio); // 音频的图文件   
  60.   
  61.                 mIcon_video = BitmapFactory.decodeResource(context.getResources(),  
  62.   
  63.                                 R.drawable.video); // 视频的图文件   
  64.   
  65.                 mIcon_apk = BitmapFactory.decodeResource(context.getResources(),  
  66.   
  67.                                 R.drawable.apk); // apk文件   
  68.   
  69.         }  
  70.   
  71.   
  72.   
  73.         @Override  
  74.   
  75.         public int getCount() {  
  76.   
  77.                 return items.size();  
  78.   
  79.         }  
  80.   
  81.   
  82.   
  83.         @Override  
  84.   
  85.         public Object getItem(int position) {  
  86.   
  87.                 return items.get(position);  
  88.   
  89.         }  
  90.   
  91.   
  92.   
  93.         @Override  
  94.   
  95.         public long getItemId(int position) {  
  96.   
  97.                 return position;  
  98.   
  99.         }  
  100.   
  101.   
  102.   
  103.         @Override  
  104.   
  105.         public View getView(int position, View convertView, ViewGroup par) {  
  106.   
  107.                 Bitmap bitMap = null;  
  108.   
  109.                 ViewHolder holder = null;  
  110.   
  111.                 if (convertView == null) {  
  112.   
  113.                         // 使用自定义的list_items作为Layout   
  114.   
  115.                         convertView = mInflater.inflate(R.layout.file_list_items, null);  
  116.   
  117.                         // 初始化holder的text与icon   
  118.   
  119.                         holder = new ViewHolder();  
  120.   
  121.                         holder.f_title = ((TextView) convertView.findViewById(R.id.f_title));  
  122.   
  123.                         holder.f_text = ((TextView) convertView.findViewById(R.id.f_text));  
  124.   
  125.                         holder.f_icon = ((ImageView) convertView.findViewById(R.id.f_icon));  
  126.   
  127.                         convertView.setTag(holder);  
  128.   
  129.                 } else {  
  130.   
  131.                         holder = (ViewHolder) convertView.getTag();  
  132.   
  133.                 }  
  134.   
  135.                 File f = new File(paths.get(position).toString());  
  136.   
  137.                 // 设置文件或文件夹的文字与icon   
  138.   
  139.                 holder.f_title.setText(f.getName());  
  140.   
  141.                 String f_type = MyUtil.getMIMEType(f, false);  
  142.   
  143.                 if (f.isDirectory()) {  
  144.   
  145.                         holder.f_icon.setImageBitmap(mIcon_folder);  
  146.   
  147.                         holder.f_text.setText("");  
  148.   
  149.                 } else {  
  150.   
  151.                         holder.f_text.setText(sizes.get(position));  
  152.   
  153.                         if ("image".equals(f_type)) {  
  154.   
  155.                                 if (isZoom == 1) {  
  156.   
  157.                                         bitMap = MyUtil.fitSizePic(f);  
  158.   
  159.                                         if (bitMap != null) {  
  160.   
  161.                                                 holder.f_icon.setImageBitmap(bitMap);  
  162.   
  163.                                         } else {  
  164.   
  165.                                                 holder.f_icon.setImageBitmap(mIcon_image);  
  166.   
  167.                                         }  
  168.   
  169.                                 } else {  
  170.   
  171.                                         holder.f_icon.setImageBitmap(mIcon_image);  
  172.   
  173.                                 }  
  174.   
  175.                                 bitMap = null;  
  176.   
  177.                         } else if ("audio".equals(f_type)) {  
  178.   
  179.                                 holder.f_icon.setImageBitmap(mIcon_audio);  
  180.   
  181.                         } else if ("video".equals(f_type)) {  
  182.   
  183.                                 holder.f_icon.setImageBitmap(mIcon_video);  
  184.   
  185.                         } else if ("apk".equals(f_type)) {  
  186.   
  187.                                 holder.f_icon.setImageBitmap(mIcon_apk);  
  188.   
  189.                         } else {  
  190.   
  191.                                 holder.f_icon.setImageBitmap(mIcon_file);  
  192.   
  193.                         }  
  194.   
  195.                 }  
  196.   
  197.                 return convertView;  
  198.   
  199.         }  
  200.   
  201.   
  202.   
  203.         /** 
  204.  
  205.          * 不单独写get set可以提高效率 class ViewHolder 
  206.  
  207.          * */  
  208.   
  209.         private class ViewHolder {  
  210.   
  211.                 TextView f_title;  
  212.   
  213.                 TextView f_text;  
  214.   
  215.                 ImageView f_icon;  
  216.   
  217.         }  
  218.   
  219. }  

相关内容