160

フラグメント内のフラグメントの操作に関してサポートが必要です。実際、戻るボタンを押すと問題が発生します。アプリケーションのメイン画面にはボタンがあり、各ボタンビューを押すと新しいフラグメントに置き換えられます(そしてそのフラグメントは別のフラグメント内に含まれます)、動的にフラグメントを追加/置換することは正常に機能します。再びボタン、例外が発生しました:

"Duplicate id 0x7f05000a, tag null, or parent id 0x7f050009 with
another fragment for com........ fragmentname"

フラグメントまたは内部フラグメントがすでに追加されていることを意味し、それらを再度追加しようとしています。サポートのおかげで、フラグメント内のフラグメントを操作して問題なく前後に移動する方法を誰もが知っています。

MainActivity。フラグメントは動的に追加および置換されます。

public class FragmentInsideFragmentTestActivity extends Activity {

    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button1 =(Button) this.findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
               onButtonClick(view);
            }
        });

        button2 =(Button) this.findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                onButtonClick(view);
            }
        });

        button3 =(Button) this.findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
               onButtonClick(view);
            }
        });

        button4 =(Button) this.findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
               onButtonClick(view);
           }
        });
    }

    public void onButtonClick(View v) {
        Fragment fg;

        switch (v.getId()) {
           case R.id.button1:
                   fg=FirstFragment.newInstance();
                   replaceFragment(fg);
                   break;
           case R.id.button2:
                   fg=SecondFragment.newInstance();
                   replaceFragment(fg);
                   break;
           case R.id.button3:
                   fg=FirstFragment.newInstance();
                   replaceFragment(fg);
                   break;
           case R.id.button4:
                   fg=SecondFragment.newInstance();
                   replaceFragment(fg);
                   break;
        }
    }

    private void replaceFragment(Fragment newFragment) {
       FragmentTransaction trasection = getFragmentManager().beginTransaction();

        if(!newFragment.isAdded()) {
            try {
                //FragmentTransaction trasection =
                getFragmentManager().beginTransaction();
                trasection.replace(R.id.linearLayout2, newFragment);
                trasection.addToBackStack(null);
                trasection.commit();
            } catch (Exception e) {
                // TODO: handle exception
                // AppConstants.printLog(e.getMessage());
            } else {
                trasection.show(newFragment);
            }
        }
    }

レイアウトは次のとおりです:main.xml

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

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" />

        <Button
            android:id="@+id/button2"
            android:text="Button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/button3"
            android:text="Button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/button4"
            android:text="Button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" />
</LinearLayout>

私が自分の問題を解決しようとしたことを願っています。

4

13 に答える 13

300

私の知る限り、フラグメントは他のフラグメントを保持できません。


アップデート

現在のバージョンの Android サポート パッケージ (または API レベル 17 以降のネイティブ フラグメント) では、getChildFragmentManager(). これは、API レベル 11 ~ 16 でフラグメントの Android サポート パッケージ バージョンを使用する必要があることを意味することに注意してください。これらのデバイスにはフラグメントのネイティブ バージョンがありますが、そのバージョンにはgetChildFragmentManager().

于 2011-07-13T00:04:44.000 に答える
110

ここに画像の説明を入力

もう少しコンテキストが必要だったので、これがどのように行われるかを示す例を作成しました。準備中に私が読んだ最も役に立ったのはこれでした:

アクティビティ

activity_main.xml

FrameLayout親フラグメントを保持するためにアクティビティに を追加します。

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity"/>

    <FrameLayout
        android:id="@+id/parent_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="200dp"/>

 </LinearLayout>

MainActivity.java

親フラグメントをロードし、フラグメント リスナーを実装します。(フラグメント通信を参照してください。)

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements ParentFragment.OnFragmentInteractionListener, ChildFragment.OnFragmentInteractionListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Begin the transaction
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.parent_fragment_container, new ParentFragment());
        ft.commit();
    }

    @Override
    public void messageFromParentFragment(Uri uri) {
        Log.i("TAG", "received communication from parent fragment");
    }

    @Override
    public void messageFromChildFragment(Uri uri) {
        Log.i("TAG", "received communication from child fragment");
    }
}

親フラグメント

fragment_parent.xml

FrameLayout子フラグメント用の別のコンテナーを追加します。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_margin="20dp"
              android:background="#91d0c2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Parent fragment"/>

    <FrameLayout
        android:id="@+id/child_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</LinearLayout>

親フラグメント.java

getChildFragmentManagerinを使用しonViewCreatedて、子フラグメントを設定します。

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;    

public class ParentFragment extends Fragment {

    private OnFragmentInteractionListener mListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_parent, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        Fragment childFragment = new ChildFragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.replace(R.id.child_fragment_container, childFragment).commit();
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void messageFromParentFragment(Uri uri);
    }
}

子フラグメント

fragment_child.xml

ここには特別なことは何もありません。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_margin="20dp"
              android:background="#f1ff91">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Child fragment"/>
</LinearLayout>

ChildFragment.java

ここでも特別なことは何もありません。

import android.support.v4.app.Fragment;

public class ChildFragment extends Fragment {

    private OnFragmentInteractionListener mListener;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_child, container, false);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void messageFromChildFragment(Uri uri);
    }
}

ノート

于 2016-09-13T10:29:16.947 に答える
73

Android 4.2 (API 17) 以降、ネストされたフラグメントが利用可能になりましたhttp://developer.android.com/about/versions/android-4.2.html#NestedFragments

フラグメントを他のフラグメント内に配置するには、getChildFragmentManager() を使用します

サポートライブラリにもあります!

于 2012-12-03T08:18:02.563 に答える
13

onDestroyView()フラグメントは他のフラグメント内に追加できますが、親フラグメントのメソッドが呼び出されるたびに、親フラグメントからフラグメントを削除する必要があります。親フラグメントのonCreateView()メソッドに再度追加します。

このようにしてください:

@Override
    public void onDestroyView()
    {
                FragmentManager mFragmentMgr= getFragmentManager();
        FragmentTransaction mTransaction = mFragmentMgr.beginTransaction();
                Fragment childFragment =mFragmentMgr.findFragmentByTag("qa_fragment")
        mTransaction.remove(childFragment);
        mTransaction.commit();
        super.onDestroyView();
    }
于 2012-08-23T10:10:07.293 に答える
4

getChildFragmentManager() を使用し、リンクをたどってください: Nested Fragment

于 2014-12-23T05:47:32.870 に答える
2

現在、ネストされたフラグメントでは、ネストされたフラグメントは、プログラムで生成された場合にのみサポートされます! そのため、現時点では、ネストされたフラグメント レイアウトは xml レイアウト スキームでサポートされていません。

于 2013-09-30T07:03:33.763 に答える
1

のサポートはありませんMapFragment。Android チームは、Android 3.0 以降に取り組んでいると述べています。問題の詳細については、ここで説明しますが、MapActivity. コード例を次に示しますinazaruk に感謝:

使い方 :

  • MainFragmentActivity は、FragmentActivity2 つの MapFragment を拡張してホストするアクティビティです。
  • MyMapActivity は MapActivity を拡張し、MapView.
  • LocalActivityManagerFragmentLocalActivityManager をホストします。
  • MyMapFragment は拡張LocalActivityManagerFragmentされ、TabHostMyMapActivity の内部インスタンスを作成します。

ご不明な点がございましたら、お知らせください

于 2012-07-12T18:10:21.867 に答える
0

こんにちは、フラグメントごとに個別のレイアウトに配置することでこの問題を解決しました。関連するレイアウトだけを表示し、他の可視性をなくしました。

つまり:

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

     <LinearLayout android:id="@+id/linearLayout1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal">
           <Button android:layout_width="wrap_content"
                   android:id="@+id/button1"
                   android:layout_height="wrap_content"
                   android:text="Button1"></Button>
           <Button android:text="Button2"
                   android:id="@+id/button2"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"></Button>
           <Button android:text="Button3"
                   android:id="@+id/button3"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"></Button>
           <Button android:text="Button4"
                   android:id="@+id/button4"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"></Button>
   </LinearLayout>
   <LinearLayout android:layout_width="full_screen"
              android:layout_height="0dp"
              android:layout_weight="1"
              android:id="action_For_Button1"
              android:visibility="visible">
                    <Fragment android:layout_width="full_screen"
                              android:layout_height="full_screen"
                              android:id="fragment1"
                                        .
                                        .
                                        .
             / >
     </LinearLayout>

     <LinearLayout android:layout_width="full_screen"
              android:layout_height="0dp"
              android:id="action_For_Button1"
              android:layout_weight="1"
              android:visibility="gone">
                       <Fragment android:layout_width="full_screen"
                                 android:layout_height="full_screen"
                                 android:id="fragment2"
                                           .
                                           .
                                           .
             / >
        </LinearLayout>
                     .
                     .
                     .
        </LinearLayout>

ボタン1がクリックされたときにページを開くと仮定しました。クリックアクションでフラグメントの可視性を制御できます。関連するレイアウトを表示し、他のレイアウトを非表示にし、フラグメントマネージャーでフラグメントを取得できます。このアプローチは私にとってはうまくいきました。そして、 views:gone を持つビューは非表示であり、レイアウトのためにスペースを必要としないため、このアプローチはスペースの問題を引き起こさないと思います。

PS: ソリューション コードに構文エラーまたは未完成の構造がある可能性があることを説明しようとしました。

于 2012-11-26T00:31:58.307 に答える