您應該只調用一次,但在初始化後保存對加載器的引用。解決這個問題的一種方法是將地圖從View
保存到YouTubeThumbnailLoader
。在getView
中有3種情況:
- 視圖是新的,需要初始化。
- 視圖已經初始化,我們有一個對加載器的引用。
- 該視圖目前正在初始化。
在情況1和3中,您需要記住加載程序在初始化時應該執行的操作。你可以例如將視頻ID保存在視圖的標記中。
示例代碼:
Map<View, YouTubeThumbnailLoader> loaders;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
String videoId = getVideoId(position);
if (view == null) {
// Case 1 - We need to initialize the loader
view = inflater.inflate(..., parent, false);
YouTubeThumbnailView thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.thumbnail);
thumbnail.setTag(videoId);
thumbnail.initialize(DeveloperKey.DEVELOPER_KEY, thumbnailListener);
} else {
YouTubeThumbnailView thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.thumbnail);
YouTubeThumbnailLoader loader = loaders.get(thumbnail);
if (loader == null) {
// Case 3 - The loader is currently initializing
thumbnail.setTag(videoId);
} else {
// Case 2 - The loader is already initialized
thumbnail.setImageResource(R.drawable.loading_thumbnail);
loader.setVideo(videoId);
}
}
return view;
}
而在你thumbnailListener:
@Override
public void onInitializationSuccess(YouTubeThumbnailView view, YouTubeThumbnailLoader loader) {
String videoId = (String) view.getTag();
loaders.put(view, loader);
view.setImageResource(R.drawable.loading_thumbnail);
loader.setVideo(videoId);
}
我遇到了一些問題,因爲我使用了錯誤的實現類的地圖,但是當我得到了整理出來,它工作正常。感謝您的解決方案! –
完美,像魅力一樣工作 – khurramengr