如何提高ListView的滚动速度,ListView的滚动速度的提高在于getView方法的实现,通常我们的getView方法会这样写:
- View getView(int position,View convertView,ViewGroup parent){
-
- LayoutInflater factory = LayoutInflater.from(context);
- View view = factory.inflate(R.layout.id,null);
-
- TextView text = (TextView) view.findViewById(R.id.textid);
- .
- .
- return view;
- }
这样ListView的滚动速度其实是最慢的,因为adapter每次加载的时候都要重新构建LayoutInflater和所有你的组件.而下面的方法是相对比较好的:
- View getView(int position,View contertView,ViewGroup parent){
-
- if(convertView == null)
- {
- LayoutInflater factory = LayoutInfater.from(context);
- convertView = factory.inflate(R.layout.id,null);
- }
-
- (TextView) convertView.findViewById(R.id.textid);
- return convertView;
- }
- 这样做的好处就是不用每次都重新构建convertView,基本上只有在加载第一个item时会创建convertView,这样就提高了adapter的加载速度,从而提高了ListView的滚动速度.而下面这种方法则是最好的:
-
- static class ViewClass{
- TextView textView;
- .
- .
- }
-
- View getView(int position,View convertView,ViewGroup parent){
- ViewClass view ;
- if(convertView == null){
- LayoutInflater factory = LayoutInflater.from(context);
- convertView = factory.inflate(R.layout.id,null);
- view = new ViewClass();
- view.textView = (TextView)
- convertView.findViewById(R.id.textViewid);
- .
- .
- convertView.setTag(view);
- }else{
- view =(ViewClass) convertView.getTag();
- }
-
-
- }
阅读(3791) | 评论(0) | 转发(0) |