我正在尝试替换复合布局中的片段。这是我的根布局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容器中的片段。
我做错了什么?
答案 0 :(得分:1)
问题解决了!这是一个非常愚蠢的错误。我已将Activity类转换为Fragment类,在这一个类中,我忘记在类onCreate方法中删除对setContentView的调用。因此,每次创建新实例
时,片段都会设置一个新布局