展会信息港展会大全

Android中ListView的性能问题
来源:互联网   发布日期:2015-11-27 13:42:12   浏览:1145次  

导读:Android自带的ListView只能满足初步的列表显示需求。如果想要在列表项中添加图片等控件,就需要一个适配器(Adapter)。此时需要重写Adapter的get...

Android自带的ListView只能满足初步的列表显示需求。

如果想要在列表项中添加图片等控件,就需要一个适配器(Adapter)。

此时需要重写Adapter的getView方法,这个方法是整个列表的主要计算消耗。

写得不好会影响列表的性能,而且往往会成为性能瓶颈。

</pre><p style="font-size:13px; line-height:1.4; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif"></p><pre name="code" class="java">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)中通过在列表的某些列表项中填充日期来分割不同日期的账目),

这种方法的性能提升就不那么明显了不过还是可以使用。

最暴力的方法,保存所有创建过的View。考虑到内存占用,可以用弱表来保存这些View。

摘自 苹果君的工作室

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港