在ListView上重复ImageView

时间:2014-01-17 03:44:07

标签: android listview android-listview android-imageview

My ImageView每隔6个项目在ListView上重复一次 这是我的CustomAdapter代码

public class NewsAdapter extends ArrayAdapter<NewsContainer> {

private Context context;
private NewsContainer[] newsContainer;

public NewsAdapter(Context context, int textViewResourceID, NewsContainer[] newsContainers) {
    super(context, textViewResourceID, newsContainers);
    this.newsContainer = newsContainers;
    this.context = context;
}

@Override
public long getItemId(int position){
    return position;
}

public View getView(int position, View view, ViewGroup parent){
    viewHolder holder = null;
    if(view == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.news_listview, null);

        holder = new viewHolder();
        holder.image = (ImageView) view.findViewById(R.id.thumbnail);
        holder.title = (TextView) view.findViewById(R.id.title);
        holder.created_date = (TextView) view.findViewById(R.id.created_date);
        holder.category = (TextView) view.findViewById(R.id.category);
        holder.post_count = (TextView) view.findViewById(R.id.post_count);
        view.setTag(holder);
    } else {
        holder = (viewHolder) view.getTag();
    }

    NewsContainer news = newsContainer[position];
    SimpleDateFormat format = new SimpleDateFormat("DD/MM/yyyy", Locale.US);
    if(news != null){
        String date = format.format(news.getCreatedDate());
        holder.title.setText(news.getTitle());
        holder.post_count.setText(String.valueOf(news.getPostCount()));
        holder.created_date.setText(date);
        holder.category.setText(Html.fromHtml(news.getCategory()));
        if(!news.getThumbnail().equals("empty")){
            new ImageDownloaderTask(holder.image).execute(news.getThumbnail());
        }    
    }

    return view;
}

static class viewHolder {
    ImageView image;
    TextView title;
    TextView created_date;
    TextView category;
    TextView post_count;
}

ImageDownloader类

public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {

private final WeakReference<ImageView> imageViewWeakReference;

public ImageDownloaderTask(ImageView imageView) {
    this.imageViewWeakReference = new WeakReference<ImageView>(imageView);
}

@Override
protected Bitmap doInBackground(String... params) {
    return downloadBitmap(params[0]);
}

@Override
protected void onPostExecute(Bitmap bitmap){
    if(isCancelled()){
        bitmap = null;
    }

    if(imageViewWeakReference != null){
        ImageView imageView = imageViewWeakReference.get();
        if(imageView != null){
            if(bitmap != null){
                imageView.setImageBitmap(bitmap);
            } else {
                imageView.setImageDrawable(imageView.getContext().getResources().getDrawable(R.drawable.ic_loading_image));
            }
        }
    }
}

@TargetApi(Build.VERSION_CODES.FROYO)
static Bitmap downloadBitmap(String url) {
    final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);
    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode
                    + " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();
                final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (Exception e) {
        // Could provide a more explicit error message for IOException or
        // IllegalStateException
        getRequest.abort();
        Log.w("ImageDownloader", "Error while retrieving bitmap from " + url);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return null;
}

我尝试过使用ViewHolder,但ImageView不断重复,并发现ListView会回收其视图以保持较低的内存占用率。但我无法理解如何解决这个问题。

3 个答案:

答案 0 :(得分:3)

else {
    holder = (viewHolder) view.getTag();
}

这是你真正的问题,如果view为空,你正在膨胀,但如果它已经有值,则不进行任何修改。当view被回收时,它将包含一些值,并且不会为null。因此,您必须更改所有值,而不是重用现有值。

答案 1 :(得分:2)

如果在getView中获得空缩略图,则应清除或重置图像视图。

从我的手机写信,为简洁道歉。

编辑:我没有看到你在代码中设置了这个。例如:

if(news != null){
    String date = format.format(news.getCreatedDate());
    holder.title.setText(news.getTitle());
    holder.post_count.setText(String.valueOf(news.getPostCount()));
    holder.created_date.setText(date);
    holder.category.setText(Html.fromHtml(news.getCategory()));
    if(!news.getThumbnail().equals("empty")){
        new ImageDownloaderTask(holder.image).execute(news.getThumbnail());
    }    
}

如果news为空或news.getThumbnail().equals("empty") == true,该怎么办?您将保持未使用的回收图像视图。

答案 2 :(得分:0)

public View getView(int position, View view, ViewGroup parent){

 if(view == null){
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      view = inflater.inflate(R.layout.news_listview, null);
  }

    ImageView image = (ImageView) view.findViewById(R.id.thumbnail);
    TextView  title = (TextView) view.findViewById(R.id.title);
    TextView  created_date = (TextView) view.findViewById(R.id.created_date);
    TextView category = (TextView) view.findViewById(R.id.category);
    TextView post_count = (TextView) view.findViewById(R.id.post_count);

 NewsContainer news = NewsContainer[position];
 SimpleDateFormat format = new SimpleDateFormat("DD/MM/yyyy", Locale.US);
 if(news != null){
     String date = format.format(news.getCreatedDate());
     title.setText(news.getTitle());
     post_count.setText(String.valueOf(news.getPostCount()));
     created_date.setText(date);
     category.setText(Html.fromHtml(news.getCategory()));
     if(!news.getThumbnail().equals("empty")){
        new ImageDownloaderTask(image).execute(news.getThumbnail());
  }    

}