本人在面试的时候碰到过这样的面试题,就是问你如何提高ListView的滚动速度的问题,当时是一头雾水,现在有了点头目,想和大家分享一下:
ListView的滚动速度的提高在于getView方法的实现,通常我们的getView方法会这样写(至少我刚开始是这么写的):
View getView(int position,View convertView,ViewGroup parent){
//首先构建LayoutInflater
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){
//如果convertView为空,初始化convertView
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();
}
//然后做一些自己想要的处理,这样就大大提高了adapter的加载速度,从而大大提高了ListView的滚动速度问题。}
ListView的滚动速度的提高在于getView方法的实现,通常我们的getView方法会这样写(至少我刚开始是这么写的):
View getView(int position,View convertView,ViewGroup parent){
//首先构建LayoutInflater
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){
//如果convertView为空,初始化convertView
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();
}
//然后做一些自己想要的处理,这样就大大提高了adapter的加载速度,从而大大提高了ListView的滚动速度问题。}
解决方案 »
- 求助:在短信的数据库里添加一条短信未读,怎么实现推送?
- 将修改过的Android 4.0 源码如何正确合理的拷贝到 4.1上
- java.lang.NullPointerException 错误??!!!
- Android自带通讯录中的Listview的效果
- android开发在程序启动过程中加入进度条
- android 快捷方式获得焦点时的背景切换
- android asynctask doInBackground出现异常
- 点击launcher图标后,进入哪个activity的疑惑
- android资源爆红
- 三年安卓,想要多学一门知识,在纠结学Java后端还是flutte移动跨平台好。
- android导入重复联系人,不能全部显示(急)
- Android Failed to open libwvm.so问题
TextView textView;
.
.
.
}只是做到不重复的findViewById,但是View中显示的数据呢,如果这些数据是从网络上拿下来的或者数据库读出来的,滚动的时候有可能会重复读