我正在创建一个回收站视图,其中将包含不同类型的媒体。将有图像/音频/视频和YouTube内容。根据内容类型,我有不同的视图持有者实例化。
例如,我的youtube内容的视图持有者xml是:
<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>
因此,如果我的列表包含多个youtube内容项,则每次都会实例化此视图。
如果我创建一个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));
}
};
所以问题是如果我有多个,新片段会一直替换第一个framelayout(容器)中的第一个片段而其他片段留空。
E.g。 第一段片段 - &gt;电影预告片 第二片段 - &gt;教程视频 第3段片段 - &gt;音乐视频
将第一个片段添加到容器中,并显示预告片中的静止帧。然后当滚动recyclerview并创建下一个视图时,第二个片段被添加到第一个容器,然后是第三个等等。所以其他帧布局是空白的。
任何人都可以帮忙吗?