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>