我的问题用文字描述并不容易,但我会尽我所能。我有一个ListView,显示YouTube视频的缩略图。 基本上这些缩略图确实正确加载(意味着每个缩略图都显示在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;
}
为什么会这样?也许是列表视图的一些缓存(因为它在切换图像之前总是显示以前的缩略图之一)?
答案 0 :(得分: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);
}