コード例 #1
0
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    VideoHolder holder;

    // The item at the current position
    final YouTubeContent.YouTubeVideo item = YouTubeContent.ITEMS.get(position);

    if (convertView == null) {
      // Create the row
      final LayoutInflater inflater =
          (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = inflater.inflate(R.layout.row_layout, parent, false);

      // Create the video holder
      holder = new VideoHolder();

      // Set the title
      holder.title = (TextView) convertView.findViewById(R.id.textView_title);
      holder.title.setText(item.title);

      // Initialise the thumbnail
      holder.thumb = (YouTubeThumbnailView) convertView.findViewById(R.id.imageView_thumbnail);
      holder.thumb.setTag(item.id);
      holder.thumb.initialize(mContext.getString(R.string.DEVELOPER_KEY), this);

      convertView.setTag(holder);
    } else {
      // Create it again
      holder = (VideoHolder) convertView.getTag();
      final YouTubeThumbnailLoader loader = mLoaders.get(holder.thumb);

      if (item != null) {
        // Set the title
        holder.title.setText(item.title);

        // Setting the video id can take a while to actually change the image
        // in the meantime the old image is shown.
        // Removing the image will cause the background color to show instead, not ideal
        // but preferable to flickering images.
        holder.thumb.setImageBitmap(null);

        if (loader == null) {
          // Loader is currently initialising
          holder.thumb.setTag(item.id);
        } else {
          // The loader is already initialised
          // Note that it's possible to get a DeadObjectException here
          try {
            loader.setVideo(item.id);
          } catch (IllegalStateException exception) {
            // If the Loader has been released then remove it from the map and re-init
            mLoaders.remove(holder.thumb);
            holder.thumb.initialize(mContext.getString(R.string.DEVELOPER_KEY), this);
          }
        }
      }
    }
    return convertView;
  }
コード例 #2
0
 @Override
 public void onInitializationSuccess(YouTubeThumbnailView view, YouTubeThumbnailLoader loader) {
   mLoaders.put(view, loader);
   loader.setVideo((String) view.getTag());
 }