Android Fragment與Activity之間的資料交換(Fragment從Activity擷取資料)

來源:互聯網
上載者:User

Fragment與Activity之間的資料交換,大體上包括三種:

一、Fragment從Activity擷取資料(本文章只介紹第一種);

二、Activity從Fragment擷取資料;

三、Fragment之間擷取資料。


實現:

從Activity傳遞資料到兩個Fragment中,Fragment擷取資料後,展示出來。


原始碼:


布局檔案:

activity_main:

                   

MyFragment1的布局檔案f1:


    

MyFragment2的布局檔案f2:

    

代碼檔案:

MainActivity:

package com.fragmentdemo5_commute;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;/** * 一、Fragment從Activity擷取資料。 */public class MainActivity extends Activity {private FragmentManager manager;private FragmentTransaction transaction;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);manager = getFragmentManager();transaction = manager.beginTransaction();MyFragment1 fragment1 = new MyFragment1();Bundle bundle1 = new Bundle();bundle1.putString("id", "Activity發送給MyFragment1的資料");fragment1.setArguments(bundle1);transaction.replace(R.id.left, fragment1, "left");MyFragment2 fragment2 = new MyFragment2();Bundle bundle2 = new Bundle();bundle2.putString("id", "Activity發送給MyFragment2的資料");fragment2.setArguments(bundle2);transaction.replace(R.id.right, fragment2, "right");transaction.commit();}}

MyFragment1:

package com.fragmentdemo5_commute;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class MyFragment1 extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.f1, null);TextView textView = (TextView) view.findViewById(R.id.textView);Bundle bundle1 = getArguments();textView.setText(bundle1.getString("id"));return view;}}

MyFragment2:


package com.fragmentdemo5_commute;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class MyFragment2 extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.f2, null);TextView textView = (TextView) view.findViewById(R.id.textView);Bundle bundle2 = getArguments();textView.setText(bundle2.getString("id"));return view;}}

原始碼下載:

點擊下載源碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.