【问题标题】:Replace Fragments替换片段
【发布时间】:2012-01-30 18:29:30
【问题描述】:

我的 Activity 有以下 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>

单击底部栏时,我需要将 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 消失了,没有任何事情发生。

【问题讨论】:

    标签: android android-layout android-fragments android-ui android-gui


    【解决方案1】:

    改用替换方法;

    FragmentTransaction trans = getFragmentManager().beginTransaction();
    trans.replace(R.id.fragment_container, new LibraryFragment()); 
    trans.commit();
    

    【讨论】:

    • 同样的结果 PS:fragment_container 是 LinearLayout 有两个片段应该被一个替换
    猜你喜欢
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    相关资源
    最近更新 更多