【发布时间】:2026-02-08 03:30:01
【问题描述】:
我有一个 listView 和一个适配器,
我已滚动到列表的中间(假设我们有 100 项我在第 50 项中)
到达那里后,我会从服务器获得一些更新.. 比如说,来自 facebook 的新故事..
A. 我想调用 notifyDataSetChanged() 并保持位置-因为我使用了this code
B. 我正在使用 volley 库中可爱的 NetworkImageView,我希望,当调用 notifyDataSetChanged 时 - 图像不会像现在这样重新加载,因为,(也许那个是我的问题的根源),目前,重新加载图像会导致用户出现某种闪烁(未加载照片)
编辑:
mQueue = Volley.newRequestQueue(getApplicationContext());// thread pool(4)
mngr.setRequestQueue(mQueue);
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
mImageLoader = new ImageLoader(mQueue, new ImageCache() {
private final LruBitmapCache mCache = new LruBitmapCache(maxMemory);
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});
我的解决方案:
// final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
mImageLoader = new ImageLoader(mQueue, new ImageCache() {
private final BitmapLruCache mCache = new BitmapLruCache();
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});
我使用了下一个bimtap lru缓存实现
public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageCache {
public static int ONE_KILOBYTE = 1024;
public BitmapLruCache() {
this(getDefaultLruCacheSize());
}
public BitmapLruCache(int maxSize) {
super(maxSize);
}
public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / ONE_KILOBYTE);
final int cacheSize = maxMemory / 8;
return cacheSize;
}
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight() / ONE_KILOBYTE;
}
@Override
public Bitmap getBitmap(String url) {
return get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
}
你们觉得呢?
10 倍
【问题讨论】:
-
请说明您是如何初始化排球队列、图像加载器和适配器代码的。
-
@Pork 我添加了初始化细节
标签: android android-arrayadapter android-volley networkimageview