2013-07-10 60 views
18

私は次のポストを認識しています: Using Multiple Fragments in an single activityAndroidの二つの断片

私が探していますどのような特定の問題に固有の答えです。次のコードの結果は空のFragmentActivityです。次のコードでは、2つのフラグメントでアクティビティをレンダリングするために、何が欠けていますか。 1つは空のリストフラグメントであり、もう1つは入力ボックスと水平レイアウトのボタン(このレイアウトはhttp://developer.android.com/training/basics/firstapp/starting-activity.htmlにあります)を含むフラグメントです。これは、画面の下部に固定して約25ディップの高さ。

たManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.my.package" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.my.package.Application" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

私の主な活動とその関連application.xmlファイル。

package com.my.package; 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentTransaction; 

public class Application 
extends FragmentActivity 
implements MessageListViewFragment.OnLineSelectedListener, 
SendMessageFragment.OnSendButtonPressed { 

    MessageListViewFragment mMessageListFragment; 
    SendMessageFragment mSendMessageFragment; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.application); 

     mMessageListFragment = new MessageListViewFragment(); 
     mSendMessageFragment = new SendMessageFragment(); 

     FragmentTransaction transaction = 
       getSupportFragmentManager().beginTransaction(); 

     transaction.add(R.id.message_fragment, mMessageListFragment); 
     transaction.add(R.id.send_fragment, mSendMessageFragment); 

     transaction.commit(); 
    } 

    @Override 
    public void onListItemSelected(int position) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onSendButtonPressed() { 
     // TODO Auto-generated method stub 

    } 
} 

レイアウト:2つのフラグメントおよびそれらに関連するXMLファイルの

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:baselineAligned="false" 
    android:orientation="vertical" > 

    <fragment 
     android:id="@+id/message_fragment" 
     android:name="com.example.android.fragments.MessageListViewFragment" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="top" /> 

    <fragment 
     android:id="@+id/send_fragment" 
     android:name="com.example.android.fragments.SendMessageFragment" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="bottom" /> 

</LinearLayout> 

そして今: 最初のフラグメント(上部のリスト・フラグメント)

package com.my.package; 

import android.app.Activity; 
import android.os.Bundle; 
import android.support.v4.app.ListFragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class MessageListViewFragment extends ListFragment { 
    OnLineSelectedListener mCallback; 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     // This makes sure that the container activity has implemented 
     // the callback interface. If not, it throws an exception 
     try { 
      mCallback = (OnLineSelectedListener)activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement OnLineSelectedListener"); 
     } 
    } 

    // Container Activity must implement this interface 
    public interface OnLineSelectedListener { 
     public void onListItemSelected(int position); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
      ViewGroup container, Bundle savedInstanceState) { 

     return inflater.inflate(R.layout.list_fragment, null); 
    } 
} 

レイアウト:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

(一番下の)第2のフラグメント

package com.my.package; 

import android.app.Activity; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class SendMessageFragment extends Fragment { 
    OnSendButtonPressed mCallback; 

    // Container Activity must implement this interface 
    public interface OnSendButtonPressed { 
     public void onSendButtonPressed(); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
      ViewGroup container, Bundle savedInstanceState) { 

     return inflater.inflate(R.layout.input_fragment, null); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     // This makes sure that the container activity has implemented 
     // the callback interface. If not, it throws an exception 
     try { 
      mCallback = (OnSendButtonPressed)activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement OnHeadlineSelectedListener"); 
     } 
    } 
} 

レイアウト:

<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"> 
    <EditText android:id="@+id/edit_message" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/edit_message" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/button_send" /> 
</LinearLayout> 
+2

あなたはすでにあなたのコードにフラグメントをハードコードしていますので、コードに再度追加しないでください。 – tyczj

+0

XMLからそれらを削除しても問題ありませんか? –

+0

xmlでコードを削除して何が起こるかを確認 – tyczj

答えて

25

は、次の操作を実行します。両方のための主な活動のXMLでFrameLayout

  1. 変更Fragment、。
  2. メインXMLファイルのFrameLayout(手順1で作成したもの)のfill_parentmatch_parentからに変更してください。
  3. layout_heightfill_parentからwrap_contentに変更します(メインXMLファイルのFrameLayout)(手順1で作成したもの)。
  4. List Fragment XMLのFrameLayoutListViewをListであるため変更します。
  5. このLisViewのIDは、ListFragmentに必要であるため、@android:id/listに変更してください。

私に教えてください、乾杯。

編集、またこれら:

  1. 変更return inflater.inflate(R.layout.list_fragment, null);return inflater.inflate(R.layout.list_fragment, container, false);へ。
  2. 変更return inflater.inflate(R.layout.input_fragment, null);

return inflater.inflate(R.layout.input_fragment, container, false);に編集:

は、このようなあなたのメインの活動のXMLファイルを作成します。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

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

    <FrameLayout 
     android:id="@+id/send_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" /> 

</RelativeLayout> 

私はそれが何であるかを知らないので、私はandroid:name"..."を取り出したりそれが何であるかを知ることができます。確かにそれが何であるかを知っていれば、それを元に戻してください。大丈夫です。

+0

このボタンはアクティビティのビューの上部に表示されます。主なアクティビティのXMLファイルをFragmentsからFrameLayoutsに変更すると、Fragmentsとしてそれらが保持されます。これは、http://developer.android.com/training/basics/fragments/index.htmlのチュートリアルとは異なります。 –

+0

FrameLayoutはより一般的です。どんな場合でも、Googleは別のチュートリアルでFrameLayoutを使用しています。 2番目のレイアウトを一番下に置いて答えを編集しました。 – LuckyMe

+0

ここでは、http://developer.android.com/training/implementing-navigation/nav-drawer.html#DrawerLayoutより複雑なレイアウトで、FrameLayoutを使用してFragmentのウィンドウになりました。 – LuckyMe