ListView加载图片错乱问题

 

常用方法,给显示图片控件添加Tag值(一般为图片url地址)

 

先看listview条目布局

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="wrap_content" 
  4.     android:layout_height="wrap_content" > 
  5.  
  6.     <ImageView  
  7.         android:id="@+id/photo" 
  8.         android:layout_width="90dip" 
  9.         android:layout_height="90dip" 
  10.         android:src="@drawable/empty_photo" 
  11.         android:layout_centerInParent="true" 
  12.         /> 
  13.  
  14. </RelativeLayout>

 

tag值添加位置

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    final String url = getItem(position);
            View view;
            if (convertView == null) {
                    view = LayoutInflater.from(getContext()).inflate(R.layout.photo_layout, null);
                } else {
                    view = convertView;
                }
            final ImageView photo = (ImageView) view.findViewById(R.id.photo);
             给ImageView设置一个Tag,保证异步加载图片时不会乱序
            photo.setTag(url);
            setImageView(url, photo);
            return view;

}

 

展示图片时,获取对应tag值的控件并显示图片

protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);
                // 根据Tag找到相应的ImageView控件,将下载好的图片显示出来。
                ImageView imageView = (ImageView) mPhotoWall.findViewWithTag(imageUrl);
                if (imageView != null && bitmap != null) {
                        imageView.setImageBitmap(bitmap);
                    }
               taskCollection.remove(this);
            }

此处是通过网络请求获取图片后进行展示的,主要是看如何通过tag值获取控件的

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值