【问题标题】:Youtube thumbnails flicker in ListView using official YouTube Android Player API使用官方 YouTube Android Player API 在 ListView 中的 Youtube 缩略图闪烁
【发布时间】:2014-02-23 09:20:09
【问题描述】:

我的问题不容易用语言描述,但我会尽力而为。我有一个显示 YouTube 视频缩略图的 ListView。 基本上这些缩略图可以正确加载(这意味着每个缩略图都显示在 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;
}

任何想法为什么会发生这种情况?可能是列表视图的一些缓存内容(因为它总是显示之前的缩略图之一,在切换图像之前)?

【问题讨论】:

    标签: android listview android-listview youtube android-youtube-api


    【解决方案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);
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-03
      • 2016-03-26
      • 1970-01-01
      • 2018-11-14
      • 2015-12-31
      • 2014-02-02
      相关资源
      最近更新 更多