I'm trying to load Youtube video thumbnails in a RecyclerView. I'm facing some issues.
Here is what I'm doing in my adapter:
public static class ItemViewHolder extends RecyclerView.ViewHolder {
private YouTubeThumbnailView thumb;
public Post post;
public ItemViewHolder(View v) {
thumb = (YouTubeThumbnailView) v.findViewById(R.id.youtube_thumbnail);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ItemViewHolder) {
((ItemViewHolder) holder).thumb.initialize(YOUTUPEKEY, new YouTubeThumbnailView.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
youTubeThumbnailLoader.setVideo(VIDEOID);
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
}});
}}}
It works fine, but I don't I'm doing it right. When I'm using the same adapter in another activity I get this Error:
Activity com.example.yasser.version6.Mespublications has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@4252bcb8 that was originally bound here
and it takes time to load thumbnails and sometimes it mix between them when swiping.
I added a function to release all the Youtube Loaders:
public void ReleaseLoaders() {
for (YouTubeThumbnailLoader loader : loaders.values()) {
loader.release();
}
}
and I'm calling this function from the Activity Onstop() :
@Override
public void onStop() {
super.onStop();
mAdapter.ReleaseLoaders();
}
It worked fine for some time, but the crashes sometimes.