无法从片段中启动新片段

时间:2016-09-18 13:10:11

标签: android android-fragments

您好,我想启动片段" Add_Create_2"来自" Add_Create"这是我的代码,两个片段都被合并,前一个片段没有被删除,这就是为什么当我点击Add_Create Layout中存在的下一个按钮时,下一个片段显示在第一个片段的顶部

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

2 个答案:

答案 0 :(得分:1)

转换应由Activity处理,而不是由片段处理。当您获得更改片段的事件时,请将此信息提供给您的活动,之后该活动应更改片段。 即,在执行转换的活动中创建一个函数,并在需要时从片段调用该函数。 您可以通过在活动类类型中创建接口类型转换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;
    }

}

答案 1 :(得分:0)

onClick上添加此内容:

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