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);
}
}
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With