Adding Youtube API with RecyclerView using Firebas

2020-05-06 11:57发布

问题:

I am building an application in which I want to use Youtube API for running videos but the problem is that the youtube API is copied to all card items where I dont want it to come, I only want a specific post where the video can be played and not on all posts. I am also using RecyclerView and cardView in this application with Firebase as my database.please help me reduce this redundancy problem, I have researched many questions but did not found anything useful. Is there any way by which the video can only be played on some post of cardView and not on all. for example facebook application or instagram application where videos come on specific posts and not on all.

I am using Android Studio

Youtube FirebaseAdapter class file:

  FirebaseRecyclerAdapter <post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(

        post.class,
        R.layout.post_row_recycle_home,
        postViewHolder.class,
        mDatabaseReference
) {
    @Override
    protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
        viewHolder.setYoutube(model.getYoutube());
    }
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);


}

public static class postViewHolder extends RecyclerViewPager.ViewHolder{

View mView;

public postViewHolder(View itemView) {
    super(itemView);
    mView = itemView;
}
public void setYoutube(final String youtube){
    final YouTubePlayerView youPlay = (YouTubePlayerView) mView.findViewById(R.id.youtuber);
    youPlay.initialize("SOME KEY",
            new YouTubePlayer.OnInitializedListener() {
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                                    YouTubePlayer youTubePlayer, boolean b) {

                    youTubePlayer.cueVideo(youtube);
                }
                @Override
                public void onInitializationFailure(YouTubePlayer.Provider provider,
                                                    YouTubeInitializationResult youTubeInitializationResult) {

                }
            });
}

youtube CardView xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/CardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.google.android.youtube.player.YouTubePlayerView
            android:id="@+id/youtuber"
            android:layout_width="match_parent"
            android:layout_height="200dp" />