Volley加载相同的缩略图

时间:2013-10-11 14:43:34

标签: android caching thumbnails android-volley

我遇到了Volley和ListView的问题。也许我不明白Volley应该怎么运作......看这里,这是我的活动代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRequestQueue = Volley.newRequestQueue(this);
    imageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache());

    items = createItemList();

    expListView = (ListView) findViewById(R.id.listView);
    ListAdapter listAdapter = new ListAdapter();
    expListView.setAdapter(listAdapter);
}

我使用Volley.newRequestQueue(this)the same here)设置ImageLoaderBitmapLruCache

在我的ListAdapter中,我覆盖getView()

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) 
            convertView = infalInflater.inflate(R.layout.menu_item, null);
        final ImageView ivCover = (ImageView) convertView.findViewById(R.id.icCoverThumb);
        imageLoader.get(items.get(position).url, ImageLoader.getImageListener(ivCover, R.drawable.ic_launcher, R.drawable.ic_launcher));
        return convertView;
    }

我在这里使用imageLoader.get(items.get(position).url, ImageLoader.getImageListener(ivCover, R.drawable.ic_launcher, R.drawable.ic_launcher));(这里是招标吗?)。

拇指已加载,但是smth出错:当应用程序第一次启动时,列表视图中的第一项具有重复的ico,其中一个图标来自另一行 - http://prntscr.com/1wmrck

谢谢!

1 个答案:

答案 0 :(得分:2)

如果没有更多信息很难重现,但我建议使用NetworkImageView从网络加载缩略图,顺便说一句,我会使用" ViewHolder"改善ListView

的效果

这是一个示例ArrayAdapter,用于在ListView上显示推文

...
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.tweet_list_item, null);
        holder = new ViewHolder();
        holder.imageView = (NetworkImageView) convertView.findViewById(R.id.twitterUserImage);
        //set more views (username, text, date ...)

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    //get the current element from the data array
    Tweet currentTweet = getItem(position);

    //set data into the views
    holder.imageView.setImageUrl(currentTweet.getUser().getProfileImageUrl(), VolleyManager.getImageLoader());
    //...


    return convertView;
}

private static class ViewHolder {
    private NetworkImageView imageView;
    //other views...
}