0

我正在研究社交应用程序,它即将完成,但我陷入了一个图像闪烁的问题。当屏幕上有9到10幅图像时,如果我滚动页面,则会发生图像闪烁。在android中闪烁的图像

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    final ViewHolder holder; 
    if (convertView == null) { 
     LayoutInflater inf = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inf.inflate(R.layout.view_grid_explore, null); 
     holder = new ViewHolder(); 
     holder.img = (ImageView) convertView.findViewById(R.id.img_grid_album); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    ImageLoader.getInstance().displayImage(
      Static_Urls.explore_pic + data.get(position).talk_pic, 
      holder.img); 
    convertView.setTag(holder); 

    notifyDataSetChanged(); 
    return convertView; 
} 
+1

先取下notifyDataSetChanged()行... – Mike

+0

感谢迈克其工作:当你建立自己一个“刷新()”方法适配器内部就像一个例子是。 –

回答

0
  • 注意:不要忘记删除notifyDataSetChanged();

发生这种情况是因为一旦图像通过UIL(通用图像加载程序)下载到设备中,图像就会将图像缓存到内存和设备中。

通过使用此代码:

ImageLoader.getInstance().displayImage(Static_Urls.explore_pic +data.get(position).talk_pic, 
      holder.img); 

每次getView()被称为UIL尝试获取来自网络的图像,但当时它释放出的图像已经被缓存,以便它显示制作后的图像网络请求优先。

所以为了摆脱这种闪烁使用此代码:

ImageLoader imageLoader = ImageLoader.getInstance(); 

     File file = imageLoader.getDiskCache().get(Static_Urls.explore_pic +data.get(position).talk_pic); 
     if (file==null) { 
      //Load image from network 
      imageLoader.displayImage(Static_Urls.explore_pic +data.get(position).talk_pic, 
      holder.img); 
     } 
     else { 
      //Load image from cache 
      holder.img.setImageURI(Uri.parse(file.getAbsolutePath())); 
     } 

该代码会先检查图像是否已经缓存与否,然后据此从网络或从缓存中获取图像。

+0

感谢您的回复,但我怎么解释getDiskCache()它显示错误.. –

+0

你使用哪个UIL版本。 –

+0

libs/universal-image-loader-1.9.3-with-sources.jar使用这个 –

0

notifyDataSetChanged()这条线在那里是多余的。使用适配器始终记住(在适配器扩展BaseAdapter的情况下),getView()方法负责扩充列表项的布局,并且如果处理它,也会更新UI(通常您会这样做)

调用notifyDataSetChanged()将导致getView()被再次调用,这就是为什么你看到闪烁。

当您想要更新适配器内容时,您只应该致电notifyDataSetChanged()

public void refresh(List<Object> list) { 
    data.clear();// Assuming data is a List<> object or an implementation of it like ArrayList(); 
    data.addAll(list); 
    notifyDataSetChanged(); // This will let the adapter know that something changed in the adapter and this change should be reflected on the UI too, thus the getView() method will be called implicitly. 
}