Android: diff 片段的内容没有更新

Android: content from diff fragments are not updating

我有一个导航抽屉和 2 个片段,每个片段只有 1 个按钮。

当我 select 第一个片段时,正确的按钮加载,然后我 select 第二个片段,正确的按钮加载。但是,如果我 select 返回到上一个片段,上一个片段的按钮将保留在屏幕中。

你知道为什么吗?非常感谢!

这是我的片段select或:

@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.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"
    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>

您需要替换同一容器中的片段。

在activity布局中

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

在activity代码中

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

    }