Problem replacing Android fragment

2

I'm doing an Android application and I use a Navigation Drawer With Material Design and I want a list to appear by default and when I select an item from the Navigation Drawer, the initial list will no longer appear, but another one. I did the following:

 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();

Although I use the replace method, it does not replace the fragment, but it overlaps the two fragments.

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));
    }

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

    
asked by adamista 27.04.2016 в 22:04
source

1 answer

3

The id must be from the view that contains the fragment, not the fragment, add a FrameLayout container, with the 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>
    
answered by 28.04.2016 / 03:04
source