常用方法,给显示图片控件添加Tag值(一般为图片url地址)
先看listview条目布局
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- <ImageView
- android:id="@+id/photo"
- android:layout_width="90dip"
- android:layout_height="90dip"
- android:src="@drawable/empty_photo"
- android:layout_centerInParent="true"
- />
- </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值获取控件的