16

検索ビューとリストビューを備えた線形レイアウトを含むタブがあります。ユーザーが検索結果の1つをクリックしたときに、そのレイアウトを選択の詳細を含む別のレイアウトに置き換えたいと思います。

検索フラグメントを置き換えると、レイアウトのリストビュー部分のみが置き換えられます。検索ビューは引き続き表示され、画面上でアクティブになっています。

これが私の検索レイアウトです:

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

    <SearchView
        android:id="@+id/public_calendar_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"
    </SearchView>

    <ListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        android:choiceMode="singleChoice" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:gravity="center"/>

</LinearLayout>

詳細なレイアウトは次のとおりです。

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="vertical" >

        <Space
            android:layout_width="0dip"
            android:layout_height="20dip"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="horizontal" >

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".1" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".8"
            android:text="@string/label_Name" />

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".1" />
    </LinearLayout>

    .........

</LinearLayout>

そして、検索フラグメントを置き換える私のコード:

FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack.
DetailFragment fragment = DetailFragment.newInstance(cal);
transaction.replace(R.id.search_layout, fragment);

transaction.addToBackStack(null);
transaction.commit();

詳細フラグメントの作成:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.detail_layout, container, false);
    return v;
}

static DetailFragment newInstance(String cal) {
    DetailFragment d = new DetailFragment();
    return d;
}

私は何が間違っているのですか?検索レイアウト全体を置き換えるにはどうすればよいですか。

ありがとう!

4

5 に答える 5

17

私は自分の問題を理解しました。マルコは正確には正しくありませんでしたが、彼は私を正しい方向に向けました。

問題は、replace メソッドに渡していたコンテナ ID が、フラグメント コンテナの ID ではなく、置き換えていたフラグメントの ID だったことです。これは、元のフラグメント コントロールの一部が置換後に残っていた理由を説明しているようです。つまり、フラグメント全体が置換されていませんでした。

フラグメント コンテナーのビュー ID を取得するように変更したところ、うまくいきました。コードは次のとおりです。

transaction.replace(((ViewGroup)(getView().getParent())).getId(), fragment); 

ここでフラグメントのコンテナー ビュー ID を取得するための答えを見つけました。 Get fragment's container view id

于 2012-10-19T00:04:15.193 に答える
11

私は正しいIDを使用していましたが、それも消えませんでした。Background colorこのように、新しい Fragment レイアウトで を必要な色に設定する必要があることがわかりました。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@color/white_color">
于 2016-01-03T03:28:33.873 に答える
5

この問題を解決するには、フラグメント レイアウト ファイルで android:background プロパティを「?android:windowBackground」に設定します。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:windowBackground"
    tools:context=".MainFragment">

    ...

</LinearLayout>

Fragment_2 内

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:windowBackground"
    tools:context=".SomeFragment">

    ...

</LinearLayout>
于 2016-09-07T10:02:29.943 に答える