Add fragment to fragment (nested fragment)

I want to add youtube snippet to my existing snippets dynamically. The code I used 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 corresponding fragment. When I checked when a new fragment always loads (when scrolling between fragments), the new inner fragment should be the first loaded fragment.

Any help would be gladly accepted.

RESOLVED: Thank you, Kobe. You were right. I had to replace "getActivity (). getSupportFragmentManager ();" with "getChildFragmentManager ()". The problem was apparently in the Sherlock library with the old Android v4 support library. I had to update the support library in Sherlock. It worked for me .....

+4
source share
1 answer

to create a nested fragment inside a fragment, you should use:

http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager ()

call getChildFragmentManager () from the parent fragment, and execute the transaction in the parent block to nest it inside.

fooobar.com/questions/179540 / ...

tell me if you need more help with this ...

+3
source

Source: https://habr.com/ru/post/1496950/


All Articles