Pregunta

Tengo un ViewPager para implementar el deslizamiento entre fragmentos "titulares".Y dentro de ellos necesito agregar múltiples Fragmentos a sus respectivos contenedores.

El problema es que estoy usando FragmentTransaction.add(containerID, fragment);, pero dado que hay varios Fragmentos de titular en ViewPager, todos los Fragmentos se agregan en el mismo Fragmento de titular, y no en el que llama para agregarlos.

¿Alguien tiene una idea sobre una buena práctica para solucionar este problema?

Aquí está el código donde agrego los Fragmentos dentro del fragmento del titular.

arrayFragments = new ArrayList<DiaAgendaFragment>();
DiaAgendaFragment objFragment;
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();

//this will be set accordingly later for each fragment
Time dataFragment = new Time();
dataFragment.setToNow();

//instantiate and set fragments
for (int i = 0; i < 5; i++) {

    objFragment = DiaAgendaFragment.newInstance(5, dataFragment);
    arrayFragments.add(objFragment);
    //pega a View pelo nome e adiciona o fragment
    transaction.add(getActivity().getResources().getIdentifier("agenda5d_activity_fragment"+i, "id", getActivity().getPackageName()), arrayFragments.get(i));
    //codigo de teste
    dataFragment.monthDay += 1;
}
transaction.commit();
¿Fue útil?

Solución

Tienes que usar al niño. FragmentManager si quieres agregar Fragments a otro Fragment.usando lo normal FragmentManager no funcionará como usted mismo descubrió.

En tus Fragment:

FragmentManager manager = getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
...
transaction.commit();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top