栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Listview显示错误的图像

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Listview显示错误的图像

问题来自于您的convertView:它是整个列表中使用的同一实例,因此,当异步加载完成时,当listview尝试使用相同的convertView绘制其他项目时,图像会更改(或在这种情况下) ,其子imageView)。

painting position 1, uses placeholder, starts loading image 1 asynchronouslypainting position 2, uses placeholder, starts loading image 2 asynchronouslyimage 1 loading is complete, calling setImageBitmap on imageViewpainting position 3, uses image 1, starts loading image 3 asynchronouslyetc.

但是,您可以做的是在listadapter中保留位图缓存。像这样:

private Bitmap[] bitmapList;private Bitmap bitmapPlaceholder;  private void initBitmapListWithPlaceholders(){ // call this whenever the list size changes// you can also use a list or a map or whatever so you // don't need to drop all the previously loaded bitmap whenever // the list contents are modified    int count = getListCount();    bitmapList = new Bitmap[count];    for(int i=0;i<count;i++){         bitmapList[i]=bitmapPlaceholder    }}private void onBitmapLoaded(int position, Bitmap bmp){// this is your callback when the load async is done    bitmapList[position] = bmp;}public View getView(int position, View convertView, ViewGroup parent) {    View v = convertView;    ImageView imageView;    TextView textView;    if (v == null) {        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(     Context.LAYOUT_INFLATER_SERVICE);        v = vi.inflate(R.layout.drink_list_row, null);    }    Drink drink = allItems.get(position);    if (drink != null && v != null) {        imageView = (ImageView) v.findViewById(R.id.picture);        textView = (TextView) v.findViewById(R.id.drinkName);        imageView.setVisibility(View.GONE);        imageView.setImageBitmap(bitmapList[position]);        loadImageBitmap(drink, position); // this should call onBitmapLoaded(int position, Bitmap bmp) when finished to update the bitmapList and replace the placeholder        textView.setText(drink.getName());        if (subItems != null && subItems.contains(drink)) { textView.setVisibility(View.VISIBLE); imageView.setVisibility(View.VISIBLE);        } else { textView.setVisibility(View.GONE); imageView.setVisibility(View.GONE);        }    }    return v;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/366916.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号