如何使用按钮将片段移动到片段

时间:2015-10-25 06:04:59

标签: android android-fragments

我创建了一个NavigationDrawer来选择一些fragments,但在SchedulingFragment内我想要一个按钮打开另一个fragment selectservicefragment,有人可以帮我?

安排片段

public class SchedulingFragment extends android.support.v4.app.Fragment {

    Spinner spinner;
    Spinner spinner2;
    ArrayAdapter<CharSequence> adapter;
    Button proxBtn;


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




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

        View v = inflater.inflate(R.layout.fragment_scheduling, container, false);

        String[] values =
                {"2015","2014","2013","2012","2011"};
        Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
        ArrayAdapter<String> LTRadapter = new ArrayAdapter<String>(this.getActivity(),R.layout.spinner_center_item, values);
        LTRadapter.setDropDownViewResource(R.layout.spinner_center_item);
        spinner.setAdapter(LTRadapter);

        String[] values2 =
                {"CHEVROLET"};
        Spinner spinner2 = (Spinner) v.findViewById(R.id.spinner2);
        ArrayAdapter<String> LTRadapter2 = new ArrayAdapter<String>(this.getActivity(),R.layout.spinner_center_item, values2);
        LTRadapter2.setDropDownViewResource(R.layout.spinner_center_item);
        spinner2.setAdapter(LTRadapter2);

        String[] values3 =
                {"CAPTIVA", "CELTA", "CLASSIC", "COBALT", "CRUZE", "MONTANA", "ONIX", "S10", "PRISMA"};
        Spinner spinner3 = (Spinner) v.findViewById(R.id.spinner3);
        ArrayAdapter<String> LTRadapter3 = new ArrayAdapter<String>(this.getActivity(),R.layout.spinner_center_item, values3);
        LTRadapter3.setDropDownViewResource(R.layout.spinner_center_item);
        spinner3.setAdapter(LTRadapter3);

        String[] values4 =
                {"1.8 Ecotec 6 - 4 Portas LT"};
        Spinner spinner4 = (Spinner) v.findViewById(R.id.spinner4);
        ArrayAdapter<String> LTRadapter4 = new ArrayAdapter<String>(this.getActivity(),R.layout.spinner_center_item, values4);
        LTRadapter4.setDropDownViewResource(R.layout.spinner_center_item);
        spinner4.setAdapter(LTRadapter4);


        return v;

    }


}

scheduling.xml

<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" tools:context="com.parse.starter.SchedulingFragment"
    android:background="@drawable/pluscar1"
    android:id="@+id/fragmenholder2">

    <!-- TODO: Update blank fragment layout -->
    <TextView android:layout_width="match_parent" android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="0dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"></TextView>
        <!--android:text="@string/agendamento"-->
        android:gravity="center"/>
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="left|top"
    android:id="@+id/fragmentScheduling"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="0dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="0dp">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true">


    </LinearLayout>

    <TextView
        android:text="@string/ano"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/button_material_light"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="24sp"
        android:textStyle="bold"
        android:gravity="top|center"
        android:id="@+id/ano"
        />

    <Spinner

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_alignParentTop="true"
        android:layout_marginTop="42dp"
        android:layout_gravity="center"
        android:textColor="@color/button_material_light" />

    <TextView
        android:text="@string/marca"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="24sp"
        android:textStyle="bold"
        android:gravity="top|center"
        android:id="@+id/marca"
        android:layout_below="@+id/spinner"
        android:layout_centerHorizontal="true"
        android:textColor="@color/button_material_light"/>

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner2"
        android:layout_gravity="center"
        android:layout_marginTop="39dp"
        android:layout_alignTop="@+id/marca"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textColor="@color/button_material_light"/>

    <TextView
        android:text="@string/modelo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="24sp"
        android:textStyle="bold"
        android:gravity="top|center"
        android:id="@+id/modelo"
        android:layout_below="@+id/spinner2"
        android:layout_centerHorizontal="true"
        android:textColor="@color/button_material_light"/>

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner3"
        android:layout_gravity="center"
        android:layout_alignTop="@+id/modelo"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="50dp"
        android:textColor="@color/button_material_light"/>

    <TextView
        android:text="@string/motor"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="24sp"
        android:textStyle="bold"
        android:gravity="top|center"
        android:id="@+id/motor"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/spinner3"
        android:textColor="@color/button_material_light"/>

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner4"
        android:layout_alignTop="@+id/motor"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="50dp"
        android:textColor="@color/button_material_light"></Spinner>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Próximo"
        android:id="@+id/proxBtn"
        android:layout_gravity="center_horizontal"
        android:layout_below="@+id/spinner4"
        android:layout_alignParentTop="false"
        android:layout_alignParentRight="false"
        android:layout_centerHorizontal="true"
        android:textColor="@color/abc_input_method_navigation_guard"
        style="@style/btnStyleBeige" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

使用FragmentTransaction。

您的点击:

@Override
            public void onClick(View v) {
                switch (v.getId()) {
                case R.id.btnSearch:

                   FragmentManager fm = getFragmentManager();
                   FragmentTransaction ft = fm.beginTransaction();
                   ft.replace(R.id.show_fragment, new TestFragment(), "fragment_screen");
                    ft.commit();
                 break;
                }
            }   

yourfile.xml:

<FrameLayout
        android:id="@+id/show_fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp">

    </FrameLayout>

答案 1 :(得分:0)

Fragment newFragment = new ProductListFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.remove(newFragment).commit();