1

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

}
4

0 回答 0