<LinearLayout 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:orientation="vertical" android:gravity="center_horizontal" tools:context="ant.snail.com.fragmentactapp.Image1Fragment" > <!-- TODO: Update blank fragment layout --> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" > <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是jin兔1"/> <Button android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="传递数据到主窗口" android:id="@+id/btfr1" /> </LinearLayout> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView1" android:src="@drawable/jin1"/> </LinearLayout>
宿主MainActivity的代码如下,通过FragmentManager得到FragmentTransaction,然后通过replace()方法实现两个Fragment之间的切换,最后实现了Commit()方法:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取两个按钮的View bt1=(Button)findViewById(R.id.bt1); bt2=(Button)findViewById(R.id.bt2); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //主Activity动态加载Fragment FragmentManager fragmentManager=getFragmentManager(); //处理Fragment事务 FragmentTransaction beginTransaction=fragmentManager.beginTransaction(); //获取Image1Fragment实例 Image1Fragment image1Fragment=Image1Fragment.newInstance("Activity传递图片1到Fragment中"); //替换操作 beginTransaction.replace(R.id.imageLayout,image1Fragment); //允许用户通过按下BACK按键返回到前一个Fragment状态 beginTransaction.addToBackStack(null); beginTransaction.commit(); } }); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //主Activity动态加载Fragment FragmentManager fragmentManager=getFragmentManager(); //处理Fragment事务 FragmentTransaction beginTransaction=fragmentManager.beginTransaction(); //获取Image1Fragment实例 Image2Fragment image2Fragment=Image2Fragment.newInstance("Activity传递图片2到Fragment中"); //替换操作 beginTransaction.replace(R.id.imageLayout,image2Fragment); //允许用户通过按下BACK按键返回到前一个Fragment状态 beginTransaction.addToBackStack(null); beginTransaction.commit(); } }); }
其中NewInstance方法是在Fragment中实现的一个实例方法,用于将Fragment实例化,通过参数param1的传递能将Activity中的数据传递到Fragment中。通过setArguments()以及getArguments()实现数据从Activity向Fragment传递的目的,如下图所示。
public static Image1Fragment newInstance(String param1) {
Image1Fragment fragment = new Image1Fragment();
//在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { //在Fragment中通过getArguments().getString()获取Activity传来的值 mParam1 = getArguments().getString(ARG_PARAM1); } }
6.Fragment与Activity通信