0

我的活动有以下 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <br.com.digitalpages.shelves.view.TopBar
        android:layout_width="match_parent"
        android:layout_height="60px" />

    <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <fragment
            android:id="@+id/fragment_categories"
            android:layout_width="240px"
            android:layout_height="match_parent"
            android:name="br.com.digitalpages.shelves.fragment.TreeCategoryFragment" />

        <fragment
            android:id="@+id/fragment_library"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:name="br.com.digitalpages.shelves.fragment.LibraryFragment" />
    </LinearLayout>

    <fragment
        android:id="@+id/fragment_bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="br.com.digitalpages.shelves.fragment.BottomBarFragment" />

</LinearLayout>

单击 Bottom_Bar 时,我需要将 fragment_categories 和 fragment_library 替换为另一个跨越旧片段空间的片段。

我怎样才能做到这一点?

我试过了:

        FragmentTransaction trans = getFragmentManager().beginTransaction();
        trans.remove(categoryExplorerFragment);
        trans.remove(libraryFragment);
        trans.add(R.id.fragment_container, new LibraryFragment());
        trans.commit();

但是当我点击时,只有 libraryFragment 消失了,没有任何事情发生。

4

1 回答 1

0

改用替换方法;

FragmentTransaction trans = getFragmentManager().beginTransaction();
trans.replace(R.id.fragment_container, new LibraryFragment()); 
trans.commit();
于 2012-01-30T18:45:40.407 回答