码迷,mamicode.com
首页 > 其他好文 > 详细

ListView的性能提升

时间:2015-01-15 10:39:03      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

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.findViewById
17 (R.id.fruit_image);
18     viewHolder.fruitName = (TextView) view.findViewById
19 (R.id.fruit_name);
20     view.setTag(viewHolder); // 将ViewHolder存储在View中
21 } else {
22     view = convertView;
23     viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder
24 }
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的性能提升

标签:

原文地址:http://www.cnblogs.com/plmmlp09/p/4225462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!