2012-06-26 15 views
10

Activityを作成して、ユーザーが通過できるメニューを表示したいとします。項目をクリックすると、新しい画面が表示され、ユーザーはより多くのオプションを選択できます(ウィザードのような)。単一のアクティビティでフラグメント間を切り替える

これはFragmentを使用して実装したいと思っていましたが、うまく動作しません。
今私が持っている:

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" 
    android:id="@+id/main_fragmentcontainer" > 

    <fragment 
     android:id="@+id/mainmenufragment" 
     android:name="com.myapp.MainMenuFragment" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

    <fragment 
     android:id="@+id/secondmenufragment" 
     android:name="com.myapp.SecondMenuFragment" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</LinearLayout> 

MainMenuFragmentOnClickListenerで:

:今

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.mainmenu, container, false); 

    setupButton(view); 
    return view; 
} 

/* Button setup code omitted */ 

@Override 
public void onClick(View v) { 
    SherlockFragment secondRunMenuFragment = (SherlockFragment) getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.secondmenufragment); 
    FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction(); 

    transaction.replace(android.R.id.content, secondMenuFragment); //also crashes with R.id.main_fragmentcontainer 
    transaction.addToBackStack(null); 
    transaction.commit(); 
} 

私は、ボタン、このlogcatを使用してアプリケーションのクラッシュを押すと、

06-27 01:45:26.309: E/AndroidRuntime(8747):java.lang.IllegalStateException:フラグメントのコンテナIDを変更できません。SecondMenuFragment {405e2a70#1 id = 0x7f060029}:2131099687 now 2131099687
06-27 01:45:26.309:E/AndroidRuntime 8747):android.support.v4.app.BackStackRecord.doAddOp(不明なソース)
06-27 01:45:26.309:E/AndroidRuntime(8747):android.support.v4.app.BackStackRecord.replace不明なソース)
06-27 01:45:26.309:E/AndroidRuntime(8747):android.support.v4.app.BackStackRecord.replace(不明なソース)
06-27 01:45:26.309:E/AndroidRuntime(8747):com.myapp.MainMenuFragment $ MyButtonOnClickListener.onClick(MainMenuFragment.java:52)

私は間違って何をしていますか?

+0

[ここに同様の質問があります](http://stackoverflow.com/questions/9092048/swap-two-fragment-同時に) – adneal

+0

ウィザードを作成しようとしている場合、2番目の「」は何ですか? – CommonsWare

+0

私はこれらのリンクを実際に見たことがありますが、私の場合はどのように適用するのか分かりません。 – nhaarman

答えて

14

個人的には、<fragment>要素はありません。

ステップ1:ウィザードの可変部分に加えて、さまざまなボタンのアクティビティレイアウトに<FrameLayout>を入力します。

ステップ2:アクティビティのonCreate()に、FragmentTransactionを実行して、最初のウィザードページをFrameLayoutにロードします。

ステップ3:「次へ」をクリックしてFragmentTransactionを実行し、FrameLayoutの内容をウィザードの次のページに置き換えます。

ステップ4:ボタンが使用できなくなったときにボタンを無効にするために、適切なスマートを追加します(たとえば、最初のウィザードページに戻る)。

また、ウィザードの[戻る]ボタンと一緒にBACKボタンがどのように機能するかを考えたいと思うでしょう。それらの両方が同じように動作するようにするには、各トランザクションをバックスタックに追加し、「戻る」ボタンを処理するときにバックスタックをポップする必要があります。

誰かが私にそれを打つことがなければ、私はフラグメントごとの例、またはおそらく再利用可能なコンポーネントを作成しようとします。

+0

私はあなたを正しく理解しているかどうかはわかりませんが、何とか働いています。私は空の ''を私の活動レイアウトファイルに入れました。次に、アクティビティの 'onCreate()'に、 'FragmentTransaction.add()'を呼び出して、 '012.'のIDである 'R.id.main_fragmentcontainer'に' new MainMenuFragment() 'を追加しました。 'MainMenuFragment'' onClick()'では、新しいSecondMenuFragment()を作成し、同じ方法でそれを 'R.id.main_fragmentcontainer'に再度追加します。それは少なくとも機能します。私は特にステップ1 _plusあなたの様々なボタン_で混乱しています。それはどういう意味ですか?私は 'LinearLayout'を使うことができませんでしたか? – nhaarman

+0

@ニーク:「それはどういう意味ですか?通常、ウィザードインターフェースには「前」(または「後」)ボタンと「次へ」ボタンがあります。それらをフラグメントに入れる理由はありません。すべてのウィザードステップで同じままにするので、私はあなたがそれらを直接アクティビティに置くことを想定します。 「LinearLayoutを使うことはできませんでしたか?」 - 何のために? – CommonsWare

+0

ああ、それらのボタン、私はあなたが私のメニューボタンを意味すると思った。そして、ステップ1でFrameLayoutを提案しましたが、それは "次へ"と "前へ"ボタンのオーバーレイと関係があります。ご協力いただきありがとうございます! – nhaarman

12

私はこの主なレイアウトを作成します。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    tools:context="com.example.fragmentsexample.MainActivity" > 

    <FrameLayout 
     android:id="@+id/contentFragment" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 

</LinearLayout> 

を、私はとFrameActivityに補充:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    ... 
    Fragment fragment = new Dashboard(); 
    FragmentManager fm = getSupportFragmentManager(); 
    FragmentTransaction transaction = fm.beginTransaction(); 
    transaction.replace(R.id.contentFragment, fragment); 
    transaction.commit(); 
    ... 
} 

そして、私はフラグメント(イベントのダッシュボード)cahnging、同じコードでのonClickメソッドにrepleace :

0

もう1つの選択肢は、Roman NuriのAndroid-WizardPagerです。 k。

主な機能:

  • 分岐、以降ウィザードのステップ間のユーザーの自由形式のナビゲーションを許可
  • をコミットする前にユーザーが確認できるようにすること
  • ステップの 可用性に影響を与えるためのウィザードの手順のための能力
  • 必須およびオプションのステップのサポート
  • ステップ・クラスのサポート(技術的には、各ステップはJava cのインスタンスです。小娘、)ウィザード内の複数のインスタンスを持つことができますので、

詳細情報here.

関連する問題