如果listview里有图片,需要进行延迟加载,不然用户体验会非常差,卡在哪里不动。
解决办法:
在listview adapter的 设置 imgvidw的 方法里 写成如下, 最主要用到的是 AsyncTask
写一个 下载图片的内部类,getUrlimg是自己写的,根据自己的需求,该方法返回的是根据URL返回的 bitmap
然后在 adapter里的设置 imageview的 时候调用这个方法 比如
解决办法:
在listview adapter的 设置 imgvidw的 方法里 写成如下, 最主要用到的是 AsyncTask
写一个 下载图片的内部类,getUrlimg是自己写的,根据自己的需求,该方法返回的是根据URL返回的 bitmap
private class ImageDownloadTask extends AsyncTask<Object, Object, Bitmap>{
private ImageView imageView = null;
@Override
protected Bitmap doInBackground(Object... params) {
// TODO Auto-generated method stub
Bitmap bmp = null;
Log.d("tips","asdfasdfasdf" );
imageView = (ImageView) params[1];
try {
// bmp = BitmapFactory.decodeStream(new URL((String)params[0]).openStream());
bmp=getUrlimg((String)params[0]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bmp;
}
protected void onPostExecute(Bitmap result){
imageView.setImageBitmap(result);
}
}
然后在 adapter里的设置 imageview的 时候调用这个方法 比如
private View makeItemView(String strTitle, String url) {
LayoutInflater inflater = (LayoutInflater) AddShowbuttonList.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 使用View的对象itemView与R.layout.item关联
View itemView = inflater.inflate(R.layout.brandlistitem, null);
// 通过findViewById()方法实例R.layout.item内各组件
TextView title = (TextView) itemView.findViewById(R.id.branditemTitle);
title.setText(strTitle);
ImageView image = (ImageView) itemView.findViewById(R.id.branditemImage);
new ImageDownloadTask().execute(url, image);
// try {
// image.setImageBitmap(getUrlimg(url));
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//image.setImageResource(R.drawable.loading);
return itemView;
}