所有YouTube播放器支持片段都在“回收者”视图中显示相同的视频

时间:2018-11-17 09:11:36

标签: android android-recyclerview youtubeplayer

我有一个reviewerview。回收者视图的每个项目都是卡片视图。卡视图的元素上具有FrameLayout。此frameLayout在代码中包含YouTubePlayerSupportFragment。现在,每个YouTubePlayerSupportFragment应该包含不同的youtube视频。但是,在呈现“回收器”视图适配器时,所有行都具有与其关联的相同视频。我试图关联不同的容器ID,即:-将ID动态分配给框架布局,但问题仍然存在。

public class YoutubeVideoAdapter extends RecyclerView.Adapter<YoutubeVideoAdapter.YoutubeViewHolder> {
        private static final String TAG = YoutubeVideoAdapter.class.getSimpleName();
        private Context context;
        private ArrayList<TaskEntry> youtubeVideoModelArrayList;
        private static String API_KEY = "AAAAABBBBCCCC"; 


        // declare variables for the elemnts of the card view
        private CardView youtubeCardView;
        private YouTubeThumbnailView videoThumbnailImageView;
        private TextView textViewTitle;
        private TextView textViewTitleDesc;
        private TextView textViewDesc;
        private TextView textViewDescDetails;
        private FrameLayout frameLayout;

        private boolean state = true;

        private YouTubePlayerSupportFragment youTubePlayerFragment;
        private YouTubePlayer youTubePlayer;

        public YoutubeVideoAdapter(Context context, ArrayList<TaskEntry> youtubeVideoModelArrayList) {
            this.context = context;
            this.youtubeVideoModelArrayList = youtubeVideoModelArrayList;
        }

        @Override
        public YoutubeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            View view = layoutInflater.inflate(R.layout.youtube_video_custom_layout, parent, false);

            return new YoutubeViewHolder(view);
        }

        @Override
        public void onBindViewHolder(YoutubeViewHolder holder, final int position) {

            //intiliase the elements of the card view
            textViewTitleDesc.setText(youtubeVideoModelArrayList.get(position).getTitle());

            textViewDescDetails.setText(youtubeVideoModelArrayList.get(position).getDescription());

            FragmentManager fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();

            int newConatainerId = getUniqueId();

            frameLayout.setId(newConatainerId);

            youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

            String videoId = youtubeVideoModelArrayList.get(position).getVideoId();

            if (youTubePlayerFragment == null)
                return;

            youTubePlayerFragment.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {

                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
                                                    boolean wasRestored) {
                    if (!wasRestored) {
                        youTubePlayer = player;

                        //set the player style default
                        youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);

                        //cue the 1st video by default
                        youTubePlayer.cueVideo(youtubeVideoModelArrayList.get(position).getVideoId());
                    }
                }

                @Override
                public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {

                    //print or show error if initialization failed
                    Log.e(TAG, "Youtube Player View initialization failed");
                }
            });

            fragmentManager.beginTransaction().add(newConatainerId,youTubePlayerFragment).addToBackStack(null).commit();

        }

        // Method that could us an unique id
        public int getUniqueId(){
            return (int)SystemClock.currentThreadTimeMillis();
        }

        @Override
        public int getItemCount() {
            return youtubeVideoModelArrayList != null ? youtubeVideoModelArrayList.size() : 0;
        }

        /**
         * method the change the selected position when item clicked
         * @param selectedPosition
         */
        public void setSelectedPosition(int selectedPosition) {
            this.selectedPosition = selectedPosition;
            //when item selected notify the adapter
            notifyDataSetChanged();
        }

        public class YoutubeViewHolder extends RecyclerView.ViewHolder
        {
            public YoutubeViewHolder(View view)
            {
                super(view);
                youtubeCardView = view.findViewById(R.id.youtube_row_card_view);
                textViewTitle = view.findViewById(R.id.tv_title);
                textViewTitleDesc = view.findViewById(R.id.tv_title_details);
                textViewDesc = view.findViewById(R.id.tv_description);
                textViewDescDetails = view.findViewById(R.id.tv_description_details);
                frameLayout = (FrameLayout) view.findViewById(R.id.frame_layout);
            }
        }
    }

0 个答案:

没有答案