Fragment跳轉(zhuǎn)時(shí)傳遞參數(shù)及結(jié)果回傳的方法(推薦)
今天總結(jié)一下Fragment間的參數(shù)傳遞及結(jié)果返回的方法。
效果圖:
1、點(diǎn)擊“加載第二個(gè)Fragment按鈕”,加載出第二個(gè)Fragment,同時(shí)傳遞過(guò)去參數(shù):“從Fragment1傳來(lái)的參數(shù)”這幾個(gè)String;
2、當(dāng)用戶點(diǎn)擊第二個(gè)Fragment中的幾個(gè)圖片時(shí),將點(diǎn)中的結(jié)果返回給第一個(gè)Fragment,將用戶的選擇在第一個(gè)Fragment顯示出來(lái)
一、基本架構(gòu)搭建
首先,我們要把整個(gè)架構(gòu)搭起來(lái),然后再進(jìn)行參數(shù)傳遞和回傳
(一)、基本XML構(gòu)建:
根據(jù)上面的效果,大家很容易看到兩個(gè)Fragment的布局:
1、Fragment1的布局:(fragment1.xml)
很簡(jiǎn)單,垂直布局,上面一個(gè)ImageView來(lái)盛裝返回過(guò)來(lái)的圖片結(jié)果,下面一個(gè)Button來(lái)用來(lái)點(diǎn)擊加載第二個(gè)Fragment;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical"> <ImageView android:id="@+id/img_result" android:layout_width="100dp" android:layout_height="100dp" android:scaleType="center"/> <Button android:id="@+id/load_fragment2_btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="加載第二個(gè)Fragment"/> </LinearLayout>
2、Fragment2的布局:(fragment2.xml)
這個(gè)也是垂直布局,上面的一個(gè)TextView用來(lái)盛裝從Fragment1傳過(guò)來(lái)的String參數(shù),下面的幾個(gè)ImageView用來(lái)顯示幾個(gè)供用戶選擇的圖片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is fragment 2" android:textColor="#000000" android:textSize="25sp" /> <ImageView android:id="@+id/img1" android:layout_width="100dip" android:layout_height="100dp" android:scaleType="center" android:src="@drawable/animal1"/> <ImageView android:id="@+id/img2" android:layout_width="100dip" android:layout_height="100dp" android:scaleType="center" android:src="@drawable/animal2"/> <ImageView android:id="@+id/img3" android:layout_width="100dip" android:layout_height="100dp" android:scaleType="center" android:src="@drawable/animal3"/> <ImageView android:id="@+id/img4" android:layout_width="100dip" android:layout_height="100dp" android:scaleType="center" android:src="@drawable/animal4"/> </LinearLayout>
(二)對(duì)應(yīng)的Fragment類
1、在MainActivity初始化時(shí),將Fragment1顯示出來(lái):
MainActivity對(duì)應(yīng)的XML文件:(main_activity.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
對(duì)應(yīng)的代碼:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Fragment1 fragment1 = new Fragment1(); getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit(); } }
2、Fragment1:在用戶點(diǎn)擊時(shí),將fragment2添加到當(dāng)前頁(yè)面顯示出來(lái);
public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1, container, false); Button btn = (Button)view.findViewById(R.id.load_fragment2_btn); btn.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(final View view) { Fragment2 fragment2 = new Fragment2(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.add(R.id.main_layout, fragment2); transaction.addToBackStack(null); transaction.commit(); } }); return view; } }
3、Fragment2:至于目前的它還是很簡(jiǎn)單的,只要能顯示出來(lái) 就好了,所以他的代碼為:
public class Fragment2 extends Fragment implements View.OnClickListener { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment2, container, false); return view; } }
二、Fragment間參數(shù)傳遞
至于Fragment間參數(shù)為什么要用SetArguments來(lái)傳遞,我就不講了,看這篇文章:《Android解惑 - 為什么要用Fragment.setArguments(Bundle bundle)來(lái)傳遞參數(shù)》,我這里只說(shuō)項(xiàng)目中如何使用:
在Fragment2中,新建一個(gè)函數(shù):newInstance(String text)來(lái)接收傳過(guò)來(lái)的參數(shù):
新建一個(gè)Fragment2實(shí)例,然后將參數(shù)通過(guò)SetArguments設(shè)置到其中;
public static Fragment2 newInstance(String text) { Fragment2 fragment = new Fragment2(); Bundle args = new Bundle(); args.putString("param", text); fragment.setArguments(args); return fragment; }
然后在Fragment2的OnCreateView的時(shí)候再?gòu)腶rguments中獲取參數(shù):
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment2, container, false); if (getArguments() != null) { String mParam1 = getArguments().getString("param"); TextView tv = (TextView)view.findViewById(R.id.textview); tv.setText(mParam1); } return view; }
在Fragment1中,在調(diào)起Fragmen2t時(shí),通過(guò)調(diào)用newInstance函數(shù)來(lái)獲取實(shí)例并傳遞參數(shù):
public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1, container, false); Button btn = (Button)view.findViewById(R.id.load_fragment2_btn); btn.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(final View view) { Fragment2 fragment2 = Fragment2.newInstance("從Fragment1傳來(lái)的參數(shù)"); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.add(R.id.main_layout, fragment2); transaction.addToBackStack(null); transaction.commit(); } }); return view; } }
(三)、從Fragment2向Fragment1回傳參數(shù)
這里只有利用回調(diào),有關(guān)回調(diào)傳遞參數(shù)的問(wèn)題,我在前一篇文章中:《詳解Dialog(三)——自定義對(duì)話框視圖及參數(shù)傳遞》第三部分:參數(shù)傳遞;詳細(xì)講過(guò),大家可以先看源碼,如果源碼不懂,可以參考下這篇文章,這里就不再贅述。
相關(guān)文章
Android自定義控件之小說(shuō)書架實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Android自定義控件之小說(shuō)書架示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪方法2023-04-04Flutter?Flow實(shí)現(xiàn)滑動(dòng)顯隱層示例詳解
這篇文章主要為大家介紹了Flutter?Flow實(shí)現(xiàn)滑動(dòng)顯隱層示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Android入門之使用SharedPreference存取信息詳解
這篇文章主要為大家詳細(xì)介紹了Android如何使用SharedPreference實(shí)現(xiàn)存取信息,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下2022-12-12Android Binder進(jìn)程間通信工具AIDL使用示例深入分析
Binder作為Android 眾多的IPC通訊手段之一,在Framework的數(shù)據(jù)傳輸中起到極為關(guān)鍵的作用。Binder機(jī)制可謂是Android 知識(shí)體系里的重中之重,作為偏底層的基礎(chǔ)組件,平時(shí)我們很少關(guān)注它,而它卻是無(wú)處不在,也是Android 面試易考察的點(diǎn)之一2022-11-11Android中Java和JavaScript交互實(shí)例
這篇文章主要介紹了Android中Java和JavaScript交互實(shí)例,本文給出了實(shí)現(xiàn)方法、實(shí)現(xiàn)代碼、js調(diào)用Java、java調(diào)用js等內(nèi)容,需要的朋友可以參考下2015-01-01Android進(jìn)階之Spinner下拉框的使用方法
這篇文章主要為大家詳細(xì)介紹了Android進(jìn)階之Spinner下拉框的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android 網(wǎng)絡(luò)html源碼查看器詳解及實(shí)例
這篇文章主要介紹了Android 網(wǎng)絡(luò)html源碼查看器詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03Android RecyclerView多類型布局卡片解決方案
這篇文章主要介紹了Android RecyclerView多類型布局卡片解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03