我正在尝试替换复合布局中的片段。这是我的根布局 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>
如您所见,我的布局中有三个区域,一个固定的左列包含一个带有按钮的片段,布局的其余部分垂直划分,顶部有一个管理文本视图的片段,下半部分有一个数据网格。
我想使用通用布局在"grid_frame"中放置各种 GridView 元素,同时按下"按钮"片段中的按钮。下面是我要用于每个 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"框架布局中的片段。我在"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 容器中的片段。
请问我做错了什么?
问题解决了!这是一个非常愚蠢的错误。我已经将我的活动类转换为片段类,在这个类中,我忘记在类onCreate方法中删除对setContentView的调用。因此,每次创建新实例时,片段都会设置新布局