Problem trying to recycle multiple fragments ( Youtubeplayerfragment )

Asked

Viewed 29 times

0

I am trying to view youtube videos using Youtubeplayerfragment through Recyclerview, the links are saved in Firebase but if there is more than one link when it will inflate a new layout I get the following error:

Duplicate id 0x7f070086, tag null, or parent id 0x7f070022 with another fragment for com.google.android.youtube.player.YouTubePlayerSupportFragment

I found several questions like this in English but could not adapt anything to my script that worked!

private RecyclerView recyclerView;

private DatabaseReference Link;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment_test);


    recyclerView = (RecyclerView)findViewById(R.id.testingrecycle);
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

    Link = FirebaseDatabase.getInstance().getReference().child("UsersPost");

}

@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerOptions<RecyclerControl> options =
            new FirebaseRecyclerOptions.Builder<RecyclerControl>()
                    .setQuery(Link, RecyclerControl.class)
                    .build();


    FirebaseRecyclerAdapter<RecyclerControl, FragmentTest.frag> adapter =
            new FirebaseRecyclerAdapter<RecyclerControl, FragmentTest.frag>(options) {
                @Override
                protected void onBindViewHolder(@NonNull final FragmentTest.frag holder, final int position, @NonNull RecyclerControl model) {


                    final String id_post = getRef(position).getKey();

                    Link.child(id_post).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


                            if (dataSnapshot.exists()) {


                                final String linkid = dataSnapshot.child("link").getValue().toString();

                                YouTubePlayerFragment youtubeFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.fragmenttesting);

                                youtubeFragment.initialize("AIzaSyBU-niwyzxfEUxxV1Nqfsg0Fc5pI4_ZKtg",
                                        new YouTubePlayer.OnInitializedListener() {
                                            @Override
                                            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {

                                                youTubePlayer.loadVideo(linkid);

                                            }

                                            @Override
                                            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

                                            }
                                        });

                            }



                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });




                }

                @NonNull
                @Override
                public FragmentTest.frag onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

                    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardviewfragment, viewGroup, false);
                    FragmentTest.frag viewHolder = new FragmentTest.frag(view);
                    return viewHolder;
                }
            };

    recyclerView.setAdapter(adapter);
    adapter.startListening();


}

public static class frag extends  RecyclerView.ViewHolder {


    public frag(@NonNull View itemView) {
        super(itemView);




    }
}

videoview.XML

<?xml version="1.0" encoding="utf-8"?>

<fragment

    android:name="com.google.android.youtube.player.YouTubePlayerFragment"
    android:id="@+id/fragmenttesting"
    android:layout_width="match_parent"
    android:layout_height="290dp"/>

Fragmenttest Activity

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.RecyclerView

    android:id="@+id/testingrecycle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


</android.support.v7.widget.RecyclerView>

1 answer

-1

Does not work with the Google component in Recycleview.

Use this component here to include in your build.grandle

 dependencies {
  implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.5'
}

In your Adapter xml use the component

<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
        android:id="@+id/youtube_player_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:autoPlay="false" />

on your Adapter load the video

YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);

youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
  @Override
  public void onReady(@NonNull YouTubePlayer youTubePlayer) {
    String videoId = "id do seu video no youtube";
    youTubePlayer.loadVideo(videoId, 0);
  }
});

Link below contains the Github page of the component with several examples of how to implect

https://github.com/PierfrancescoSoffritti/android-youtube-player

Browser other questions tagged

You are not signed in. Login or sign up in order to post.