ImageView在滚动ListView时不断重新加载

时间:2014-05-15 12:23:13

标签: android android-asynctask imageview thumbnails android-cursoradapter

我有一个包含ImageView和Textview的列表。我试图在ImageView中显示视频缩略图。问题是,当我向下滚动列表时,ImageView会不断重新加载缩略图,直到找到正确的缩略图。当我向上滚动列表时,会发生同样的事情。

我使用的是CursorAdapter和AsyncTask类。这是代码:

public class VideoAdapter extends CursorAdapter {

private final LayoutInflater mInflater;

public TheAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
    mInflater=LayoutInflater.from(context);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    String name = cursor.getString(cursor.getColumnIndex(VideoColumns.DISPLAY_NAME));
    int path = cursor.getColumnIndexOrThrow(VideoColumns.DATA);

    ViewHolder holder =  (ViewHolder)view.getTag();
    holder.thumb.setId(cursor.getPosition());

    holder.title.setText(name);

    GetThumbnail newImg = new GetThumbnail(holder.thumb);
    newImg.execute(cursor.getString(path));
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final View view=mInflater.inflate(R.layout.main_list,parent,false); 
    ViewHolder holder = new ViewHolder(view);
    view.setTag(holder);
    return view;
}

    private static class ViewHolder{
    TextView title; 
    ImageView thumb;
    public ViewHolder(View base) {
        title = (TextView)base.findViewById(R.id.text);
        thumb = (ImageView)base.findViewById(R.id.image);
    }
}

    private class GetThumbnail extends AsyncTask<String, Void, Bitmap>   {

        private ImageView imv;
        private int mPosition;

        public GetThumbnail(ImageView i){
            imv = i;
            mPosition = i.getId();
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            Bitmap bm;
          bm = ThumbnailUtils.createVideoThumbnail(params[0], Thumbnails.MINI_KIND);
            return bm;
        }

        @Override
            protected void onPostExecute(Bitmap result) {
           if(result != null && mPosition == imv.getId()){

            imv.setImageBitmap(result);   
           }

            }
    }
}

任何建议将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

列表视图向上或向下滚动时回收行它可以提高列表视图的性能。但如果你不想回收它,你可以阻止它。