سؤال

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.....

هل كانت مفيدة؟

المحلول

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...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top