提升ListView的运行效率

2017-07-11  本文已影响0人  W_Nicotine
public View getView(int position, View converView, ViewGroup parent){
    Fruit fruit = getItem(position);
    View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
    ImageView fruitImage = (ImageView) view.findViewById(R.id.furit_image);
    TextView fruitName = (TextView) view.findViewById(R.id.furit_name);
    fruitImage.setImageResource(fruit.getImageId());
    fruitName.setText(fruit.getName());
    return view;
}

1 . 以上的ListView的运行效率很低,因为在FruitAdapter的getView()方法中,每次都将布局重新加载一遍,当ListView快速滚动的时候,这就会成为性能的饿瓶颈。

 public View getView(int position, View converView, ViewGroup parent){
     Fruit fruit = getItem(position);
     View view;
     if (converView ==null){
         view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
     } else {
         view = converView;
     }
     ImageView fruitImage = (ImageView) view.findViewById(R.id.furit_image);
     TextView fruitName = (TextView) view.findViewById(R.id.furit_name);
     fruitImage.setImageResource(fruit.getImageId());
     fruitName.setText(fruit.getName());
     return view;
 }```
2 .  如果改为上述,现在我们可以在getView()方法中进行判断,如果cnvertView为null,则使用LayoutInfater去加载布局,如果不为null则直接对converView进行崇从用。这样就大大提高了ListView的运行效率,在快速滚动的时候也可以表现出更好的性能。

















上一篇下一篇

猜你喜欢

热点阅读