2017-03-04 7 views
0

私はナビゲーション・ドロワーと2つのフラグメントを持っていますが、それぞれに1つのボタンしかありません。Android:差分フラグメントのコンテンツは更新されていません

最初のフラグメントを選択すると、正しいボタンがロードされ、次に2番目のフラグメントが選択され、ボタンのロードが正しく行われます。しかし、前のフラグメントに戻ると、前のフラグメントのボタンが画面に残ります。

理由をご存知でしょうか?どうもありがとうございます!ここで

されているが、私の素片選択である:ここで

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

は私のフラグメント1 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> 

答えて

1

:ここ

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

私のフラグメント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" 
    android:id="@+id/container"/> 

アクティビティコードで

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

ありがとうございました! – user1060551

関連する問題