日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android listview 异步加载问题

發(fā)布時(shí)間:2023/12/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android listview 异步加载问题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

============問題描述============


學(xué)做android,自己想模仿QQ空間做一個(gè)小demo
listview異步加載圖片的時(shí)候遇到了一個(gè)問題
異步加載用到了SoftReference?和文件緩存的方法
每次加載圖片的時(shí)候,也在內(nèi)存或緩存中找到了圖片
第一次加載出來后,listview滑動(dòng)了,同樣也進(jìn)到了setImageBitmap這一步
可是就是圖片顯示不出來變成空白
下面帖幾張圖和代碼
滑動(dòng)前

滑動(dòng)后Image_url?=?new?StringBuffer(AppConstant.DOWNLOAD_IMAGE_URL).append(msg.getOPImageList().get(0).getImageUrl()).toString();ImageView?imageView?=?holder.msgImage;imageView.setTag(Image_url);Bitmap?bitmap?=?Loader.loadBitmap(imageView,?Image_url,new?ImageCallBack()?{@Overridepublic?void?imageLoad(ImageView?imageView,?Bitmap?bitmap)?{if?(imageView.getTag()?!=?null&&?imageView.getTag().equals(Image_url))?{imageView.setImageBitmap(bitmap);}}});Log.e("當(dāng)前的postion",?""?+?position);if?(bitmap?==?null)?{imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));}?else?{imageView.setImageBitmap(bitmap);}
loaderpublic?class?Loader?{/***?內(nèi)存圖片軟引用緩沖*/private?static?HashMap<String,?SoftReference<Bitmap>>?imageCache?=?null;public?Loader()?{imageCache?=?new?HashMap<String,?SoftReference<Bitmap>>();}public?static?String?getFileName(String?url)?{return?url.substring(url.lastIndexOf(File.separator)?+?1);}public?static?Bitmap?getLocalResource(String?destDir,?String?imageName)?{Bitmap?bmp?=?null;File?imgeDir?=?new?File(destDir);File?cache?=?null;if?(!imgeDir.exists())?{?//?判斷本地緩存目錄是否存在imgeDir.mkdirs();}?else?{cache?=?new?File(destDir?+?File.separator?+?imageName);?//?判斷該圖片資源是否存在if?(cache.exists())?{?//?如果存在就根據(jù)圖片的存儲(chǔ)路徑得到Bitmap對(duì)象?bmbmp?=?BitmapFactory.decodeFile(cache.getAbsolutePath());}}return?bmp;}public?static?Bitmap?loadBitmap(final?ImageView?imageView,?final?String?imageURL,final?ImageCallBack?imageCallBack)?{//?在內(nèi)存緩存中,則返回Bitmap對(duì)象if?(imageCache.containsKey(imageURL))?{SoftReference<Bitmap>?reference?=?imageCache.get(imageURL);Bitmap?bitmap?=?reference.get();if?(bitmap?!=?null)?{return?bitmap;}}?else?{/***?加上一個(gè)對(duì)本地緩存的查找*/String?bitmapName?=?imageURL.substring(imageURL.lastIndexOf("/")?+?1);Bitmap?bitmapTemp?=?null;bitmapTemp?=?getLocalResource(AppConstant.TEST,?bitmapName);if?(bitmapTemp?!=?null)?{return?bitmapTemp;}}final?Handler?handler?=?new?Handler()?{/**?(non-Javadoc)*?*?@see?android.os.Handler#handleMessage(android.os.Message)*/@Overridepublic?void?handleMessage(Message?msg)?{//?TODO?Auto-generated?method?stubimageCallBack.imageLoad(imageView,?(Bitmap)?msg.obj);}};//?如果不在內(nèi)存緩存中,也不在本地(被jvm回收掉),則開啟線程下載圖片new?Thread()?{/**?(non-Javadoc)*?*?@see?java.lang.Thread#run()*/@Overridepublic?void?run()?{//?TODO?Auto-generated?method?stubBitmap?bitmap?=?null;try?{URL?imageUrl?=?new?URL(imageURL);HttpURLConnection?conn?=??(HttpURLConnection)?imageUrl.openConnection();conn.setConnectTimeout(30000);conn.setReadTimeout(30000);conn.setInstanceFollowRedirects(true);//?InputStream?is?=?conn.getInputStream();InputStream?in?=?conn.getInputStream();BitmapFactory.Options?options?=?new?BitmapFactory.Options();options.inJustDecodeBounds?=?false;options.inSampleSize?=?10;?//?width,hight設(shè)為原來的十分一bitmap?=?BitmapFactory.decodeStream(in,?null,?options);}?catch?(MalformedURLException?e1)?{//?TODO?Auto-generated?catch?blocke1.printStackTrace();}?catch?(IOException?e1)?{//?TODO?Auto-generated?catch?blocke1.printStackTrace();}imageCache.put(imageURL,?new?SoftReference<Bitmap>(bitmap));Message?msg?=?handler.obtainMessage(0,?bitmap);handler.sendMessage(msg);File?dir?=?new?File(AppConstant.TEST);if?(!dir.exists())?{dir.mkdirs();}File?bitmapFile?=?new?File(AppConstant.TEST?+?File.separator+?imageURL.substring(imageURL.lastIndexOf("/")?+?1));if?(!bitmapFile.exists())?{try?{bitmapFile.createNewFile();}?catch?(IOException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}}FileOutputStream?fos;try?{fos?=?new?FileOutputStream(bitmapFile);bitmap.compress(Bitmap.CompressFormat.JPEG,?100,?fos);fos.close();}?catch?(FileNotFoundException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}?catch?(IOException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}}}.start();return?null;}/***?回調(diào)接口*?*?@author?onerain*?*/public?interface?ImageCallBack?{public?void?imageLoad(ImageView?imageView,?Bitmap?bitmap);}}
坐等大神支招啊
每次都進(jìn)了setimagebitmap這一步?就是加載不出圖片

============解決方案1============


你用imageLoader這個(gè)三方開源庫試試。

============解決方案2============


看一下你imageview控件設(shè)的是src還是background。或者試試把bitmap轉(zhuǎn)成drawable,再setbackground試試

============解決方案3============


看下getView方法?

============解決方案4============


貼下Adapter的代碼看看唄

============解決方案5============


你試試AsyncImage這個(gè)第三方開源庫

============解決方案6============


??ImageView?imageViewByTag?=?(ImageView)?listview?
????????????????????????????????.findViewWithTag(imageUrl);?

問題應(yīng)該在這一句?你這個(gè)搜索?只會(huì)找到第一個(gè)TAG?是imageUrl的控件。?可能并不是你的要更新的控件。

比如??你setTAG了??,?listView?會(huì)重用item的,???在?msg.getOPImageList().size()=0?時(shí)候?還是有這個(gè)tag。??
然后你find的結(jié)果就是這個(gè)contentView里面的,不是你要更新的那個(gè)ImageView。?

============解決方案7============


引用Image_url?=?new?StringBuffer(AppConstant.DOWNLOAD_IMAGE_URL)
.append(msg.getOPImageList().get(0).getImageUrl())
.toString();

ImageView?imageView?=?holder.msgImage;
imageView.setTag(Image_url);
Bitmap?bitmap?=?Loader.loadBitmap(imageView,?Image_url,
new?ImageCallBack()?{
@Override
public?void?imageLoad(ImageView?imageView,?Bitmap?bitmap)?{
if?(imageView.getTag()?!=?null
&&?imageView.getTag().equals(Image_url))?{
imageView.setImageBitmap(bitmap);
}
}
});
Log.e("當(dāng)前的postion",?""?+?position);
if?(bitmap?==?null)?{
imageView.setImageDrawable(context.getResources().getDrawable(
R.drawable.ic_launcher));
}?else?{
imageView.setImageBitmap(bitmap);
}


這個(gè)代碼中??在?ImageCallBack?接口中?吧imageUrl?回傳回來,跟imageView.getTag()?進(jìn)行比較,應(yīng)該可以解決問題。試試看。

轉(zhuǎn)載于:https://www.cnblogs.com/lengyanyue39/p/4091340.html

總結(jié)

以上是生活随笔為你收集整理的android listview 异步加载问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。