Fragment跳转时传递参数及结果回传的方法

今天总结一下Fragment间的参数传递及结果返回的方法。html

效果图:java

一、点击“加载第二个Fragment按钮”,加载出第二个Fragment,同时传递过去参数:“从Fragment1传来的参数”这几个String;android

二、当用户点击第二个Fragment中的几个图片时,将点中的结果返回给第一个Fragment,将用户的选择在第一个Fragment显示出来架构



1、基本架构搭建

首先,咱们要把整个架构搭起来,而后再进行参数传递和回传ide

(一)、基本XML构建:

根据上面的效果,你们很容易看到两个Fragment的布局:函数

一、Fragment1的布局:(fragment1.xml)布局

很简单,垂直布局,上面一个ImageView来盛装返回过来的图片结果,下面一个Button来用来点击加载第二个Fragment;spa

[java] view plain copy 在CODE上查看代码片派生到个人代码片.net

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  code

  2.     android:layout_width="match_parent"  

  3.     android:layout_height="match_parent"  

  4.     android:background="#ffffff"  

  5.     android:orientation="vertical">  

  6.     <ImageView  

  7.         android:id="@+id/img_result"  

  8.         android:layout_width="100dp"  

  9.         android:layout_height="100dp"  

  10.         android:scaleType="center"/>  

  11.   

  12.     <Button  

  13.         android:id="@+id/load_fragment2_btn"  

  14.         android:layout_width="fill_parent"  

  15.         android:layout_height="wrap_content"  

  16.         android:text="加载第二个Fragment"/>  

  17.   

  18. </LinearLayout>  

二、Fragment2的布局:(fragment2.xml)

这个也是垂直布局,上面的一个TextView用来盛装从Fragment1传过来的String参数,下面的几个ImageView用来显示几个供用户选择的图片

[html] view plain copy 在CODE上查看代码片派生到个人代码片

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  2.     android:layout_width="match_parent"  

  3.     android:layout_height="match_parent"  

  4.     android:background="#ffffff"  

  5.     android:orientation="vertical">  

  6.   

  7.     <TextView  

  8.         android:id="@+id/textview"  

  9.         android:layout_width="wrap_content"  

  10.         android:layout_height="wrap_content"  

  11.         android:text="This is fragment 2"  

  12.         android:textColor="#000000"  

  13.         android:textSize="25sp" />  

  14.   

  15.     <ImageView  

  16.         android:id="@+id/img1"  

  17.         android:layout_width="100dip"  

  18.         android:layout_height="100dp"  

  19.         android:scaleType="center"  

  20.         android:src="@drawable/animal1"/>  

  21.   

  22.     <ImageView  

  23.         android:id="@+id/img2"  

  24.         android:layout_width="100dip"  

  25.         android:layout_height="100dp"  

  26.         android:scaleType="center"  

  27.         android:src="@drawable/animal2"/>  

  28.   

  29.     <ImageView  

  30.         android:id="@+id/img3"  

  31.         android:layout_width="100dip"  

  32.         android:layout_height="100dp"  

  33.         android:scaleType="center"  

  34.         android:src="@drawable/animal3"/>  

  35.   

  36.     <ImageView  

  37.         android:id="@+id/img4"  

  38.         android:layout_width="100dip"  

  39.         android:layout_height="100dp"  

  40.         android:scaleType="center"  

  41.         android:src="@drawable/animal4"/>  

  42.   

  43. </LinearLayout>  

(二)对应的Fragment类

一、在MainActivity初始化时,将Fragment1显示出来:
MainActivity对应的XML文件:(main_activity.xml)

[html] view plain copy 在CODE上查看代码片派生到个人代码片

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  2.     xmlns:tools="http://schemas.android.com/tools"  

  3.     android:id="@+id/main_layout"  

  4.     android:layout_width="match_parent"  

  5.     android:layout_height="match_parent"  

  6.     tools:context=".MainActivity">  

  7.   

  8.     <TextView  

  9.         android:text="@string/hello_world"  

  10.         android:layout_width="wrap_content"  

  11.         android:layout_height="wrap_content" />  

  12.   

  13. </RelativeLayout>  

对应的代码:

[java] view plain copy 在CODE上查看代码片派生到个人代码片

  1. public class MainActivity extends Activity {  

  2.   

  3.     @Override  

  4.     protected void onCreate(Bundle savedInstanceState) {  

  5.         super.onCreate(savedInstanceState);  

  6.         setContentView(R.layout.activity_main);  

  7.         Fragment1 fragment1 = new Fragment1();  

  8.         getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit();  

  9.     }  

  10. }  

二、Fragment1:在用户点击时,将fragment2添加到当前页面显示出来;

[java] view plain copy 在CODE上查看代码片派生到个人代码片

  1. public class Fragment1 extends Fragment {  

  2.   

  3.     @Override  

  4.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  

  5.                              Bundle savedInstanceState) {  

  6.         View view = inflater.inflate(R.layout.fragment1, container, false);  

  7.         Button btn = (Button)view.findViewById(R.id.load_fragment2_btn);  

  8.         btn.setOnClickListener(new View.OnClickListener(){  

  9.             @Override  

  10.             public void onClick(final View view) {  

  11.                 Fragment2 fragment2 = new Fragment2();  

  12.                   

  13.                 FragmentTransaction transaction = getFragmentManager().beginTransaction();  

  14.   

  15.                 transaction.add(R.id.main_layout, fragment2);  

  16.                 transaction.addToBackStack(null);  

  17.                 transaction.commit();  

  18.             }  

  19.         });  

  20.         return view;  

  21.     }  

  22. }  

三、Fragment2:至于目前的它仍是很简单的,只要能显示出来 就行了,因此他的代码为:

[java] view plain copy 在CODE上查看代码片派生到个人代码片

  1. public class Fragment2 extends Fragment implements View.OnClickListener {  

  2.   

  3.     @Override  

  4.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  

  5.         View view = inflater.inflate(R.layout.fragment2, container, false);  

  6.         return view;  

  7.     }  

  8.   

  9. }  


2、Fragment间参数传递

至于Fragment间参数为何要用SetArguments来传递,我就不讲了,看这篇文章:《Android解惑 - 为何要用Fragment.setArguments(Bundle bundle)来传递参数》,我这里只说项目中如何使用:

在Fragment2中,新建一个函数:newInstance(String  text)来接收传过来的参数:

新建一个Fragment2实例,而后将参数经过SetArguments设置到其中;

[java] view plain copy 在CODE上查看代码片派生到个人代码片

  1. public static Fragment2 newInstance(String text) {  

  2.     Fragment2 fragment = new Fragment2();  

  3.     Bundle args = new Bundle();  

  4.     args.putString("param", text);  

  5.     fragment.setArguments(args);  

  6.     return fragment;  

  7. }  

而后在Fragment2的OnCreateView的时候再从arguments中获取参数:

[java] view plain copy 在CODE上查看代码片派生到个人代码片

  1. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  

  2.     View view =  inflater.inflate(R.layout.fragment2, container, false);  

  3.     if (getArguments() != null) {  

  4.         String mParam1 = getArguments().getString("param");  

  5.         TextView tv =  (TextView)view.findViewById(R.id.textview);  

  6.         tv.setText(mParam1);  

  7.     }  

  8.     return view;  

  9. }  

在Fragment1中,在调起Fragmen2t时,经过调用newInstance函数来获取实例并传递参数:

[java] view plain copy 在CODE上查看代码片派生到个人代码片

  1. public class Fragment1 extends Fragment {  

  2.   

  3.     @Override  

  4.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  

  5.                              Bundle savedInstanceState) {  

  6.         View view = inflater.inflate(R.layout.fragment1, container, false);  

  7.         Button btn = (Button)view.findViewById(R.id.load_fragment2_btn);  

  8.         btn.setOnClickListener(new View.OnClickListener(){  

  9.             @Override  

  10.             public void onClick(final View view) {  

  11.                 Fragment2 fragment2 = Fragment2.newInstance("从Fragment1传来的参数");  

  12.   

  13.                 FragmentTransaction transaction = getFragmentManager().beginTransaction();  

  14.                 transaction.add(R.id.main_layout, fragment2);  

  15.                 transaction.addToBackStack(null);  

  16.                 transaction.commit();  

  17.             }  

  18.         });  

  19.         return view;  

  20.     }  

  21. }  

相关文章
相关标签/搜索