RecyclerView 中的 YouTubePlayerFragment
YouTubePlayerFragment inside ReyclerView
我有一个 RecyclerView,里面有 CardView。我希望每个 CardView 都有自己的 YouTubePlayerFragment。即使它们不能同时全部激活,我什至无法在任何 CardView 中实例化 YouTubePlayerFragment,而是在 RecyclerView 中实例化第一个。当点击卡上应该设置 YouTubePlayerFragment 的按钮时,它总是在第一张卡上设置它,即使我点击了第二张卡。我正在尝试做的事情可行吗?
以下是代码的相关部分:
CardView 中片段的持有者 XML:(直接在 xml 中使用片段给出二进制 XML 异常,可能是由于沿线某处片段中的片段)
<FrameLayout
android:id="@+id/youtube_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/layout_video_thumbnail"
android:layout_alignStart="@+id/layout_video_thumbnail"
android:layout_alignRight="@+id/layout_video_thumbnail"
android:layout_alignEnd="@+id/layout_video_thumbnail"
android:visibility="visible"/>
ViewHolder:
public class VideoViewHolder extends RecyclerView.ViewHolder {
protected FrameLayout containerYouTubePlayer;
protected TextView textViewTitle;
protected ImageView imageViewThumbNail;
protected Button buttonPreview, buttonSet;
protected Context activityContext;
public VideoViewHolder(Context activityContext, View itemView) {
super(itemView);
this.activityContext = activityContext;
containerYouTubePlayer = (FrameLayout) itemView.findViewById(R.id.youtube_holder);
textViewTitle = (TextView) itemView.findViewById(R.id.textView_title);
imageViewThumbNail = (ImageView) itemView.findViewById(R.id.imageView_video_thumbnail);
buttonPreview = (Button) itemView.findViewById(R.id.button_preview);
buttonSet = (Button) itemView.findViewById(R.id.button_set);
}
}
RecyclerView 适配器:
public class VideoCardAdapter extends RecyclerView.Adapter<VideoViewHolder> {
private static final String TAG = "VidWall.VideoCardAdapter";
private List<VideoInfo> videoList;
private Context activityContext;
public VideoCardAdapter(Context activityContext, List<VideoInfo> videoList) {
this.activityContext = activityContext;
this.videoList = videoList;
}
@Override
public int getItemCount() {
return videoList.size();
}
@Override
public void onBindViewHolder(final VideoViewHolder videoViewHolder, int i) {
final VideoInfo videoInfo = videoList.get(i);
videoViewHolder.textViewTitle.setText(videoInfo.displayName);
videoViewHolder.imageViewThumbNail.setImageDrawable(ContextCompat.getDrawable(activityContext, activityContext.getResources().getIdentifier(videoInfo.fileNameThumbnail, "drawable", activityContext.getPackageName())));
videoViewHolder.buttonPreview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Utils.logDebug(TAG, "buttonPreview clicked");
// Add the YouTube fragment to view
final YouTubePlayerFragment youTubePlayerFragment = YouTubePlayerFragment.newInstance();
((Activity) activityContext).getFragmentManager().beginTransaction().replace(videoViewHolder.containerYouTubePlayer.getId(), youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(WallpapersActivity.Y_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {
Utils.logDebug(TAG, "onInitializationSuccess");
youTubePlayer.cueVideo("xxxxxxxx");
youTubePlayer.setShowFullscreenButton(false);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Utils.logError(TAG, "Could not initialize YouTubePlayer");
}
});
videoViewHolder.containerYouTubePlayer.setVisibility(View.VISIBLE);
}
});
}
@Override
public VideoViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_wallpaper_card, viewGroup, false);
return new VideoViewHolder(activityContext, itemView);
}
}
所以我想通了。只要你为 YouTube FrameLayout 设置一个唯一的 id,activity 就会正确的插入 fragment 到正确的卡片上!您可以在运行时在视图上设置 Id(),因此只需确保它们在某种程度上彼此不同即可。
// Add the YouTube fragment to view
wallpaperInfo.youTubePlayerFragment = YouTubePlayerFragment.newInstance();
// Must set a unique id to the framelayout holding the fragment or else it's always added to the first item in the recyclerView (the first card)
videoViewHolder.containerYouTubePlayer.setId(wallpaperInfo.id);
((Activity) activityContext).getFragmentManager().beginTransaction().add(videoViewHolder.containerYouTubePlayer.getId(), wallpaperInfo.youTubePlayerFragment).addToBackStack(null).commit();
我有一个 RecyclerView,里面有 CardView。我希望每个 CardView 都有自己的 YouTubePlayerFragment。即使它们不能同时全部激活,我什至无法在任何 CardView 中实例化 YouTubePlayerFragment,而是在 RecyclerView 中实例化第一个。当点击卡上应该设置 YouTubePlayerFragment 的按钮时,它总是在第一张卡上设置它,即使我点击了第二张卡。我正在尝试做的事情可行吗?
以下是代码的相关部分:
CardView 中片段的持有者 XML:(直接在 xml 中使用片段给出二进制 XML 异常,可能是由于沿线某处片段中的片段)
<FrameLayout
android:id="@+id/youtube_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/layout_video_thumbnail"
android:layout_alignStart="@+id/layout_video_thumbnail"
android:layout_alignRight="@+id/layout_video_thumbnail"
android:layout_alignEnd="@+id/layout_video_thumbnail"
android:visibility="visible"/>
ViewHolder:
public class VideoViewHolder extends RecyclerView.ViewHolder {
protected FrameLayout containerYouTubePlayer;
protected TextView textViewTitle;
protected ImageView imageViewThumbNail;
protected Button buttonPreview, buttonSet;
protected Context activityContext;
public VideoViewHolder(Context activityContext, View itemView) {
super(itemView);
this.activityContext = activityContext;
containerYouTubePlayer = (FrameLayout) itemView.findViewById(R.id.youtube_holder);
textViewTitle = (TextView) itemView.findViewById(R.id.textView_title);
imageViewThumbNail = (ImageView) itemView.findViewById(R.id.imageView_video_thumbnail);
buttonPreview = (Button) itemView.findViewById(R.id.button_preview);
buttonSet = (Button) itemView.findViewById(R.id.button_set);
}
}
RecyclerView 适配器:
public class VideoCardAdapter extends RecyclerView.Adapter<VideoViewHolder> {
private static final String TAG = "VidWall.VideoCardAdapter";
private List<VideoInfo> videoList;
private Context activityContext;
public VideoCardAdapter(Context activityContext, List<VideoInfo> videoList) {
this.activityContext = activityContext;
this.videoList = videoList;
}
@Override
public int getItemCount() {
return videoList.size();
}
@Override
public void onBindViewHolder(final VideoViewHolder videoViewHolder, int i) {
final VideoInfo videoInfo = videoList.get(i);
videoViewHolder.textViewTitle.setText(videoInfo.displayName);
videoViewHolder.imageViewThumbNail.setImageDrawable(ContextCompat.getDrawable(activityContext, activityContext.getResources().getIdentifier(videoInfo.fileNameThumbnail, "drawable", activityContext.getPackageName())));
videoViewHolder.buttonPreview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Utils.logDebug(TAG, "buttonPreview clicked");
// Add the YouTube fragment to view
final YouTubePlayerFragment youTubePlayerFragment = YouTubePlayerFragment.newInstance();
((Activity) activityContext).getFragmentManager().beginTransaction().replace(videoViewHolder.containerYouTubePlayer.getId(), youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(WallpapersActivity.Y_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {
Utils.logDebug(TAG, "onInitializationSuccess");
youTubePlayer.cueVideo("xxxxxxxx");
youTubePlayer.setShowFullscreenButton(false);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Utils.logError(TAG, "Could not initialize YouTubePlayer");
}
});
videoViewHolder.containerYouTubePlayer.setVisibility(View.VISIBLE);
}
});
}
@Override
public VideoViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_wallpaper_card, viewGroup, false);
return new VideoViewHolder(activityContext, itemView);
}
}
所以我想通了。只要你为 YouTube FrameLayout 设置一个唯一的 id,activity 就会正确的插入 fragment 到正确的卡片上!您可以在运行时在视图上设置 Id(),因此只需确保它们在某种程度上彼此不同即可。
// Add the YouTube fragment to view
wallpaperInfo.youTubePlayerFragment = YouTubePlayerFragment.newInstance();
// Must set a unique id to the framelayout holding the fragment or else it's always added to the first item in the recyclerView (the first card)
videoViewHolder.containerYouTubePlayer.setId(wallpaperInfo.id);
((Activity) activityContext).getFragmentManager().beginTransaction().add(videoViewHolder.containerYouTubePlayer.getId(), wallpaperInfo.youTubePlayerFragment).addToBackStack(null).commit();