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. 
}