¿Cómo puedo implementar FragmentManager y FragmentTransaction para reemplazar sólo un fragmento?

Tengo una actividad con 3 fragmentos; una cabecera; un cuerpo; un pie de página (el mismo punto que en HTML). El bodyfragment contiene tres botones que cada uno debe reemplazar el fragmento medio (body, itself) con otro, pero no puedo averiguar cómo trabajar FragmentManager y FragmentTransition aquí. Parece que no puedo encontrar ninguna coherencia en las preguntas de otras personas aquí con respecto a la forma en que otros implementan sus fragmentos. Parece que todo el mundo tiene sus propios métodos, o simplemente no incluye el código completo en sus hilos.

MainActivity.java

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.test_frag); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } 

}

TestFragment.java

 public class TestFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.test_frag, container, false); } 

}

BodyFragment.java

 public class BodyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.body_frag, container, false); } 

}

Fragmento en XML

 <fragment android:id="@+id/bodyfragment" android:name="com.example.test.BodyFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" tools:layout="@layout/body_frag" /> 

Diseño de BodyFragment en XML (botón x3)

  <Button android:id="@+id/bsettings" android:layout_width="130dp" android:layout_height="40dp" android:layout_alignBaseline="@+id/bgames" android:layout_alignBottom="@+id/bgames" android:layout_toRightOf="@+id/bgames" android:text="SETTINGS" /> 

Utiliza el FragmentManager como este:

 FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.bodyfragment, AnotherFragment.newInstance()); // newInstance() is a static factory method. transaction.commit(); 

Este código reemplazaría al Fragmento que se encuentra en la Vista con el fragmento ID id.id con una nueva Instancia de otro Fragmento.

EDITAR:

Para crear una nueva instancia de otro Fragmento se supone que se utilizará un método estático de fábrica. Los implementarías en tu Fragmento como este:

 public class AnotherFragment extends Fragment { public static AnotherFragment newInstance() { AnotherFragment fragment = new AnotherFragment(); ... // do some initial setup if needed, for example Listener etc ... return fragment; } ... } 
  • Android: en el interruptor de desplazamiento entre Vistas / Actividades / Fragmentos
  • Cómo borrar argumentos en Fragmento?
  • DialogFragment: NullPointerException (biblioteca de soporte)
  • Listview conjunto adaptador fragmento nullpointerexception
  • no alcanzable después de usar .getActivity () en un fragmento
  • Pase entre actividades
  • Android Google Maps en Fragmento
  • Cómo obtener un fragmento añadido en un diseño XML
  • Fragmenttabhost rendimiento es lento?
  • Reemplazar un fragmento / ficha en un Viewpager
  • Buscar Fragmento por nombre de etiqueta en Contenedor
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.