I want to add a youtube fragment into my already existing fragments dynamically. The code I used is below:
// setting the Youtube Player Dynamically
private int setYoutubePlayer(String desc, View view, int prevID,
Bundle input) {
if (desc.indexOf("=") != -1) {
desc = desc.substring(desc.indexOf("=") + "=".length());
} else {
return prevID;
}
final String url = desc;
LinearLayout videoLayout = new LinearLayout(view.getContext());
videoLayout.setOrientation(LinearLayout.VERTICAL);
prevID++;
videoLayout.setId(prevID);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragment.setVideoId(url);
LinearLayout itemLayout = (LinearLayout) view.findViewById(R.id.items);
itemLayout.addView(videoLayout);
fragmentTransaction.add(itemLayout.getId(), fragment,
"youtube fargment " + prevID);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
return prevID;
}
I need to get the youtube fragment in the appropriate fragment. As I checked when always a new fragment get loaded (when swipe between fragments), the new inner fragment needs to be the first loaded fragment.
Any help will be gladly accepted.
SOLVED: Thank you Koby You were right. i had to replace "getActivity().getSupportFragmentManager();" with "getChildFragmentManager()". The problem was apparently the Sherlock library came with a old android v4 support library. I had to update the support library in the Sherlock. It worked for me.....
Fragments are also capable of hosting one or more child fragments. Inside a fragment, you can get a reference to the FragmentManager that manages the fragment's children through getChildFragmentManager() . If you need to access its host FragmentManager , you can use getParentFragmentManager() .
Since FragmentContainerView is a subclass of FrameLayout, a Fragment can be added to it in the same way it can be added to a FrameLayout. Another way of adding a Fragment to a FragmentContainerView is by using the android:name attribute in the XML layout file.
Fragments represent a behavior or portion of UI in an Activity. Fragments are mostly used in tablets to divide screen and utilize screen space in efficient way. With Android 4.2 nested fragments are introduced, with which now you can embed fragments inside fragments.
to create a nested fragment inside a fragment, you should use:
http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager()
call the getChildFragmentManager() from the parent fragment, and do the transaction in the parent to nest the child inside.
https://stackoverflow.com/a/13381825/1693674
tell me if you need more help in doing that...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With