• 它们的定义Adapterg在etView( )正在使用View.setTag()与不同的是不使用。


    首先看使用Tag案件。

    @Override
    	public View getView(int position, View view, ViewGroup group) {
    		ViewHolder holder = new ViewHolder();
    		if(view==null){
    			view = inflater.inflate(R.layout.note_list_item, null);//载入列表项的布局文件.
    			holder.title = (TextView)view.findViewById(R.id.note_title);
    			holder.createtime = (TextView)view.findViewById(R.id.note_createtime);
    			holder.clock = (ImageView)view.findViewById(R.id.note_clock);
    			view.setTag(holder);	//将view和holder进行绑定
    		}else{
    			holder = (ViewHolder)view.getTag();
    		}
    		String title = items.get(position);
    		title = title.length()>10 ? title.substring(0, 10)+"..." : title;
    		holder.title.setText(title);
    		holder.createtime.setText(times.get(position));
    	}
    能够看到。本例使用了一个特别的ViewHolder类对组件进行保存。

    private class ViewHolder{
    private TextView title;
    private TextView createtime;
    private ImageView clock;
    }

    再来看下不使用Tag的情况:

     @Override                

    @Override                
    public View getView(int position, View convertView, ViewGroup parent) {                      
      if(convertView == null) {                               
        convertView = mInflater.inflate(R.layout.multiple_checkbox_main_row, null);                      
      }                        
        TextView tN = (TextView) convertView.findViewById(R.id.multiple_title); 
        tN.setText((String)mList.get(position).get(NAME));
        TextView tP = (TextView) convertView.findViewById(R.id.multiple_summary);                       
        tP.setText((String)mList.get(position).get(PHONE_NUMBER));
    }
    能够发现不使用Tag获取View时每次都会调用View.findViewById(XXX)方法。

    而使用Tag的时候因为控件已经保存到了Tag中所以不必再调用View.findViewById(XXX)方法。

    Tag相当于一个缓存的作用。从而提升了程序的性能。

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------分隔线 2014/12/4

    getView()中的參数convertView什么时候才不为null呢。

    经过试验。第一次载入ListView的时候convertView都是为空的。

    仅仅有当ListView中的item载入过之后隐藏(上下滑动在屏幕中隐藏)之后然后再次显示时间,convertView不是null。

    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    解决 git 同步时 Everything up-to-date
    vs2019 git Authentication failed for xxx
    vs2015发布项目到虚拟主机组策略阻止csc.exe程序问题
    vs2017 使用 reportviewer
    var,dynamic的用法
    水晶报表报无法在资源中找到报表,请重新创建项目 错误
    css隐藏元素的方法
    css-浮动与清除浮动的原理详解(清除浮动的原理你知道吗)
    正则并不适合严格查找子串
    浏览器加载、渲染过程总结
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4688975.html
Copyright © 2020-2023  润新知