在获得布局填充view的时候我们一般用到的是View.inflate()方法,我们每滑动一次getview就要执行一次同样,也要填充一次布局,
listview只能显示屏幕那么多的内容,而不用的不会马上销毁而是放到缓存里,这样我们就可以利用这点来进行优化,当有缓存的时候我
们就要用到缓存,不用重新调用View.inflate()
代码实现
// 返回视图,这个方法很主要 public View getView(int position, View arg, ViewGroup arg2) { View view = null;
//判断是否有缓存,如果没有填充布局,有的话利用缓存。不管是缓存还是新获取的都要重新设置内容
if(arg==null){ view = View.inflate(context, R.layout.lisviewt_item1, null); }else{ view = arg; } FriendBean fb = data.get(position); ImageView img = (ImageView) view.findViewById(R.id.iv); TextView name = (TextView) view.findViewById(R.id.tv_1); TextView gexing = (TextView) view.findViewById(R.id.tv_2); img.setImageResource(fb.getImgID()); name.setText(fb.getName()); gexing.setText(fb.getGexing()); return view; }
private class AppAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return appMessage.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int position, View arg1, ViewGroup arg2) { View v = null; ViewHolder viewHolder; if (arg1 == null) { v = View.inflate(AppManagerActivity.this, R.layout.item_app_show, null); viewHolder = new ViewHolder(); viewHolder.tv_name=(TextView) v.findViewById(R.id.app_item_tv_name); viewHolder.tv_location = (TextView) v.findViewById(R.id.app_item_tv_location); viewHolder.img_icon = (ImageView) v.findViewById(R.id.app_item_img_icon); //对应关系一致 v.setTag(viewHolder); } else { v = arg1; viewHolder = (ViewHolder) v.getTag(); } AppInfo app = appMessage.get(position); viewHolder.img_icon.setImageDrawable(app.getIcon()); viewHolder.tv_name.setText(app.getName()); if(app.isRow()){ viewHolder.tv_location.setText("内部存储程序"); }else{ viewHolder.tv_location.setText("外部存储程序"); } return v; } } static class ViewHolder { ImageView img_icon; TextView tv_name; TextView tv_location; }