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>

ご覧のとおり、レイアウトには3つの領域があります。ボタン付きのフラグメントを含む固定された左側の列は、レイアウトの残りの部分が垂直に分割され、フラグメントは上部のtextViewを管理し、下部のデータのグリッドを管理します。

「buttons」フラグメントのボタンを押すときに、共通のレイアウトを使用して「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>

この複合レイアウトは、アプリを起動すると正常に開きます。そして、次のコードを使用して、最初のアクティビティでFrameLayoutID"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のリスト項目の1つをクリックすると、「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メソッド呼び出しでコンテナビューIDとして「grid_frame」FrameLayoutを指定しても、ルートレイアウトは、新しいフラグメントによって拡張されたレイアウトに完全に置き換えられているように見えます。

レイアウトをそのままにして、FrameLayoutコンテナのフラグメントを変更するために、見つけたすべてのことを試しました。

私は何を間違っているのですか?

4

1 に答える 1

1

問題が解決しました!それは非常に愚かな間違いでした。ActivityクラスをFragmentクラスに変換しましたが、この1つのクラスでは、クラスonCreateメソッドのsetContentViewの呼び出しを削除するのを忘れていました。そのため、新しいインスタンスを作成するたびに、フラグメントは新しいレイアウトを設定していました

于 2012-05-20T18:15:32.207 に答える