Android ViewHolder工作不正常
我有3页的艺术家,专辑,名单。所有这3个页面都使用ListView。在使用ViewHolder之前,这3个页面的ListView滚动不顺畅。落后太多了。使用ViewHolder后,所有问题都会在眨眼中消失。但是我的相册页面的ListView有点滞后。我认为这是因为专辑《艺术图像视图》。如何解决这个问题 我搜索了很多地方,发现ViewHolder是最好的解决方案。如何正确使用它?我对每个页面都使用相同样式的ViewHolder。那个么为什么只有相册页面并不像其他两个页面那个样滚动呢 以下是我的相册页面的适配器代码:Android ViewHolder工作不正常,android,listview,scroll,imageview,albumart,Android,Listview,Scroll,Imageview,Albumart,我有3页的艺术家,专辑,名单。所有这3个页面都使用ListView。在使用ViewHolder之前,这3个页面的ListView滚动不顺畅。落后太多了。使用ViewHolder后,所有问题都会在眨眼中消失。但是我的相册页面的ListView有点滞后。我认为这是因为专辑《艺术图像视图》。如何解决这个问题 我搜索了很多地方,发现ViewHolder是最好的解决方案。如何正确使用它?我对每个页面都使用相同样式的ViewHolder。那个么为什么只有相册页面并不像其他两个页面那个样滚动呢 以下是我的相册
public class PropertyOfAlbum extends BaseAdapter {
public static ViewHolder holder;
private Context mContext;
Cursor cursor;
private LayoutInflater layoutInflater;
Typeface tf, tf2, tf3;
Bitmap coverBitmap;
public PropertyOfAlbum(Context context, Cursor cur) {
super();
mContext = context;
cursor = cur;
tf = Typeface.createFromAsset(context.getAssets(),
"fonts/gang_wolfik_blade.ttf");
tf2 = Typeface.createFromAsset(context.getAssets(),
"fonts/gang_wolfik_blade.ttf");
tf3 = Typeface.createFromAsset(context.getAssets(),
"fonts/gang_wolfik_blade.ttf");
layoutInflater = LayoutInflater.from(context);
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public View getView(int position, View view, ViewGroup parent) {
cursor.moveToPosition(position);
if (view == null) {
view = layoutInflater.inflate(R.layout.album_row, null);
holder = new ViewHolder();
holder.title = (TextView) view.findViewById(R.id.title);
holder.duration = (TextView) view.findViewById(R.id.duration);
holder.artist = (TextView) view.findViewById(R.id.artist);
holder.iv = (ImageView) view.findViewById(R.id.list_image);
holder.col = cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ART);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.title.setTypeface(tf);
holder.artist.setTypeface(tf2);
holder.duration.setTypeface(tf3);
holder.title.setTextSize(18);
Log.d(null, "col " + holder.col);
holder.title.setText(cursor.getString(1));
holder.artist.setText(cursor.getString(2));
holder.duration.setText(cursor.getString(4));
coverBitmap = BitmapFactory.decodeFile(cursor.getString(holder.col));
if(coverBitmap == null){
holder.iv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher));
}else{
holder.iv.setImageBitmap(coverBitmap);
}
// LayoutInflater inflater = (LayoutInflater) mContext
// .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// view = inflater.inflate(R.layout.album_row, null);
// cursor.moveToPosition(position);
// TextView du = (TextView) view.findViewById(R.id.duration);
// TextView ti = (TextView) view.findViewById(R.id.title);
// TextView ar = (TextView) view.findViewById(R.id.artist);
// Typeface tf = Typeface.createFromAsset(view.getContext().getAssets(),
// "fonts/ideoma_SPRAY.otf");
// Typeface tf2 = Typeface.createFromAsset(view.getContext().getAssets(),
// "fonts/ideoma_SPRAY.otf");
// Typeface tf3 = Typeface.createFromAsset(view.getContext().getAssets(),
// "fonts/ideoma_SPRAY.otf");
// ti.setTypeface(tf);
// ti.setTextSize(18);
// ar.setTypeface(tf2);
// du.setTypeface(tf3);
// Integer.parseInt(cursor.getString(5))
// int seconds = (Integer.parseInt(cursor.getString(5)) / 1000) % 60;
// long minutes = ((Integer.parseInt(cursor.getString(5)) - seconds) / 1000) / 60;
// du.setText("" + minutes + "." + seconds);
// if (cursor.getString(1).equals(null)
// || cursor.getString(1).equals("<unknown>")
// || cursor.getString(1).equals("unknown")
// || cursor.getString(1).equals("")) {
// } else {
// ar.setText(cursor.getString(1));
// }
// ar.setText(cursor.getString(1));
// ti.setText(cursor.getString(1));
// du.setText("Total Songs: " + cursor.getString(4));
// if (cursor.getString(2).equals(null)
// || cursor.getString(2).equals("<unknown>")
// || cursor.getString(2).equals("unknown")
// || cursor.getString(2).equals("")) {
// } else {
// ar.setText(cursor.getString(2));
// }
//du.setText(cursor.getString(2));
return view;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return cursor.getCount();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public static class ViewHolder {
TextView title;
TextView duration;
TextView artist;
ImageView iv;
int col;
}
}
公共类PropertyOfAlbum扩展了BaseAdapter{
公共静态取景器;
私有上下文;
光标;
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
字体tf,tf2,tf3;
位图覆盖位图;
公共属性yofalbum(上下文,游标cur){
超级();
mContext=上下文;
光标=cur;
tf=Typeface.createFromAsset(context.getAssets(),
“字体/gang_wolfik_blade.ttf”);
tf2=Typeface.createFromAsset(context.getAssets(),
“字体/gang_wolfik_blade.ttf”);
tf3=Typeface.createFromAsset(context.getAssets(),
“字体/gang_wolfik_blade.ttf”);
layoutInflater=layoutInflater.from(上下文);
}
@TargetApi(构建版本代码姜饼)
@SuppressLint(“新API”)
公共视图getView(内部位置、视图视图、视图组父视图){
光标。移动位置(位置);
如果(视图==null){
视图=LayoutFlater.充气(R.layout.album_行,空);
holder=新的ViewHolder();
holder.title=(TextView)view.findViewById(R.id.title);
holder.duration=(TextView)view.findViewById(R.id.duration);
holder.artist=(TextView)view.findViewById(R.id.artist);
holder.iv=(ImageView)view.findViewById(R.id.list\u image);
holder.col=cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM\u ART);
视图.设置标签(支架);
}否则{
holder=(ViewHolder)view.getTag();
}
持有者。标题。设置字体(tf);
holder.Artister.setTypeface(tf2);
持牌人.持牌人.持牌人.设定字型(tf3);
持有人。头衔。setTextSize(18);
Log.d(空,“col”+holder.col);
holder.title.setText(cursor.getString(1));
holder.artist.setText(cursor.getString(2));
holder.duration.setText(cursor.getString(4));
coverBitmap=BitmapFactory.decodeFile(cursor.getString(holder.col));
if(coverBitmap==null){
holder.iv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_启动器));
}否则{
holder.iv.setImageBitmap(封面位图);
}
//LayoutFlater充气器=(LayoutFlater)mContext
//.getSystemService(上下文布局\充气机\服务);
//视图=充气机。充气(R.layout.album_行,空);
//光标。移动位置(位置);
//TextView du=(TextView)view.findViewById(R.id.duration);
//TextView ti=(TextView)view.findViewById(R.id.title);
//TextView ar=(TextView)view.findViewById(R.id.artist);
//Typeface tf=Typeface.createFromAsset(view.getContext().getAssets(),
//“fonts/ideoma_SPRAY.otf”);
//Typeface tf2=Typeface.createFromAsset(view.getContext().getAssets(),
//“fonts/ideoma_SPRAY.otf”);
//Typeface tf3=Typeface.createFromAsset(view.getContext().getAssets(),
//“fonts/ideoma_SPRAY.otf”);
//设置字体(tf);
//ti.setTextSize(18);
//ar.setTypeface(tf2);
//设置字体(tf3);
//Integer.parseInt(cursor.getString(5))
//int秒=(Integer.parseInt(cursor.getString(5))/1000)%60;
//长分钟=((Integer.parseInt(cursor.getString(5))-seconds)/1000)/60;
//du.setText(“+分钟+”+秒);
//if(cursor.getString(1).equals(null)
//| | cursor.getString(1).equals(“”)
//| | cursor.getString(1).equals(“未知”)
//| | cursor.getString(1).equals(“”){
//}其他{
//ar.setText(cursor.getString(1));
// }
//ar.setText(cursor.getString(1));
//ti.setText(cursor.getString(1));
//du.setText(“歌曲总数:+cursor.getString(4));
//if(cursor.getString(2).equals(null)
//| | cursor.getString(2).equals(“”)
//| | cursor.getString(2).equals(“未知”)
//| | cursor.getString(2).equals(“”){
//}其他{
//ar.setText(cursor.getString(2));
// }
//du.setText(cursor.getString(2));
返回视图;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回cursor.getCount();
}
@凌驾
公共对象getItem(int arg0){
//TODO自动生成的方法存根
返回arg0;
}
@凌驾
公共长getItemId(int arg0){
//TODO自动生成的方法存根
返回arg0;
}
公共静态类视图持有者{
文本视图标题;
文本视图持续时间;
文本视图艺术家;
ImageView iv;
int col;
}
}
实际上,ViewHolder没有多大帮助,例如CursorAdapter及其所有子类都不使用ViewHolder,我不会说它们很慢。我们的数据模型是基于光标的,所以为什么不使用一个简单的ViewHolder呢?我认为ViewHolder是最好的方法。ViewHolder只保存了一些findVuewById调用,仅此而已,首先使用SimpleCorsorAdapter,然后尝试查看是否存在任何瓶颈。我该怎么办?