1

我的問題不容易用文字描述,但我會盡我所能。我有一個顯示YouTube視頻縮略圖的ListView。 基本上這些縮略圖的確可以正確加載(意味着每個縮略圖都顯示了其對應的ListView行),但在滑動列表時會發生奇怪的事情。 每當新的縮略圖進入屏幕時向下滑動列表(=從列表頂部開始到結束),它在顯示幾毫秒之前顯示錯誤的縮略圖,然後切換到正確的縮略圖。 之前,錯誤的縮略圖始終是兩行或三行的縮略圖。在向上/向後滑動列表時,所有內容立即以正確的方式顯示。Youtube縮略圖在使用官方YouTube Android播放器API的ListView中閃爍

我絕對不知道從哪裏開始這樣做的搜索,但我想我的ListView適配器可能會感興趣:

private final Map<View, YouTubeThumbnailLoader> thumbnailViewToLoaderMap; 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    SuggestionViewHolder holder; 

    Suggestion suggestion = getItem(position); 
    String videoId = suggestion.getYoutubeId(); 

    // There are three cases here: 
    if (convertView == null) { 
     convertView = layoutInflater.inflate(R.layout.row_suggestion, parent, false); 

     holder = new SuggestionViewHolder(); 

     // 1) The view has not yet been created - we need to initialize the YouTubeThumbnailView. 
     holder.thumbnailView = (YouTubeThumbnailView) convertView.findViewById(R.id.youtubeThumbnail); 
     holder.thumbnailView.setTag(videoId); 
     holder.thumbnailView.initialize(DeveloperKey.DEVELOPER_KEY, this); 

     convertView.setTag(holder); 

    } else { 

     holder = (SuggestionViewHolder) convertView.getTag(); 

     // 2) and 3) The view is already created... 
     YouTubeThumbnailLoader loader = thumbnailViewToLoaderMap.get(holder.thumbnailView); 

     // ...and is currently being initialized. We store the current videoId in the tag. 
     if (loader == null) { 
      holder.thumbnailView.setTag(videoId); 

     // ...and already initialized. Simply set the right videoId on the loader. 
     } else { 
      loader.setVideo(videoId); 

     } 
    } 

    return convertView; 
} 


@Override 
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) { 
    String videoId = (String) youTubeThumbnailView.getTag(); 

    thumbnailViewToLoaderMap.put(youTubeThumbnailView, youTubeThumbnailLoader); 

    youTubeThumbnailLoader.setOnThumbnailLoadedListener(this); 
    youTubeThumbnailLoader.setVideo(videoId); 
} 


private static class SuggestionViewHolder { 
    public YouTubeThumbnailView thumbnailView; 
} 

任何想法,爲什麼出現這種情況?也許一些緩存的列表視圖的東西(因爲它總是顯示之前的縮略圖之一,在切換圖像之前)?

回答

1

這是因爲加載程序需要時間來顯示縮略圖。當單元格被重用時,它會保留前一個單元格的圖像,因此如果加載程序需要時間,它會在加載新圖像之前顯示舊圖像一段時間。調用裝載程序之前,您必須清除它。

holder = (SuggestionViewHolder) convertView.getTag(); 

    // 2) and 3) The view is already created... 
    YouTubeThumbnailLoader loader = thumbnailViewToLoaderMap.get(holder.thumbnailView); 

    // ...and is currently being initialized. We store the current videoId in the tag. 
    if (loader == null) { 
     holder.thumbnailView.setTag(videoId); 

    // ...and already initialized. Simply set the right videoId on the loader. 
    } else { 
     holder.thumbnailView.setImageBitmap(null); 
     loader.setVideo(videoId); 

    } 
+0

是的!現在這是有道理的,謝謝! – muetzenflo

相關問題