[英]Open new Fragment from button in other Fragment
我有兩個片段。 在第一個中,我有一個按鈕,我想通過單擊按鈕更改為另一個片段。
這是我的代碼:
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>
我該如何解決?
您必須替換現有容器中的新片段。
在第一個片段中嘗試以下操作:
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();
}
});
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();
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.