作为一个android开发不知道多少年的开发者来说,除了四大组件之外,我觉得开发过程中最需要注意的就是在使用listview的优化。关于listview的优化我有几个心得:
未优化的代码:
public View getView(int position, View convertView, ViewGroup parent) { final yuangongBean.DataBean product = this.alls.get(position); View view = LayoutInflater.from(context).inflate(R.layout.yuangong_item, null); TextView yuangong_name=(TextView) view.findViewById(R.id.yuangong_name); TextView yuangong_zhiwei=(TextView) view.findViewById(R.id.yuangong_zhiwei); ImageView hand=(RoundImageView) view.findViewById(R.id.hand); yuangong_name.setText(product.getUserName()); yuangong_zhiwei.setText(product.getJobName()); ToolImage.setImageLoader(hand, product.getAvatar(), context, R.drawable.moreng, R.drawable.moreng);//图片 return view ; }
1、convertView的重复使用:在adpter中getView()下有convertView,我们判断convertView是否为空,为空的情况下我们新建一个,不为空的情况下,我们重复使用已经“消失”的view,给view重新加上数据。
优化convertView后:
private View view; @Override public View getView(int position, View convertView, ViewGroup parent) { final yuangongBean.DataBean product = this.alls.get(position); if(convertView==null){ view = LayoutInflater.from(context).inflate(R.layout.yuangong_item, null); } TextView yuangong_name=(TextView) view.findViewById(R.id.yuangong_name); TextView yuangong_zhiwei=(TextView) view.findViewById(R.id.yuangong_zhiwei); ImageView hand=(RoundImageView) view.findViewById(R.id.hand); yuangong_name.setText(product.getUserName()); yuangong_zhiwei.setText(product.getJobName()); ToolImage.setImageLoader(hand, product.getAvatar(), context, R.drawable.moreng, R.drawable.moreng);//图片 return view ; }
2、使用ViewHolder:在开发中你会发现,不使用ViewHoder的时候你会发现每次运行到findViewById的时候都会赋值一次,在使用ViewHoder的时候实际上就是相当于我们建了一个容器,当converView==null的时候我们吧findViewById对应控件放进容器“ViewHoder”中,当我们需要使用的时候就走ViewHoder中取出,相对于findViewById效率不是一般的高
优化ViewHolder后:
public View getView(int position, View convertView, ViewGroup parent) { final yuangongBean.DataBean product = this.alls.get(position); View view; ViewHolder viewHolder; if(convertView==null){ view = LayoutInflater.from(context).inflate(R.layout.yuangong_item, null); viewHolder=new ViewHolder(); viewHolder. yuangong_name=(TextView) view.findViewById(R.id.yuangong_name); viewHolder. yuangong_zhiwei=(TextView) view.findViewById(R.id.yuangong_zhiwei); viewHolder.hand=(RoundImageView) view.findViewById(R.id.hand); view.setTag(viewHolder); }else{ view=convertView; viewHolder=(ViewHolder) view.getTag(); } viewHolder.yuangong_name.setText(product.getUserName()); viewHolder. yuangong_zhiwei.setText(product.getJobName()); ToolImage.setImageLoader(viewHolder.hand, product.getAvatar(), context, R.drawable.moreng, R.drawable.moreng);//图片 return view ; } class ViewHolder { ImageView hand; TextView yuangong_name; TextView yuangong_zhiwei; }
3、分页加载:相信很多开发者都知道对于应用中的listView大多都是成百上千条数据,在处理成百上千的数据的时候你会发现数据量大网络请求会很慢,在开发中对于大量数据的list,我们都会使用分页请求的方式,来降低单个接口请求量。分页加载无非是最合适的优化方式,也是必不可少的。记住成千上万条数据一定要分页哦,不然加载奔溃的锅,你就要背了!!!
4、异步加载:list中不单单是单个的数据什么的,更多的时候是有大量的图片存在的,对于有图片存在的情况下,我们一般会怎么处理呢,异步加载图片,没错就是异步加载图片,这样减少了图片下载存在的问题,还有一个更为重要的一点,这里我要补充一下,千万不要在list滑动的时候去加载图片,在list停止滑动的时候加载图片是最好的选择!!!!
还想进一步优化,记得重写父类!