【问题标题】:Android BaseAdapter With LruCache Some ui problemsAndroid BaseAdapter 和 LruCache 一些 ui 问题
【发布时间】:2014-12-11 14:53:35
【问题描述】:

我正在尝试在基本适配器和 LruCache 的帮助下实现包含所有联系人图像的列表视图。但是在屏幕上长滚动时,所有图像(对应于该视图)都会在设置实际图像之前显示。

例如:每页 5 个项目的列表视图,如果我们从第一个联系人滚动到第 60 个,在列表视图的第一个视图中,1、6、11、16、21..51 的图像会显示几毫秒之前显示第 55 张图片

主要代码是

//Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = (ImageView) convertView;
if(imageView == null){
 imageView = new ImageView(getActivity());
}    
int id =  contactId[position];
final String imageKey = String.valueOf(contactId);
final Bitmap bitmap = cache.get(imageKey);
if (bitmap != null) {
  imageView.setImageBitmap(bitmap);
} else {    
  Resources res = context.getResources();
  BitmapManager bm = new BitmapManager(imageView, res, cache);
  bm.setContext(getActivity());
  bm.execute(id);
}
return imageView;
}

BitmapManager 后执行代码

@Override
protected void onPostExecute(Bitmap bitmap) {
    // TODO Auto-generated method stub
    try{
        if(isCancelled()){
            bitmap = null;
        }
        if(imageViewReference != null && bitmap != null){
            ImageView imageView = imageViewReference.get();
            imageView.setImageBitmap(bitmap);
            cache.put(String.valueOf(res), bitmap);
            if(imageView != null){
                imageView.setImageBitmap(bitmap);
            }

        }
    }catch(Exception e){

    }
    super.onPostExecute(bitmap);
}

如何解决这个问题。 谢谢

【问题讨论】:

    标签: android android-listview baseadapter android-lru-cache


    【解决方案1】:

    当您向下滚动时,您的视图会被重新用于出现在视图中的新列表位置。由于每次调用 getView 时都会启动一个新的 BitmapManager 任务,因此这些任务会排成一列,等待更新图像。当他们每个人完成加载他们的位图时,他们将它按顺序放入 ImageView 中,这就是您所看到的。

    看起来您试图使用对 ImageView 的引用来避免在位图滚动出视图后使用它,但不起作用的原因是适配器正在回收您的 ImageView,因此引用保持有效,即使实际上 ImageView 现在正在用于不同的列表项。

    有不同的方法可以解决这个问题,但想到的最简单的方法是使用列表索引而不是 ImageView 本身来构建您的 BitmapManager。然后在 getView 中,您可以保留在哪些位置使用哪些视图的地图。当 BitmapManager 完成时,检查您刚刚加载到缓存中的位置是否存在当前 ImageView。如果没有,那么什么也不做。

    这里有一些代码显示了我在说什么。没试过,如有错误请见谅。

    //Adapter
    private SparseArray<ImageView> ivMap = new SparseArray<ImageView>();
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = (ImageView) convertView;
        if(imageView == null){
          imageView = new ImageView(getActivity());
        } else {
          // If recycled, remove the ImageView's previous position from map
          int oldPosition = ivMap.indexOfValue(imageView);
          if (oldPosition >= 0) {
            ivMap.remove(oldPosition);
          }
        }
        // Keep track of which view is representing this position
        ivMap.put(position, imageView);
    
        int id =  contactId[position];
        final String imageKey = String.valueOf(contactId);
        final Bitmap bitmap = cache.get(imageKey);
        if (bitmap != null) {
          imageView.setImageBitmap(bitmap);
        } else {    
          Resources res = context.getResources();
          BitmapManager bm = new BitmapManager(ivMap, position, res, cache);
          bm.setContext(getActivity());
          bm.execute(id);
        }
        return imageView;
    }
    
    //BitmapManager
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        // TODO Auto-generated method stub
        try{
            if(isCancelled()){
                bitmap = null;
            }
            if(bitmap != null){
                cache.put(String.valueOf(res), bitmap);
                ImageView imageView = ivMap.get(position);
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }catch(Exception e){
    
        }
        super.onPostExecute(bitmap);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-25
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      相关资源
      最近更新 更多