首页 > 代码库 > 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的性能提升
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。