我使用a ListView来显示与这些图像相关的一些图像和标题.我从互联网上获取图像.有没有办法延迟加载图像,以便文本显示时,UI不会被锁定,图像会在下载时显示?
图像总数不固定.
我正在使用自定义适配器处理listview.我想加载图像和文本视图.图像是从互联网网址加载的.我只是想显示可见列表项的图像以供用户使用.我提到了来自romainguy的Shelves开源项目示例,但它很难理解代码.对于初学者级别,我只想知道如何处理适配器和活动之间的标记.从commonsware 示例我可以在适配器上设置标签,但不能在listview的空闲状态下显示相应的图像.请帮我解决你的想法.示例代码更容易理解.
谢谢.
编辑:
对高效适配器示例所做的更改如下:
public class List14 extends ListActivity implements ListView.OnScrollListener {
// private TextView mStatus;
private static boolean mBusy = false;
static ViewHolder holder;
public static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Bitmap mIcon1;
private Bitmap mIcon2;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
// Icons bound …Run Code Online (Sandbox Code Playgroud) 我在这里找到了Fedor的代码并将其实现到我的项目中.唯一的区别是我的应用程序没有列表视图,而是我从服务器一次访问1个图像.当活动启动时,我调用"DisplayImage(...)"来显示第一张图片.然后有2个按钮(上一个/下一个),当点击它们时,它们称为"DisplayImage(...)".
它工作正常一段时间,但后来我得到一个Out of Memory错误.在他的代码的顶部,他评论您可能想要使用SoftReference.我假设这会解决我的问题,对吗?我玩了一下但是当我尝试修改它以使用SoftReference时,图像永远不会加载.我之前从未使用过SoftReference所以我认为我只是缺少一些东西.我如何修改该代码(ImageLoader)来修复我的OOM错误?在浏览图片时是否有更好的缓存方式?
更新: 以下是您不想查看源中的其他文件的代码.
package com.fedorvlasov.lazylist;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Stack;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
public class ImageLoader {
//the simplest in-memory cache implementation. This should be replaced with something like SoftReference or BitmapOptions.inPurgeable(since 1.6)
private HashMap<String, Bitmap> cache=new HashMap<String, Bitmap>();
private File cacheDir;
public ImageLoader(Context context){
//Make the background thead low priority. This way it will …Run Code Online (Sandbox Code Playgroud)