2

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" />

1 respuesta 1

3

El id debe ser de la vista que contiene el fragmento no la del fragmento, agrega un FrameLayout contenedor, con el id listado:

    <FragmentLayout
        android:id="@+id/listado" 
        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/myFragment" />


    </FragmentLayout>

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.