android中listview滑动的时候卡
具体代码如下,大家帮忙看看getView方法有什么问题吗?为什么滑动会卡privateclassViewHolder{ImageViewiv_thumbnail;Text...
具体代码如下,大家帮忙看看 getView方法有什么问题吗?为什么滑动会卡
private class ViewHolder{
ImageView iv_thumbnail;
TextView tv_filename;
TextView tv_filesize;
}
private ViewHolder viewHolder = null;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//如果convertView为空,从布局解压缩器中解压缩行布局
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.item_main_list, null);
viewHolder.iv_thumbnail = (ImageView) convertView.findViewById(R.id.iv_thumbnail);
viewHolder.tv_filename = (TextView) convertView.findViewById(R.id.tv_filename);
viewHolder.tv_filesize = (TextView) convertView.findViewById(R.id.tv_filesize);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//为布局视图设置内容
File video = files.get(position);
viewHolder.iv_thumbnail.setImageBitmap(ThumbnailUtils.createVideoThumbnail(video.getAbsolutePath(), Thumbnails.MINI_KIND));
viewHolder.tv_filename.setText("片名:"+video.getName().substring(0, video.getName().lastIndexOf(".")));
DecimalFormat formater = new DecimalFormat("#.00");
viewHolder.tv_filesize.setText("大小:"+formater.format(video.length()*1.0/(1024*1024)) + "MB");
return convertView;
} 展开
private class ViewHolder{
ImageView iv_thumbnail;
TextView tv_filename;
TextView tv_filesize;
}
private ViewHolder viewHolder = null;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//如果convertView为空,从布局解压缩器中解压缩行布局
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.item_main_list, null);
viewHolder.iv_thumbnail = (ImageView) convertView.findViewById(R.id.iv_thumbnail);
viewHolder.tv_filename = (TextView) convertView.findViewById(R.id.tv_filename);
viewHolder.tv_filesize = (TextView) convertView.findViewById(R.id.tv_filesize);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//为布局视图设置内容
File video = files.get(position);
viewHolder.iv_thumbnail.setImageBitmap(ThumbnailUtils.createVideoThumbnail(video.getAbsolutePath(), Thumbnails.MINI_KIND));
viewHolder.tv_filename.setText("片名:"+video.getName().substring(0, video.getName().lastIndexOf(".")));
DecimalFormat formater = new DecimalFormat("#.00");
viewHolder.tv_filesize.setText("大小:"+formater.format(video.length()*1.0/(1024*1024)) + "MB");
return convertView;
} 展开
2个回答
展开全部
1..Adapter的getView方法里面convertView没有使用setTag和getTag方式;
2.在getView方法里面ViewHolder初始化后的赋值或者是多个控件的显示状态和背景的显示没有优化好,抑或是里面含有复杂的计算和耗时操作;
3.在getView方法里面 inflate的row
嵌套太深(布局过于复杂)或者是布局里面有大图片或者背景所致;
4.Adapter多余或者不合理的notifySetDataChanged;
5.listview
被多层嵌套,多次的onMessure导致卡顿,如果多层嵌套无法避免,建议把listview的高和宽设置为fill_parent.
如果是代码继承的listview,那么也请你别忘记为你的继承类添加上LayoutPrams,注意高和宽都是fill_parent的;
2.在getView方法里面ViewHolder初始化后的赋值或者是多个控件的显示状态和背景的显示没有优化好,抑或是里面含有复杂的计算和耗时操作;
3.在getView方法里面 inflate的row
嵌套太深(布局过于复杂)或者是布局里面有大图片或者背景所致;
4.Adapter多余或者不合理的notifySetDataChanged;
5.listview
被多层嵌套,多次的onMessure导致卡顿,如果多层嵌套无法避免,建议把listview的高和宽设置为fill_parent.
如果是代码继承的listview,那么也请你别忘记为你的继承类添加上LayoutPrams,注意高和宽都是fill_parent的;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询