首页 > 代码库 > ListView的性能提升

ListView的性能提升

1 public class Fruite{2     private String name;3     private int imageId;4     public Fruit(String name,int imageId){5         this.name = name;6         this.imageId = imageId;7     }8 }
 1 public class FruitAdapter extends ArrayAdapter<Fruit>{ 2     private int resourceId; 3     public FruitAdapter(Context context,int textViewresourceId, 4                                         List<Fruit> objects){ 5                 super(context,textViewResourceId,objects); 6                 resourceId = textViewResourceId; 7     } 8      9 public View getView(int position, View convertView, ViewGroup parent) {10     Fruit fruit = getItem(position);11     View view;12     ViewHolder viewHolder;13     if (convertView == null) {14     view = LayoutInflater.from(getContext()).inflate(resourceId,     null);15     viewHolder = new ViewHolder();16     viewHolder.fruitImage = (ImageView) view.findViewById17 (R.id.fruit_image);18     viewHolder.fruitName = (TextView) view.findViewById19 (R.id.fruit_name);20     view.setTag(viewHolder); // 将ViewHolder存储在View中21 } else {22     view = convertView;23     viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder24 }25             viewHolder.fruitImage.setImageResource(fruit.getImageId());26     viewHolder.fruitName.setText(fruit.getName());27     return view;     28 }                   
1 class ViewHolder {2 ImageView fruitImage;3 TextView fruitName;4 }

 

ListView的性能提升