your app will crash When you try to inflate view with same Fragment
every ListView
item (As duplication in fragment id like this quiz)
There Two solution (one of them from @ivagarz answer) :
1- You can display only one youtube video Using YouTubePlayerView
above your ListView
into your Activity
layout But your Activity
Must extends YouTubeBaseActivity
Like YouTube Android Player API Doc (I Think this Solution will not help you to solve your problem)
2- you can use YouTubeThumbnailView
to Display youtube Thumbnail Image of Video for Every Item into ListView
with youtube player button over youtube Thumbnail Image Used to Display Video.
Like The next Picture :

When User click on youtube player button, You Have two ways:
First option using YouTubePlayerSupportFragment
: you can replacing YouTubeThumbnailView
with YouTubePlayerSupportFragment
If you take This Option you must :
A- add YouTubePlayerSupportFragment
programmatically on the Fly (to avoid duplication in Fragment
Id).
B- Display Only one Video into your ListView
As answer of this Question.
Ex:
list_item.xml
layout for ListView
item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.youtube.player.YouTubeThumbnailView
android:id="@+id/youtube_thumbnail"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:visibility="gone"/>
<RelativeLayout android:id="@+id/relativeLayout_over_youtube_thumbnail"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:background="@color/color_background_transparent"
android:visibility="gone">
<ImageView android:id="@+id/btnYoutube_player"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
android:src="@drawable/youtube_player"/>
</RelativeLayout>
</RelativeLayout>
in getView
mehtod of ListView
adapter:
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflator.inflate(R.layout.list_item.xml, parent, false);
final RelativeLayout relativeLayoutOverYouTubeThumbnailView = (RelativeLayout) convertView.findViewById(R.id.relativeLayout_over_youtube_thumbnail);
final YouTubeThumbnailView youTubeThumbnailView = (YouTubeThumbnailView) convertView.findViewById(R.id.youtube_thumbnail);
// get parent relative layout
RelativeLayout parentRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.parent_relativeLayout);
// then create dynamic FrameLayout
FrameLayout.LayoutParams dynamicFrameLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
final FrameLayout dynamicFrameLayout = new FrameLayout(ctx);
dynamicFrameLayout.setId(Different_ID);
dynamicFrameLayout.setLayoutParams(dynamicFrameLayoutParams);
// then add dynamic FrameLayout as children in parent relative layout
parentRelativeLayout.addView(dynamicFrameLayout);
ImageView youtubePlayerButton = (ImageView) convertView.findViewById(R.id.btnYoutube_player);
youtubePlayerButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View parentView = (View) v.getParent().getParent();
YouTubeThumbnailView youTubeThumbnailView = (YouTubeThumbnailView) parentView.findViewById(R.id.youtube_thumbnail);
RelativeLayout relativeLayoutOverYouTubeThumbnailView = (RelativeLayout) parentView.findViewById(R.id.relativeLayout_over_youtube_thumbnail);
youTubeThumbnailView.setVisibility(View.GONE);
relativeLayoutOverYouTubeThumbnailView.setVisibility(View.GONE);
YouTubeFragment youTubeFragment = new YouTubeFragment();
youTubeFragment.youTubeFragmentInitialize(VideoID, youTubeFragment, parentView);
getSupportFragmentManager()
.beginTransaction()
.replace(dynamicFrameLayout.getId(), youTubeFragment)
.commit();
}
});
final YouTubeThumbnailLoader.OnThumbnailLoadedListener onThumbnailLoadedListener = new YouTubeThumbnailLoader.OnThumbnailLoadedListener(){
@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
}
@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
loadingImage.setVisibility(View.GONE);
youTubeThumbnailView.setVisibility(View.VISIBLE);
relativeLayoutOverYouTubeThumbnailView.setVisibility(View.VISIBLE);
}
};
youTubeThumbnailView.initialize(API_KEY, new YouTubeThumbnailView.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
youTubeThumbnailLoader.setVideo(VideoID);
youTubeThumbnailLoader.setOnThumbnailLoadedListener(onThumbnailLoadedListener);
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
}
});
}
I was created my own Fragment
extends from YouTubePlayerSupportFragment
:
public class YouTubeFragment extends YouTubePlayerSupportFragment {
public void youTubeFragmentInitialize(final String videoId, final YouTubeFragment fragment, final View parent) {
fragment.initialize(apiKey, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean wasRestored) {
youTubePlayer.setShowFullscreenButton(false);
if (!wasRestored) {
youTubePlayer.loadVideo(videoId);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.e("app", youTubeInitializationResult.toString());
}
});
}
}
Second option using YouTubeStandalonePlayer
or creating your Own Activity
extends YouTubeBaseActivity
: Just when User click on youtube player button, you can create an Intent to open new activity to display Video
Intent intent = YouTubeStandalonePlayer.createVideoIntent(context, YOUR_DEVELOPER_KEY, VIDEO_ID);
startActivity(intent);