列表视图中的YouTubeThumbnailView

时间:2013-10-28 21:35:39

标签: android listview youtube thumbnails

我正在使用YouTube API播放来自YouTube (显然)的视频,这些视频到目前为止运作良好。我遇到的问题是我使用列表视图来显示不同的视频标题和缩略图,这需要自定义适配器和YouTubeThumbnailView。

现在我的代码运行得很好,因为它加载缩略图并将它们正确地存储在Map中,但是当重新创建视图并且需要再次从Map中检索缩略图时,它有时会显示错误的缩略图和经常重复。

这是适配器代码:

public class VideoListAdapter extends BaseAdapter implements
    YouTubeThumbnailView.OnInitializedListener{

Map<View, YouTubeThumbnailLoader> mLoaders;

public VideoListAdapter(final Context context, final List<Video> list, final int layoutResourceId) {
    mList = VideoManager.getInstance().getContentList();
    this.mLayID = layoutResourceId;
    this.mContext = context;
    mList = list;
    mLoaders = new HashMap<View, YouTubeThumbnailLoader>();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View mCurrentRow = convertView;
    PostHolder holder;
    String videoId = mList.get(position).videoId;

    if(mCurrentRow == null)
    {
        LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
        mCurrentRow = inflater.inflate(mLayID, parent, false);

        holder = new PostHolder();
        holder.txtTitle = (TextView)mCurrentRow.findViewById(R.id.list_content_title);
        holder.txtTitle.setText(mList.get(position).title);

        //Case 1 - Initalise the thumbnail
        holder.thumb = (YouTubeThumbnailView) mCurrentRow.findViewById(R.id.list_content_thumb);
        holder.thumb.setTag(videoId);
        holder.thumb.initialize(Utils.DEVELOPER_KEY, this);

        mCurrentRow.setTag(holder);
    }
    else
    {
        holder = (PostHolder) mCurrentRow.getTag();
        YouTubeThumbnailLoader loader = mLoaders.get(holder.thumb);

        //Set the title
        Video post = mList.get(position);
        if(post != null){
            holder.txtTitle.setText(post.title);
        }

        if (holder.loader == null)
        {
            //Case 2 - Loader is currently initializing
           holder.thumb.setTag(videoId);
        } else
        {
            //Case 3 - The loader is already initialised
            holder.thumb.setImageResource(R.drawable.ic_launcher);
            holder.loader.setVideo(videoId);
        }
    }
    return mCurrentRow;
}

@Override
public void onInitializationSuccess(YouTubeThumbnailView view, YouTubeThumbnailLoader loader) {
    String videoId = (String) view.getTag();
    mLoaders.put(view, loader);
    view.setImageResource(R.drawable.ic_launcher);
    loader.setVideo(videoId);
}

@Override
public void onInitializationFailure(
        YouTubeThumbnailView thumbnailView, YouTubeInitializationResult errorReason) {
    if (errorReason.isUserRecoverableError()) {
        if (errorDialog == null || !errorDialog.isShowing()) {
            //errorDialog = errorReason.getErrorDialog(, RECOVERY_DIALOG_REQUEST);
            errorDialog.show();
        }
    } else {
        /*String errorMessage =
                String.format(getString(R.string.error_thumbnail_view), errorReason.toString());
        Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();*/
    }
}

static class PostHolder
{
    YouTubeThumbnailView thumb;
    YouTubeThumbnailLoader loader;
    TextView txtTitle;
    TextView txtTime;
}

我已经阅读了文档中的内容,并且我已经看了示例应用程序,但是我找不到任何可以帮助我的内容。这可能不是YT API的问题,但是我存储ImageViews的方式存在问题。

如果有人能提供任何帮助,我将不胜感激,提前谢谢。

1 个答案:

答案 0 :(得分:3)

您不能在PostHolder类中保存YouTubeThumbnailLoader,而是让mLoaders HashMap处理所有加载器。正如您在评论中意识到的那样,您需要使用此HashMap通过回调设置加载器。因此,将getView() - else的else部分更改为:

     } else {

        holder = (PostHolder) convertView.getTag();

        //Set the title
        Video post = mList.get(position);
        if(post != null){
            holder.txtTitle.setText(post.title);
        }

        // 2) and 3) The view is already created...
        YouTubeThumbnailLoader loader = mLoaders.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.setImageResource(R.drawable.ic_launcher);
            loader.setVideo(videoId);

        }
    }

您的帖子持有人课程不需要加载器:

 static class PostHolder
 {
   YouTubeThumbnailView thumbnailView;
   TextView txtTitle;
   TextView txtTime;
 }