2017-03-04 2 views
0

Ich habe eine Navigationsschublade und 2 Fragmente, in jedem habe ich nur 1 Taste.Android: Inhalt von Diff-Fragmente aktualisieren nicht

Wenn ich das erste Fragment auswählen, laden Sie die richtige Schaltfläche, dann wähle ich das zweite Fragment, korrekte Schaltfläche lädt. Wenn ich jedoch zum vorherigen Fragment zurückwähle, bleibt die Schaltfläche des vorherigen Fragments auf dem Bildschirm erhalten.

Würdest du wissen warum? Ich danke dir sehr!

Hier sind meine Fragment Selektor:

@SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      AssetView assetView = new AssetView(); 
      android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); 
      fragmentManager.beginTransaction().replace(
        R.id.assetView_relative_layout, 
        assetView, 
        assetView.getTag() 
      ).commit(); 

     } else if (id == R.id.nav_gallery) { 
      AccountView accountView = new AccountView(); 
      android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); 
      fragmentManager.beginTransaction().replace(
        R.id.accountView_relative_layout, 
        accountView, 
        accountView.getTag() 
      ).commit(); 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

Hier mein Fragment 1 xml ist:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="it.bitrack.fabio.bitrack.AssetView" 
    android:id="@+id/assetView_relative_layout"> 

    <Button 
     android:text="Asset View" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:id="@+id/button" /> 

</RelativeLayout> 

Hier ist mein Fragment 2 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="it.bitrack.fabio.bitrack.AccountView" 
    android:id="@+id/accountView_relative_layout"> 

    <Button 
     android:text="Account View" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:id="@+id/button" /> 

</RelativeLayout> 

Antwort

1

Sie müssen ersetzen Fragmente in demselben Behälter.

In Aktivität Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/container"/> 

In Aktivitätscode

if (id == R.id.nav_camera) { 
     AssetView assetView = new AssetView(); 
     android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction().replace(
       R.id.container, 
       assetView, 
       assetView.getTag() 
     ).commit(); 

    } else if (id == R.id.nav_gallery) { 
     AccountView accountView = new AccountView(); 
     android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction().replace(
       R.id.container, 
       accountView, 
       accountView.getTag() 
     ).commit(); 

    } 
+0

Vielen Dank! – user1060551

Verwandte Themen