现在的位置: 首页 > 综合 > 正文

Android中ListView的性能问题

2017年12月05日 ⁄ 综合 ⁄ 共 1847字 ⁄ 字号 评论关闭

如何将你的Android使用界面更快和更高效益

提倡大家采用ViewHolder模式,效果很明显哦

再去熟悉一下异步加载网络图片,双缓存机制

Android自带的ListView只能满足初步的列表显示需求。如果想要在列表项中添加图片等控件,就需要一个适配器(Adapter)。此时需要重写Adapter的getView方法,这个方法是整个列表的主要计算消耗。写得不好会影响列表的性能,而且往往会成为性能瓶颈。

  private LayoutInflater mInflater =
      (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  public View getView(int position, View convertView, ViewGroup parent) {
      convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
      TextView text = (TextView) convertView.findViewById(R.id.text);
      ImageView icon  = (ImageView) convertView.findViewById(R.id.icon);
      text.setText(textData[position]);
      icon.setImageBitmap(iconData[position]);
      return convertView;
  }

  如以上代码所示,列表需要展现列表项的时候,就会调用getView方法。其中position就是列表项的位置,convertView为列表项当前使用的视图对象。开发者只需根据position来决定如何填充一个视图,并返回给适配器就行了。以上代码重用了列表项中的视图对象,但是这样做性能还是很差。

  性能提升:
  Google I/O大会上给出的方案,确实能提升性能。将方法中调用findViewById得到的控件对象存起来,避免每次创建新视图对象时都要调用inflate和findViewById,从而减少运行时间。
  代码如下:

  private LayoutInflater mInflater =
      (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder; // 该容器用来存放控件对象
      if (convertView == null) {
          convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
          holder = new ViewHolder();
          holder.text = (TextView) convertView.findViewById(R.id.text);
          holder.icon = (ImageView) convertView.findViewById(R.id.icon);
          convertView.setTag(holder); // 将存放对象的容器存进视图对象中
      } else {
          holder = (ViewHolder) convertView.getTag(); // 直接拿出控件对象
      }
      text.setText(textData[position]);
      icon.setImageBitmap(iconData[position]);
      return convertView;
  }
  static class ViewHolder {
      TextView text;
      ImageView icon;
  }

  不过这样写的条件是每个列表项里面的控件都是一样的,如果列表项的控件不是一样的,如列表中需要使用某些列表项来做分割(快速记账本(QuickCharge)中通过在某些列表项中填充日期来分割不同日期的账目),这种方法的性能提升就不那么明显了(还是可以使用)。

抱歉!评论已关闭.