4

I am creating a recycler view in which will be different types of media. There will be images/audio/video and youtube content. Depending on the content type I have different view holders instantiated.

So for example my view holder xml for youtube content is:

<android.support.v7.widget.CardView
    android:layout_margin="10dp"
    app:cardBackgroundColor="@color/lightGrey"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cv">

    <RelativeLayout
        android:id="@+id/anchor"
        android:orientation = "vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <include
            android:id="@+id/content_header"
            android:layout_marginRight="@dimen/feed_item_margin"
            android:layout_marginLeft="@dimen/feed_item_margin"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            layout = "@layout/content_item_header_layout"
            />

        <FrameLayout
            android:id="@+id/youtube_fragment_container"
            android:background="@color/primaryColor"
            android:layout_marginTop="@dimen/feed_item_margin"
            android:layout_below="@+id/content_header"
            android:layout_width="match_parent"
            android:layout_height="@dimen/youtube_view_height">
        </FrameLayout>

        <TextView
            android:id="@+id/text_holder1"
            android:lines="1"
            android:textStyle="bold"
            android:ellipsize="end"
            android:layout_below="@+id/youtube_fragment_container"
            android:textColor="@color/primaryText"
            android:layout_marginRight="@dimen/feed_item_margin"
            android:layout_marginLeft="@dimen/feed_item_margin"
            android:layout_marginTop="@dimen/feed_item_margin"
            android:text="Heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/text_holder2"
            android:layout_below="@+id/text_holder1"
            android:textColor="@color/secondaryText"
            android:maxLines="4"
            android:ellipsize="end"
            android:text="body"
            android:layout_marginLeft="@dimen/feed_item_margin"
            android:layout_marginTop="@dimen/feed_item_margin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <include
            android:id="@+id/content_footer"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginRight="@dimen/feed_item_margin"
            android:layout_marginLeft="@dimen/feed_item_margin"
            android:layout_marginTop="@dimen/feed_item_margin"
            android:layout_below="@+id/text_holder2"
            layout = "@layout/content_item_footer_layout"/>
    </RelativeLayout>
</android.support.v7.widget.CardView>

So if my list contains multiple youtube content items I instantiate this viewholder each time.

If I create one YouTubePlayerSupportFragment, this works well, however, when I have more than one after the other there is a problem. Here is how I add the YouTubePlayerSupportFragment to the viewholder

YouTubePlayerSupportFragment youTubePlayerFragment = new YouTubePlayerSupportFragment();
        FragmentManager fm = mMainActivity.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.youtube_fragment_container, youTubePlayerFragment).commit();
        initMediaLayout(feedContent, media_view, youTubePlayerFragment);

initMediaLayout method

 public void initMediaLayout(IFeedContent content, TextureView view, YouTubePlayerSupportFragment youTubePlayerView){

        //Get a handle on Media Layout to set the background
        RelativeLayout layout = (RelativeLayout)view.getParent();

        //Initialize global variable to access in YouTube initialize listener
        mFeedContent = content;

        if(!content.getMedia().getMimeType().contains(itemView.getContext().getString(R.string.youtube))) {
            //If mimeType isn't youtube, hide view
            //Set required background placeholder image
            if (content.getMedia().getMimeType().contains("audio")) {
                layout.setBackgroundDrawable(itemView.getContext().getResources().getDrawable(R.drawable.music_placeholder));
            } else {
                layout.setBackgroundDrawable(itemView.getContext().getResources().getDrawable(R.drawable.video_placeholder));
            }
        }else{
            youTubePlayerView.initialize(itemView.getContext().getString(R.string.you_tube_api_key), YoutubePlayerInitializer);
            layout.setVisibility(View.GONE);
        }
    }

    YouTubePlayer.OnInitializedListener YoutubePlayerInitializer = new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {
            youTubePlayer.cueVideo(new MediaUtils().getVideoId(mFeedContent.getMedia().getMediaUrl()));
            youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
            youTubePlayer.setFullscreenControlFlags(2);

            youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
                @Override
                public void onFullscreen(boolean b) {
                    if(b) {
                        mAbMainActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
                        PreferenceUtils.setDisplayMedia(b);
                    }
                    else {
                        mAbMainActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                        PreferenceUtils.setDisplayMedia(b);
                    }
                }
            });
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult){
            Log.i(AbstractHolder.class.getSimpleName(), String.valueOf(youTubeInitializationResult));
        }
    };

So the problem is if I have more than one, the new fragment keeps replacing the first fragment in the first framelayout (container) leaving the others blank.

E.g. 1st Fragment --> Movie Trailer 2nd Fragment --> Tutorial Video 3rd Fragment --> Music Video

The first fragment is added to the container and displays the still frame from the trailer. Then when the recyclerview is scrolled and the next viewholder is created, the 2nd fragment is added to the firsts container and then the third etc.. So the other frame layouts are blank.

Can anyone help with this?

DJ-DOO
  • 4,545
  • 15
  • 58
  • 98
  • 3
    For anyone else wondering...this isn't possible, it appears that they use a singleton for the you tube player return from the onintialized callback – DJ-DOO Sep 04 '15 at 08:04
  • So you can only really have one player in an Activity? I was trying to do something very similar with multiple YoutubePlayerFragments in a RecyclerView. – Flyview Nov 06 '15 at 00:52
  • @DJ-DOO did you solve this? Can you please share idea to solve this or code snippet. – aman.nepid May 10 '16 at 05:48
  • @aman.nepid I mentioned in the above comment that it's not possible as they appear to use a singleton for the youtube player – DJ-DOO May 19 '16 at 11:00
  • See [this question](http://stackoverflow.com/questions/37194653/fragment-replacing-in-recyclerview-item), it might be the source of the problem. – Irhala Oct 19 '16 at 09:57

0 Answers0