Put Youtube videos on RecyclerView-CardView Adapter Android

2

Good afternoon, I'm trying to put YouTube videos on a CardView using the Youtube API, but it does not show me anything.

This is my xml:

     

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="5dp"
    android:id="@+id/CardViewComoSeHace"
    android:layout_margin="9dp"
    android:elevation="5dp"
    >

    <com.google.android.youtube.player.YouTubeThumbnailView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:id="@+id/youtubeThumb"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        />


        <me.grantland.widget.AutofitTextView
            android:text="descripcion_video"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="2"
            app:minTextSize="13sp"
            android:textSize="15sp"
            android:ellipsize="end"
            android:id="@+id/txtComoSeHace"
            android:layout_marginTop="167dp"
            android:layout_gravity="center_horizontal"
            android:textColor="#000"
            />

</android.support.v7.widget.CardView>

This is my RecyclerView adapter:

 public class ProductocomosehaceAdaptador extends RecyclerView.Adapter<ProductocomosehaceAdaptador.ProdComoseHaceViewHolder> {


private ArrayList<ProductocomosehaceBean> bean;
//Acá va la APIKEY, no lo pongo solo para no mostrarlo.
private static String API_KEY = "MyApiKey";


public ProductocomosehaceAdaptador(ArrayList<ProductocomosehaceBean> prod){
        this.bean = prod;
 }


@Override
public ProdComoseHaceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_recycler_comosehace,parent,false);

    return new ProdComoseHaceViewHolder(view);
}

@Override
public void onBindViewHolder(final ProdComoseHaceViewHolder holder, int position) {
    //holder.imagen.setImageUrl(bean.get(position).getImagen());
    //Picasso.with(context).load(bean.get(position).getImagen()).into(holder.imagen);

    final int i = position;

    holder.descrip.setText(bean.get(position).getDescripcion());
    Log.i("Link", bean.get(position).getLink());
    Log.i("Imagen",bean.get(position).getImagen());


  //poner videos..

    YouTubeThumbnailView youTubeThumbnailView = (YouTubeThumbnailView) holder.view.findViewById(R.id.youtubeThumb);
    youTubeThumbnailView.initialize(API_KEY, new YouTubeThumbnailView.OnInitializedListener(){


        @Override
        public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) {
            youTubeThumbnailLoader.setVideo(bean.get(i).getLink());
            youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
                @Override
                public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
                    youTubeThumbnailLoader.release();

                }

                @Override
                public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
                    Log.e("ErrorReason", errorReason.toString());

                }
            });

        }

        @Override
        public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {

        }
    });


}

@Override
public int getItemCount() {
    return bean.size();
}

public static class ProdComoseHaceViewHolder extends RecyclerView.ViewHolder {

    CardView cardView;
    //SmartImageView imagen;
    AutofitTextView descrip;
    YouTubeThumbnailView view;



    public ProdComoseHaceViewHolder(View itemView) {
        super(itemView);

        cardView = (CardView) itemView.findViewById(R.id.CardViewComoSeHace);
        //imagen = (SmartImageView) itemView.findViewById(R.id.SmartImageProductoComoSeHace);
        descrip = (AutofitTextView) itemView.findViewById(R.id.txtComoSeHace);
        view = (YouTubeThumbnailView) itemView.findViewById(R.id.youtubeThumb);

        Typeface type = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Roboto-Medium.ttf");
        descrip.setTypeface(type);

          }
       }
   }

I'm getting the url of the videos from Firebase, in the part:

Log.i ("Link", bean.get (position) .getLink ()); I get the links perfectly. This log booted me these 2 links that work correctly:

uxVQfkW_fCk

wnUAVyhgRRo

Almost all the information I get from Firebase works perfectly, like for example the variable "descrip", that works normal; what does not work is when putting the YouTube video in the cardView.

This is the result I have now:

Then finally my query would be, in case you are doing wrong when trying to put the YouTube videos in the CardView then, what would be the way to put the videos?

    
asked by Jorge Requez 15.02.2017 в 20:22
source

1 answer

0
  

Log.i ("Link", bean.get (position) .getLink ()); you get me the links   perfectly. This log booted me these 2 links that work   correctly:

     

uxVQfkW_fCk

     

wnUAVyhgRRo

You are getting the id of the videos through the API, but what you really need is the path of the image so that you can add it to YouTubeThumbnailView , for this you need to complete the thumbnail path:

https://img.youtube.com/vi/<id video>/default.jpg

or the first image:

https://img.youtube.com/vi/<id video>/0.jpg

In the case of the example that shows the url would be:

link

link

link

link

    
answered by 15.02.2017 / 21:24
source