2016-09-18 17 views
1

こんにちは、ここでは、 "Add_Create_2"を "Add_Create"から開始したいと思います。私のコードは、フラグメントがマージされ、前のフラグメントが削除されないためです。今まで私はAdd_Createレイアウトフラグメントから新しいフラグメントを開くことができません。

Add_Create Java Code 
    @Override 
     public void onClick(View view) { 
      FragmentTransaction transaction=getActivity().getSupportFragmentManager() 
        .beginTransaction() 
        .replace(R.id.root,new Add_Create_2()) 
        ; 
      transaction.addToBackStack(null); 

      transaction.commit(); 

     } 

Add_Create XML Code 
     <ScrollView 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:fillViewport="true"> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:id="@+id/root" 

    android:layout_height="match_parent"> 

    <TextView 
     android:text="General Information" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     tools:layout_editor_absoluteX="151dp" 
     tools:layout_editor_absoluteY="15dp" 
     android:id="@+id/textView1" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="20dp" 
     android:textColor="@color/colorPrimary" 
     android:textSize="20dp" 
     android:textStyle="bold" 
     android:layout_alignParentTop="true" /> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginLeft="10dp" 
     android:hint="Enter Announcement Name" 
     android:backgroundTint="@color/lightblue" 
     android:textColorHint="#3F51B5" 
     android:textColor="#3F51B5" 
     android:id="@+id/announcement_name" 
     android:layout_below="@+id/textView1" 
     android:layout_centerHorizontal="true" /> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginLeft="10dp" 
     android:hint="Enter Description" 
     android:backgroundTint="@color/lightblue" 
     android:textColorHint="#3F51B5" 
     android:textColor="#3F51B5" 
     android:id="@+id/description" 
     android:maxLines="4" 
     android:lines="2" 
     android:layout_below="@+id/announcement_name" 
     android:layout_centerHorizontal="true" /> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:backgroundTint="@color/lightblue" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginLeft="10dp" 
     android:hint="Enter Price" 
     android:textColorHint="#3F51B5" 
     android:textColor="#3F51B5" 
     android:id="@+id/price" 
     android:layout_below="@+id/description" 
     android:layout_centerHorizontal="true" /> 
    <Spinner 
    android:id="@+id/duration" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:entries="@array/items" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginLeft="10dp" 

     android:layout_below="@+id/price" 
    android:prompt="@string/duration" /> 

    <TextView 
     android:text="Choose Image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     tools:layout_editor_absoluteX="151dp" 
     tools:layout_editor_absoluteY="15dp" 
     android:id="@+id/image1" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="20dp" 
     android:textColor="@color/colorPrimary" 
     android:textSize="20dp" 
     android:layout_below="@+id/duration" /> 
    <Button 
     android:text="Choose" 
     android:layout_width="wrap_content" 
     android:layout_below="@+id/duration" 
     android:layout_height="wrap_content" 
     android:layout_marginStart="21dp" 
     android:id="@+id/Image_Picker" 
     android:layout_gravity="right" 
     android:layout_marginTop="20px" 
     android:textColor="#FFFFFF" 
     android:layout_marginLeft="150dp" 
     android:background="@drawable/buttons" 
     android:layout_centerHorizontal="true" 
     android:layout_toRightOf="@+id/image1" /> 
    <TextView 
     android:text="Location" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     tools:layout_editor_absoluteX="151dp" 
     tools:layout_editor_absoluteY="15dp" 
     android:id="@+id/textView2" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="20dp" 
     android:layout_below="@+id/image1" 
     android:textColor="@color/colorPrimary" 
     android:textSize="20dp" 
     android:textStyle="bold" /> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginLeft="10dp" 
     android:hint="Enter Location" 
     android:backgroundTint="@color/lightblue" 
     android:textColorHint="#3F51B5" 
     android:textColor="#3F51B5" 
     android:id="@+id/room_address" 
     android:maxLines="4" 
     android:lines="2" 
     android:layout_below="@+id/textView2" 
     android:layout_centerHorizontal="true" /> 
     <Button 
     android:text="Next" 
     android:layout_width="wrap_content" 
     android:layout_below="@+id/room_address" 
     android:layout_height="wrap_content" 
     android:id="@+id/Submit_Room_Information" 
     android:textColor="#FFFFFF" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="10dp" 
      android:layout_alignParentRight="true" 
     android:layout_marginRight="20dp" 
      android:background="@drawable/buttons" /> 

</RelativeLayout> 

Add_Create_2 Java Code 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    return inflater.inflate(R.layout.create_add_2, container, false); 
} 

Add_Create_2 XML Code 
<?xml version="1.0" encoding="utf-8"?> 


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_height="match_parent"> 
<TextView 
    android:text="Availability" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    tools:layout_editor_absoluteX="151dp" 
    tools:layout_editor_absoluteY="15dp" 
    android:id="@+id/textView5" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="20dp" 
    android:textColor="@color/colorPrimary" 
    android:textSize="20dp" 
    android:textStyle="bold" /> 
<CheckBox 
    android:id="@+id/checkbox_alltheday" 
    android:layout_width="wrap_content" 
    android:layout_below="@+id/textView5" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="20dp" 
    android:layout_marginRight="10dp" 
    android:textColor="@color/colorPrimary" 
    android:layout_marginLeft="10dp" 
    android:text="All the Day" /> 
<TextView 
    android:text="Start Time" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    tools:layout_editor_absoluteX="151dp" 
    tools:layout_editor_absoluteY="15dp" 
    android:id="@+id/textView7" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="20dp" 
    android:layout_below="@+id/checkbox_alltheday" 
    android:textColor="@color/colorPrimary" 
    android:textSize="20dp" /> 
<TextView 
    android:text="Services" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    tools:layout_editor_absoluteX="151dp" 
    tools:layout_editor_absoluteY="15dp" 
    android:id="@+id/textView9" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="20dp" 
    android:textColor="@color/colorPrimary" 
    android:textSize="20dp" 
    android:layout_below="@+id/textView8" 

    android:textStyle="bold" /> 

<CheckBox 
    android:id="@+id/service_bathroom" 
    android:layout_width="wrap_content" 
    android:layout_below="@+id/textView9" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="20dp" 
    android:layout_marginRight="10dp" 
    android:textColor="@color/colorPrimary" 
    android:layout_marginLeft="10dp" 
    android:text="Bathroom" /> 

    android:text="" /> 
<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/service_other" 
    android:layout_marginTop="17dp" 

    android:backgroundTint="@color/lightblue" 
    android:layout_marginRight="10dp" 
    android:layout_marginLeft="10dp" 
    android:hint="Other" 
    android:textColorHint="#3F51B5" 
    android:textColor="#3F51B5" 
    android:id="@+id/other" 
    android:layout_below="@+id/service_parking" 
    /> 

here is the result

に存在して[次へ]ボタンをクリックすると、最初のフラグメントの上部に表示

答えて

1

トランジションは、フラグメントではなくアクティビティで処理する必要があります。フラグメントを変更するイベントが発生したら、そのアクティビティがフラグメントを変更する必要がある場合は、この情報をアクティビティに与えます。 すなわち、遷移を実行し、必要に応じてフラグメントからその関数を呼び出すアクティビティで関数を作成します。 あなたは活動のすべての方法どちらかgetActivity(インターフェースまたは型キャストを作成することによって)このコードを修正する必要があり MainActivityのXML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="yourapp.com.fragments.MainActivity"> 

    <FrameLayout 
     android:id="@+id/root" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"></FrameLayout> 
</RelativeLayout> 
方法

を呼び出すためにあなたの活動のクラス型で方法

MainActivity.java

public class MainActivity extends AppCompatActivity { 
    FrameLayout root; 
    AddCreate firstFragment; 
    AddCreate2 secondFragment; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     firstFragment = new AddCreate(); 
     secondFragment = new AddCreate2(); 
     showFragment(0); 

    } 

    public void showFragment(int which) { 
     android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
     switch (which) { 
      case 0: 
       transaction.replace(R.id.root,firstFragment); 
       break; 
      case 1: 
       transaction.replace(R.id.root,secondFragment); 
       break; 
     } 
     transaction.addToBackStack(null); 
     transaction.commit(); 
    } 
} 

AddCreate.java

public class AddCreate extends Fragment { 


    public AddCreate() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.add__create, container, false); 
     v.findViewById(R.id.Submit_Room_Information).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       ((MainActivity)getActivity()).showFragment(1); 
      } 
     }); 
     return v; 
    } 

} 

AddCreate.xml

<ScrollView 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"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="pantherstechnik.com.fragments.fragment.AddCreate"> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="20dp" 
      android:text="General Information" 
      android:textColor="@color/colorPrimary" 
      android:textSize="20dp" 
      android:textStyle="bold" 
      tools:layout_editor_absoluteX="151dp" 
      tools:layout_editor_absoluteY="15dp" /> 

     <EditText 
      android:id="@+id/announcement_name" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textView1" 
      android:layout_centerHorizontal="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="20dp" 
      android:hint="Enter Announcement Name" 
      android:textColor="#3F51B5" 
      android:textColorHint="#3F51B5" /> 

     <EditText 
      android:id="@+id/description" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/announcement_name" 
      android:layout_centerHorizontal="true" 
      android:layout_marginLeft="10dp" 

      android:layout_marginRight="10dp" 
      android:layout_marginTop="20dp" 
      android:hint="Enter Description" 
      android:lines="2" 
      android:maxLines="4" 
      android:textColor="#3F51B5" 
      android:textColorHint="#3F51B5" /> 

     <EditText 
      android:id="@+id/price" 
      android:layout_width="match_parent" 

      android:layout_height="wrap_content" 
      android:layout_below="@+id/description" 
      android:layout_centerHorizontal="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" 
      android:hint="Enter Price" 
      android:textColor="#3F51B5" 
      android:textColorHint="#3F51B5" /> 

     <Spinner 
      android:id="@+id/duration" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 

      android:layout_below="@+id/price" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 

      android:layout_marginTop="10dp" 
      /> 

     <TextView 
      android:id="@+id/image1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/duration" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="20dp" 
      android:text="Choose Image" 
      android:textColor="@color/colorPrimary" 
      android:textSize="20dp" 
      tools:layout_editor_absoluteX="151dp" 
      tools:layout_editor_absoluteY="15dp" /> 

     <Button 
      android:id="@+id/Image_Picker" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/duration" 
      android:layout_centerHorizontal="true" 
      android:layout_gravity="right" 
      android:layout_marginLeft="150dp" 
      android:layout_marginStart="21dp" 
      android:layout_marginTop="20px" 
      android:layout_toRightOf="@+id/image1" 

      android:text="Choose" 
      android:textColor="#FFFFFF" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/image1" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="20dp" 
      android:text="Location" 
      android:textColor="@color/colorPrimary" 
      android:textSize="20dp" 
      android:textStyle="bold" 
      tools:layout_editor_absoluteX="151dp" 
      tools:layout_editor_absoluteY="15dp" /> 

     <EditText 
      android:id="@+id/room_address" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textView2" 
      android:layout_centerHorizontal="true" 
      android:layout_marginLeft="10dp" 

      android:layout_marginRight="10dp" 
      android:layout_marginTop="20dp" 
      android:hint="Enter Location" 
      android:lines="2" 
      android:maxLines="4" 
      android:textColor="#3F51B5" 
      android:textColorHint="#3F51B5" /> 

     <Button 
      android:id="@+id/Submit_Room_Information" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_below="@+id/room_address" 
      android:layout_marginBottom="10dp" 
      android:layout_marginRight="20dp" 
      android:layout_marginTop="10dp" 
      android:text="Next" 
      android:textColor="#FFFFFF" /> 

    </RelativeLayout> 

</ScrollView> 

AddCreate2のXML

<ScrollView 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" 
    tools:context="pantherstechnik.com.fragments.fragment.AddCreate2"> 
    <LinearLayout 
     android:orientation="vertical" android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
    <TextView 
     android:text="Availability" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     tools:layout_editor_absoluteX="151dp" 
     tools:layout_editor_absoluteY="15dp" 
     android:id="@+id/textView5" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="20dp" 
     android:textColor="@color/colorPrimary" 
     android:textSize="20dp" 
     android:textStyle="bold" /> 
    <CheckBox 
     android:id="@+id/checkbox_alltheday" 
     android:layout_width="wrap_content" 
     android:layout_below="@+id/textView5" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:layout_marginRight="10dp" 
     android:textColor="@color/colorPrimary" 
     android:layout_marginLeft="10dp" 
     android:text="All the Day" /> 
    <TextView 
     android:text="Start Time" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     tools:layout_editor_absoluteX="151dp" 
     tools:layout_editor_absoluteY="15dp" 
     android:id="@+id/textView7" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="20dp" 
     android:layout_below="@+id/checkbox_alltheday" 
     android:textColor="@color/colorPrimary" 
     android:textSize="20dp" /> 
    <TextView 
     android:text="Services" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     tools:layout_editor_absoluteX="151dp" 
     tools:layout_editor_absoluteY="15dp" 
     android:id="@+id/textView9" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="20dp" 
     android:textColor="@color/colorPrimary" 
     android:textSize="20dp" 
     android:layout_below="@+id/textView8" 

     android:textStyle="bold" /> 

    <CheckBox 
     android:id="@+id/service_bathroom" 
     android:layout_width="wrap_content" 
     android:layout_below="@+id/textView9" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:layout_marginRight="10dp" 
     android:textColor="@color/colorPrimary" 
     android:layout_marginLeft="10dp" 
     android:text="Bathroom" /> 

    android:text="" /> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/service_other" 
     android:layout_marginTop="17dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginLeft="10dp" 
     android:hint="Other" 
     android:textColorHint="#3F51B5" 
     android:textColor="#3F51B5" 
     android:id="@+id/other" 
     android:layout_below="@+id/service_parking" 
     /> 
    </LinearLayout> 
</ScrollView> 

AddCreate2.java

public class AddCreate2 extends Fragment { 


    public AddCreate2() { 
     // Required empty public constructor 
    } 


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

} 
+0

私は自分のアクティビティで関数を作成し、フラグメントボタンのクリックから呼び出す必要があることをお勧めしますか? –

+0

@ AlicaCorwinはい –

+0

ちょうどこれのように? アクティビティa1 =新しいアクティビティ(); a1.method() –

0

onClickにこれを追加します。

  FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
      transaction.replace(R.id.root,new Add_Create_2());  
      transaction.addToBackStack(null); 
      transaction.commit(); 
+0

NAHは問題を解決しませんでした –

+0

ここにあなたのlogcatを追加してください。 – W4R10CK

+0

エラーはありませんスクリーンショットを参照してください –

関連する問題