gpt4 book ai didi

android - ListAdapter 和后台线程

转载 作者:行者123 更新时间:2023-12-03 13:04:37 25 4
gpt4 key购买 nike

我有一个listadapter,其中有很多listitems,每个项目都包含一个textview 和一个imageview。我想从后端下载图片( 以 Base64 编码 ),所以 Picasso 和通过 URL 下载解决方案对我不利。
在我下载了 Base64 解码并且位图创建已经完成之后,我只是卡住了如何在列表适配器中启动后台线程,其中 View 总是在回收(当用户滚动时)。

我开始使用 Hanlder 创建一个线程,其中处理程序将位图插入到 ImageView 中,但它没有工作(内存不足)。之后我触发了一个 AsyncTask,但在这种情况下我遇到了回收问题,namley:当用户向下滚动时,我在底部 View 中看到顶部图像:(

使用改造下载

你能帮我吗 ?

我的 AsyncTaks 代码:
这里的问题,我更新了imageview,但也许用户已经被滚动了!

public DownloadImageAsyncTask(ImageView iw, Display display, String imageID, ImagesCache imagesCache) {
this.iw = iw;
this.imageCache = imagesCache;
this.imageID = imageID;
this.display = display;
}

@Override
protected Void doInBackground(Void... params) {
String[] imageArray = new String[1];
imageArray[0] = imageID;
ImageResponse imageResponse = new IdeaBackend().getImageByID(imageArray);
bitmap = UserExperienceHelper.decodeBase64AndScaleDownImage(imageResponse.getResponse().get(0).getImageBase64Code(), display);
imageCache.put(imageID, bitmap);

return null;
}


@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
this.iw.setImageBitmap(bitmap);
}

最佳答案

在您的适配器中:

View listItemLayout = (View) inflater.inflate(...);
ImageView imageView = (ImageView) listItemLayout.findViewById(R.id.imageView);
MyImageLoader.loadImage(activity, imageView, id);

这是一个公共(public)类,将为您完成图像的下载和缓存。
public class MyImageLoader {

private HashMap < String, Bitmap > cache = new HashMap < String, Bitmap > ();

public static loadImage(final Activity activity, final ImageView imageView, final String id) {

new Thread(new Runnable() {
@Override
public void run() {
if (cache.containsKey(id) {
//Bitmap already exists.
} else {
//This bitmap has not been downloaded so you need to download the bitmap.
//Once bitmap is downloaded, add it to the HashMap.

}

activity.runOnUiThread(new Runnable() {
public void run() {
imageView.setImageBitmap(cache.get(id));
}
});
}
}).start();
}
}

现在,无论你做什么 imageView.setImageBitmap()图像将自动更新。您不需要单独刷新 View 。

对前面代码的一些补充:
public class MyImageLoader {

private static HashMap <String, Bitmap> cache = new HashMap <String, Bitmap>();
private static HashMap<String, DownloadListener> listeners = new HashMap()<>;
private static ArrayList<String> currentlyDownloading = new ArrayList()<>;

public static loadImage(final Activity activity, final ImageView imageView, final String id) {

DownloadListener downloadListener = new DownloadListener(){
@Override
public void onDownloadComplete(){
activity.runOnUiThread(new Runnable() {
public void run() {
imageView.setImageBitmap(cache.get(id));
}
});
}

@Override
public void onDownloadFailed(){
// Do something when download fails.
}
};

if(!listeners.containsKey(id)){
listener.put(id, new ArrayList<DownloadListener>());
}

listener.get(id).add(downloadListener);

if(cache.containsKey(id)){
for(DownloadListener listener : listeners.get(id)){
listener.onDownloadComplete();
}
} else if(!currentlyDownloading.contains(id)) {
new Thread(new Runnable() {
@Override
public void run() {
currentlyDownloading.add(id);
//Start Downloading
//Download finished
currentlyDownloading.remove(id);
//If download successful
cache.put(id, bitmap);
for(DownloadListener listener : listeners.get(id)){
listener.onDownloadComplete();
}
/*
If download failed
for(DownloadListener listener : listeners.get(id)){
listener.onDownloadFailed();
}
*/
currentlyDownloading.remove(id);
}
}).start();
}
}

public interface DownloadListener {

public void onDownloadComplete();

public void onDownloadFailed();
}
}

关于android - ListAdapter 和后台线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30874117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com