0

我正在尝试替换复合布局中的片段。这是我的根布局 XML:

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

    <fragment
        android:id="@+id/buttons"
        android:name="com.kippygo.android.touch_talker.ButtonsFragment"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        class="com.kippygo.android.touch_talker.ButtonsFragment" >
        <!-- Preview: layout=@layout/buttons_fragment -->
    </fragment>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/buffer"
        android:name="com.kippygo.android.touch_talker.BufferFragment"
        android:layout_width="match_parent"
        android:layout_height="80dp" >

        <!-- Preview: layout=@layout/buffer_fragment -->
    </fragment>

    <FrameLayout
        android:id="@+id/grid_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/detailsElementBackground" >

    </FrameLayout>

</LinearLayout> 

</LinearLayout>

正如你所看到的,我的布局中有三个区域,一个固定的左列包含一个带有按钮的片段,布局的其余部分垂直划分,一个片段在顶部管理 textView,在下部有一个数据网格。

当我按下“按钮”片段中的按钮时,我想使用通用布局将各种 GridView 元素放置在“grid_frame”中。这是我要用于每个 GridView 片段的“grid_fragment”布局:

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

<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="80dp"
    android:clipToPadding="true"
    android:gravity="center"
    android:horizontalSpacing="20dp"
    android:numColumns="auto_fit"
    android:orientation="vertical"
    android:stretchMode="columnWidth"
    android:verticalSpacing="20dp" >
    <!-- Preview: listitem=@android:layout/simple_list_item_2 -->
</GridView>

<TextView
    android:id="@+id/android:empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="22sp"
    android:textStyle="bold"
    android:text="@string/no_list_items"/>

</LinearLayout>

当我启动我的应用程序时,这个复合布局打开得很好。我在我的初始活动中使用以下代码在 FrameLayout id“grid_frame”中设置了我的第一个片段:

    // Execute a transaction to put the categoriesFragment in the grid_view
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.grid_frame, new CategoriesFragment(), "category_fragment");
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.commit();

这工作得非常好,我看到左侧的按钮,顶部的文本输入框和按钮右侧的填充良好的单元格网格。

当我单击动态片段呈现的 GridView 中的列表项之一时,我想替换“grid_frame”FrameLayout 中的片段。我使用以下代码在“grid_frame”片段的 onItemClick 侦听器方法中使用以下代码执行此操作:

    // Execute a transaction to put the categoriesFragment in the grid_view
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.grid_frame, categoryWordsFragment, "category_words_fragment");
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.commit();

这看起来与我在活动中用于设置工作正常的初始片段的代码非常相似。

当我这样做时,我的整个屏幕被替换为新的片段布局,这是另一个具有不同数据的 GridView。replace即使我在方法调用中将“grid_frame”FrameLayout 指定为容器视图 id,我的根布局似乎也完全被新片段膨胀的布局所取代。

我已经尝试了我能找到的一切来使我的布局保持不变,只需更改 FrameLayout 容器中的片段。

请问我做错了什么?

4

1 回答 1

1

问题解决了!这是一个非常愚蠢的错误。我已将我的 Activity 类转换为 Fragment 类,在这一类中,我忘记删除类 onCreate 方法中对 setContentView 的调用。所以每次我创建一个新实例时,片段都会设置一个新的布局

于 2012-05-20T18:15:32.207 回答