ViewHolder无法正常工作

时间:2014-05-04 06:55:48

标签: android listview scroll imageview albumart

我有3页ARTIST,ALBUM,LIST。所有这3个页面都使用ListView。在使用ViewHolder之前,这3页的ListView没有顺利滚动。有太多的滞后。使用ViewHolder后,所有问题都在闪烁。但我的ALBUM页面的ListView直到有点滞后。我认为它正在发生,因为ALBUM_ART ImageView。如何解决这个问题呢?

我搜索了很多,到处都发现ViewHolder是最好的解决方案。如何正确使用?我正在为每个页面使用相同样式的ViewHolder。那么为什么只有ALBUM页面不像其他两个页面一样滚动?

这是我的ALBUM页面的适配器代码:

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;
    }

}

0 个答案:

没有答案