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

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;

        }

}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值