IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]View Tag 的使用

    lincyang发表于 2011-07-21 17:07:56
    love 0

    发现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中看到的,给我很大启发:

    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;
    
            }
    
    }
    


     

     



沪ICP备19023445号-2号
友情链接