ListView的优化
private static class ViewHolder{ //新建一个类 private TextView tvHolder; private TextView tvHolder1; private TextView tvHolder2; }
public View getView(int position, View convertView, ViewGroup viewGroup) { //第一个参数是在数据集合中的位置,第二个是旧视图 View view; ViewHolder holder; if(convertView==null) { //如果convertView不为空,说明旧的视图已经产生,可以复用
如果不为空,item并没有充满屏幕,查看屏幕信息也无需滑屏,
这时就需要新建一个view.
view = View.inflate(MainActivity.this, R.layout.activity_item, null); holder=new ViewHolder(); holder.tvHolder=(TextView)view.findViewById(R.id.t1); holder.tvHolder1=(TextView)view.findViewById(R.id.t2); holder.tvHolder2=(TextView)view.findViewById(R.id.t3); view.setTag(holder); //setTag把查找的view缓存起来方便多次重用 }else{ view=convertView; holder=(ViewHolder)view.getTag(); //得到已经缓存的view. } holder.tvHolder.setText(String.valueOf(list_infor.get(position).getId())); holder.tvHolder1.setText(list_infor.get(position).getName()); //list_infor.get(position).getName()指在集合list_infor中position
位置上的Name,然后通过setText()添加到tvHolder1中。
holder.tvHolder2.setText(list_infor.get(position).getNumber());
return view; }