在看了一些vogella的文章之后,发现关于android listview性能优化这一段很有意思,于是实践了一下,经过优化,性能确实提升不少!
成都创新互联公司-专业网站定制、快速模板网站建设、高性价比红桥网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式红桥网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖红桥地区。费用合理售后完善,十载实体公司更值得信赖。
先看看优化前和优化后的比较:
优化前的log截图:
优化后的log截图:
并且,在不停滚动ListView的过程中,优化之前会出现ANR现象,在AVD上特别容易复现:
然后,优化后显得很流畅,附上对于的log截图:
下面附上相关代码分析:
ListView中的每一个Item由一个ImageView 和一个TextView组成
Layout:
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal" >
- android:layout_width="wrap_content"
- android:layout_height="fill_parent" />"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginLeft="15dp"
- android:gravity="center_vertical" />
Activity继承自ListActivity,我故意增加了Item,方便测试,效果更明显:
- public class ListViewDemo extends ListActivity{
- private final String[] mItems = new String[] { "Android", "iPhone",
- "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7",
- "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X",
- "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
- "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu",
- "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7",
- "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X",
- "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
- "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu",
- "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7",
- "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X",
- "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
- "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu",
- "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7",
- "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X",
- "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
- "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu",
- "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7",
- "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X",
- "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
- "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu",
- "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7",
- "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X",
- "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
- "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu",
- "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7",
- "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X",
- "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- ListViewArrayAdapter adapter = new ListViewArrayAdapter(this, mItems);
- getListView().setAdapter(adapter);
- }
- }
然后custom Adapter,优化之前的adapter:
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- long start = System.currentTimeMillis();
- LayoutInflater inflater = (LayoutInflater) mContext.getLayoutInflater();
- View rowView = inflater.inflate(mViewResourceId, parent, false);
- TextView textView = (TextView) rowView
- .findViewById(mTextViewResourceId);
- ImageView imageView = (ImageView) rowView
- .findViewById(mImageViewResourceId);
- textView.setText(mNames[position]);
- String s = mNames[position];
- if (s.startsWith("Windows7") || s.startsWith("iPhone")) {
- imageView.setImageResource(R.drawable.no);
- } else {
- imageView.setImageResource(R.drawable.yes);
- }
- Log.v("jerikc","cost time = " + (System.currentTimeMillis() - start));
- return rowView;
- }
优化之后的Adapter:
- public class ListViewArrayAdapter extends ArrayAdapter
{ - private final Activity mContext;
- private final String[] mNames;
- private final static int mViewResourceId = R.layout.text_image_row_layout;
- private final static int mTextViewResourceId = R.id.textView;
- private final static int mImageViewResourceId = R.id.imageView;
- static class ViewHolder {
- public TextView text;
- public ImageView image;
- }
- public ListViewArrayAdapter(Activity context, String[] names) {
- super(context, mViewResourceId, names);
- this.mContext = context;
- this.mNames = names;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- long start = System.currentTimeMillis();
- View rowView = convertView;
- if (rowView == null) {
- LayoutInflater inflater = mContext.getLayoutInflater();
- rowView = inflater.inflate(mViewResourceId, null);
- ViewHolder viewHolder = new ViewHolder();
- viewHolder.text = (TextView) rowView.findViewById(mTextViewResourceId);
- viewHolder.image = (ImageView) rowView.findViewById(mImageViewResourceId);
- rowView.setTag(viewHolder);
- }
- ViewHolder holder = (ViewHolder) rowView.getTag();
- String s = mNames[position];
- holder.text.setText(s);
- if (s.startsWith("Windows7") || s.startsWith("iPhone")) {
- holder.image.setImageResource(R.drawable.no);
- } else {
- holder.image.setImageResource(R.drawable.yes);
- }
- Log.v("jerikc","cost time = " + (System.currentTimeMillis() - start));
- return rowView;
- }
- }
优化的大致思想就是:优化之前,每次加载item的时候,都要加载一下布局文件,然后生成一个新的row View对象,然后通过View找到对应的ImageView和TextView,正如我们所知道的那样,加载布局文件时很耗时的,特别是在操作比较频繁 情况下,这是不可忍受的,所以会导致ANR现象。
因此,我们可以重复利用已不可见的row View对象。Android中,当它决定让row View对象不可见的时候,它允许通过getView方法中的convertView参数来重复利用刚刚不可见的row View对象。
在优化的过程中,第一次加载的时候,我们需要把相关的数据保存起来,而View有一个方法setTag,该方法可用来保存一些数据结构。我们一个row View对象是由ImageView和TextView空间组成的,因此定义一个ViewHolder来保存ImageView和TextView对象。 在重复利用的过程中,只需简单修改它们的值,而不用再次findViewById。
本文标题:Android优化ListView实践
文章地址:http://www.shufengxianlan.com/qtweb/news49/538549.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联