0

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

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

这是适配器代码:

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 的问题,而是我存储 ImageView 的方式的问题。

如果有人可以提供任何帮助,我将不胜感激,在此先感谢。

4

1 回答 1

3

您不能将 YouTubeThumbnailLoader 保存在 PostHolder 类中,而是让所有加载程序由 mLoaders HashMap 处理。正如您在评论中所意识到的,您需要此 HashMap 通过回调设置加载程序。因此,将 getView() 方法的 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);

        }
    }

您的 Post Holder 类不需要加载器:

 static class PostHolder
 {
   YouTubeThumbnailView thumbnailView;
   TextView txtTitle;
   TextView txtTime;
 }
于 2014-01-29T20:55:31.583 回答