Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube Video is not Displaying in RecyclerView

I am facing issue where youtube video is not displaying only last one is getting played and that too when I scroll the video disappears. What i wanted is every video should be displayed and every video is different.

public class MyRecyclerViewAdapter extends Adapter<MyRecyclerViewAdapter
.DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<DataObject> mDataset;
private static MyClickListener myClickListener;
private static final int RECOVERY_DIALOG_REQUEST = 10;
public  static  String key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
Context ctx;
View view;

public static class DataObjectHolder extends ViewHolder
    implements View
    .OnClickListener {
    YouTubePlayerView youTubeView;
public DataObjectHolder(View itemView) {
    super(itemView);
    youTubeView = (YouTubePlayerView) itemView.findViewById(R.id.youtube_view);
    Log.i(LOG_TAG, "Adding Listener");
    itemView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    myClickListener.onItemClick(getAdapterPosition(), v);
}
}

public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}

public MyRecyclerViewAdapter(Context context,ArrayList<DataObject> myDataset) {
    mDataset = myDataset;
    ctx=context;
}

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

DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}



@Override
public void onBindViewHolder(DataObjectHolder holder, final int position) {
holder.youTubeView.initialize(key, new YouTubePlayer.OnInitializedListener() {
    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean b) {
        player.cueVideo(mDataset.get(position).getmText1());
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog((Activity) ctx, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format("YouTube Error (%1$s)",
                    errorReason.toString());
            Toast.makeText(ctx, errorMessage, Toast.LENGTH_LONG).show();
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECOVERY_DIALOG_REQUEST) {
            // Retry initialization if user performed a recovery action
            getYouTubePlayerProvider().initialize(key, this);
        }
    }

    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        Toast.makeText(ctx, view.toString(), Toast.LENGTH_LONG).show();
        return (YouTubePlayerView) view.findViewById(R.id.youtube_view);
    }


});
}

public void addItem(DataObject dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}

public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}

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

public interface MyClickListener {
public void onItemClick(int position, View v);
}
}
like image 617
Ravi Avatar asked Apr 07 '16 11:04

Ravi


People also ask

How many types of RecyclerView in Android?

Android RecyclerView Multiple ViewType Project Structure We'll be implementing three view types (text, image, audio) that are inflated by three different layouts. Each has its own implementation specified in the adapter class.

When to use Recycler view?

Android recyclerview is the most advanced version of the listview. basically, an android listview is used to present a simple data set. if you want to display large data set in your app, you should use recyclerview.


Video Answer


1 Answers

You cannot do that as the YoutubePlayer view is too large to be added to a recyclerview. Use a YouTubeThumbnailView instead to display the thumbnails. When the user clicks on one of them, you can start a YouTubePlayerFragment or an activity with a YouTubeplayerView view.

In your case this would be like this

public class MyRecyclerViewAdapter extends Adapter<MyRecyclerViewAdapter
.DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<DataObject> mDataset;
private static MyClickListener myClickListener;
private static final int RECOVERY_DIALOG_REQUEST = 10;
public  static  String key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
Context ctx;
View view;

public static class DataObjectHolder extends ViewHolder
    implements View
    .OnClickListener {
    YouTubeThumbnailView youTubeThumbnailView ;
public DataObjectHolder(View itemView) {
    super(itemView);
    youTubeThumbnailView = (YouTubeThumbnailView ) itemView.findViewById(R.id.yourId);
    itemView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    myClickListener.onItemClick(getAdapterPosition(), v);
}
}

public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}

public MyRecyclerViewAdapter(Context context,ArrayList<DataObject> myDataset) {
    mDataset = myDataset;
    ctx=context;
}

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

DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}



@Override
public void onBindViewHolder(DataObjectHolder holder, final int position) {

holder.youTubeThumbnailView.initialize(key, new YouTubePlayer.OnInitializedListener() {
    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean b) {
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog((Activity) ctx, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format("YouTube Error (%1$s)",
                    errorReason.toString());
            Toast.makeText(ctx, errorMessage, Toast.LENGTH_LONG).show();
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECOVERY_DIALOG_REQUEST) {
            // Retry initialization if user performed a recovery action
            getYouTubePlayerProvider().initialize(key, this);
        }
    }

    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        Toast.makeText(ctx, view.toString(), Toast.LENGTH_LONG).show();
        return (YouTubePlayerView) view.findViewById(R.id.youtube_view);
    }


});
}

public void addItem(DataObject dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}

public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}

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

public interface MyClickListener {
public void onItemClick(int position, View v);
}
}

Edit Thanks to Irfan Raza for pointing this out. See this answer, it seems as if it can done

like image 180
Niza Siwale Avatar answered Nov 15 '22 06:11

Niza Siwale