estoy realizando una aplicación Android y para ello utilizo un Navigation Drawer Con Material Design y quiero que por defecto aparezca un listado y que cuando selecciono un ítem del Navigation Drawer ya no aparezca el listado inicial, sino otro. Hice lo siguiente:
private void seleccionarItem(MenuItem itemDrawer) {
ListFragment fragmentoGenerico = null;
switch (itemDrawer.getItemId()) {
case R.id.item_inicio:
fragmentoGenerico = new ListFragment()
break;
case R.id.item_cuenta:
fragmentoGenerico = new List2Fragment();
break;
default:
fragmentoGenerico = new ListFragment();
break;
}
if (fragmentoGenerico != null) {
getSupportFragmentManager().beginTransaction().replace(android.R.id.listado, fragmentoGenerico).commit();
A pesar de que utilizo el método replace no remplaza el fragmento, sino que me superpone los dos fragmentos.
EL OnCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
ListFragment listado = new ListFragment();
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_cast_dark);
ab.setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView) findViewById
(R.id.nav_view);
if (navigationView != null) {
prepararDrawer(navigationView);
seleccionarItem(navigationView.getMenu().getItem(0));
}
El layout activity_main:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="ListFragment"
android:id="@+id/listado" />
</LinearLayout>
<!-- Menú Deslizante -->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/cabecera"
app:menu="@layout/menu" />