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. }  

相關文章
相關標籤/搜索