gpt4 book ai didi

android - Youtube 缩略图在使用官方 YouTube Android Player API 的 ListView 中闪烁

转载 作者:行者123 更新时间:2023-11-30 03:07:56 28 4
gpt4 key购买 nike

我的问题不容易用语言描述,但我会尽力而为。我有一个显示 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;
}

知道为什么会这样吗?也许是 ListView 的一些缓存内容(因为在切换图像之前它总是显示以前的缩略图之一)?

最佳答案

发生这种情况是因为加载程序需要时间来显示缩略图。当单元格被重复使用时,它会保留前一个单元格的图像,因此如果加载程序需要时间,它会在加载新图像之前显示旧图像一段时间。你必须在调用加载程序之前清除它。

    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);

}

关于android - Youtube 缩略图在使用官方 YouTube Android Player API 的 ListView 中闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21467216/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com