从其他片段中的按钮打开新片段

时间:2014-09-12 07:41:02

标签: java android android-layout android-fragments

我有两个片段。在第一个,我有一个按钮,我想通过单击按钮更改为另一个片段。

这是我的代码:

FirstFragment

public class FirstFragement extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View ios = inflater.inflate(R.layout.activity_annuncio, container, false);

        Button bottAnn = (Button) ios.findViewById(R.id.bNuovoAnnuncio);
        bottAnn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            }

        });
        return ios;
    }
}

Firstxml代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bNuovoAnnuncio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nuovo Annuncio"
        android:background="@drawable/button"
        android:textColor="#ffffff" 
        android:layout_marginTop="5dp"/>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="294dp"
        android:layout_height="wrap_content" >
    </ListView>

    </LinearLayout>
 </ScrollView>

第二条片段

public class SecondFragement extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View ios = inflater.inflate(R.layout.activity_annuncio, container, false);
        return ios;
    }
}

Secondxml代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="294dp"
        android:layout_height="wrap_content" >
    </ListView>

    </LinearLayout>
 </ScrollView>

我怎么解决?

2 个答案:

答案 0 :(得分:0)

您必须在现有容器中替换新片段。

在第一个片段中尝试这个:

Button bottAnn = (Button) ios.findViewById(R.id.bNuovoAnnuncio);
bottAnn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {        

    SecondFragement frag = new SecondFragement();

    getActivity().getFragmentManager().beginTransaction().replace(//Your container, frag).commit();

  }

});

答案 1 :(得分:0)

TextView textView_paydetails;
FragmentManager fragmentManager_payment;
Button button_placeorder;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View view = inflater.inflate(R.layout.fragment_paymentdetails,
            container, false);
    textView_paydetails = (TextView) view.findViewById(R.id.txtsdid);
    button_placeorder = (Button) view.findViewById(R.id.btnponid);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    fragmentManager_payment = getFragmentManager();
    textView_paydetails.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        /*  // TODO Auto-generated method stub
            PaymentShipping payment_shipping = new PaymentShipping();
            FragmentTransaction fragmentTransaction = fragmentManager_payment
                    .beginTransaction();
            // Locate Position

            fragmentTransaction.replace(R.id.fsp, payment_shipping, "A");

            fragmentTransaction.commit();*/
        }
    });

    button_placeorder.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            SecondFragement payment_shippingmethod = new SecondFragement();
            FragmentTransaction fragmentTransaction = fragmentManager_payment
                    .beginTransaction();
            // Locate Position

            fragmentTransaction.replace(R.id.fsp, payment_shippingmethod,
                    "A");
            //fragmentTransaction.addToBackStack("A");
            fragmentTransaction.commit();
        }
    });

}