Я создаю представление ресайклера, в котором будут разные типы носителей. Там будут изображения/аудио/видео и контент YouTube. В зависимости от типа контента у меня есть экземпляры разных держателей представлений.
Так, например, мой держатель представления xml для контента YouTube:
<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.
Если я создаю один YouTubePlayerSupportFragment, это работает хорошо, однако, когда у меня есть более одного за другим, возникает проблема. Вот как я добавляю YouTubePlayerSupportFragment в окно просмотра
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
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));
}
};
Итак, проблема в том, что если у меня их несколько, новый фрагмент продолжает заменять первый фрагмент в первом макете кадра (контейнере), оставляя остальные пустыми.
Например. 1-й фрагмент --> трейлер к фильму 2-й фрагмент --> обучающее видео 3-й фрагмент --> музыкальное видео
Первый фрагмент добавляется в контейнер и отображает неподвижный кадр из трейлера. Затем, когда recyclerview прокручивается и создается следующий просмотрщик, второй фрагмент добавляется в первый контейнер, а затем третий и т. д. Таким образом, другие макеты кадров пусты.
Может кто-нибудь помочь с этим?