Android Fragment与Activity之间的数据交换(Fragment从Activity获取数据)

Fragment与Activity之间的数据交换,大体上包括三种:

一、Fragment从Activity获取数据(本文章只介绍第一种);

二、Activity从Fragment获取数据;

三、Fragment之间获取数据。


实现效果图:

从Activity传递数据到两个Fragment中,Fragment获取数据后,展示出来。


源代码:


布局文件:

activity_main:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity"   
  6.     android:orientation="horizontal">  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/left"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="match_parent"  
  12.         android:orientation="vertical"  
  13.         android:layout_weight="2"   
  14.         android:background="#CCCCCC">  
  15.     </LinearLayout>  
  16.   
  17.     <LinearLayout  
  18.         android:id="@+id/right"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="match_parent"  
  21.         android:orientation="vertical"   
  22.         android:layout_weight="2">  
  23.     </LinearLayout>  
  24.      
  25. </LinearLayout>  

MyFragment1的布局文件f1:


[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/textView"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content" />  
  12.   
  13. </LinearLayout>  

MyFragment2的布局文件f2:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/textView"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content" />  
  12.   
  13. </LinearLayout>  

代码文件:

MainActivity:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.fragmentdemo5_commute;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Bundle;  
  7.   
  8. /** 
  9.  * 一、Fragment从Activity获取数据。 
  10.  */  
  11. public class MainActivity extends Activity {  
  12.     private FragmentManager manager;  
  13.     private FragmentTransaction transaction;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.   
  20.         manager = getFragmentManager();  
  21.         transaction = manager.beginTransaction();  
  22.   
  23.         MyFragment1 fragment1 = new MyFragment1();  
  24.         Bundle bundle1 = new Bundle();  
  25.         bundle1.putString("id""Activity发送给MyFragment1的数据");  
  26.         fragment1.setArguments(bundle1);  
  27.         transaction.replace(R.id.left, fragment1, "left");  
  28.   
  29.         MyFragment2 fragment2 = new MyFragment2();  
  30.         Bundle bundle2 = new Bundle();  
  31.         bundle2.putString("id""Activity发送给MyFragment2的数据");  
  32.         fragment2.setArguments(bundle2);  
  33.         transaction.replace(R.id.right, fragment2, "right");  
  34.   
  35.         transaction.commit();  
  36.     }  
  37.   
  38. }  

MyFragment1:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.fragmentdemo5_commute;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.TextView;  
  9.   
  10. public class MyFragment1 extends Fragment {  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.     }  
  15.   
  16.     @Override  
  17.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  18.             Bundle savedInstanceState) {  
  19.         View view = inflater.inflate(R.layout.f1, null);  
  20.           
  21.         TextView textView = (TextView) view.findViewById(R.id.textView);  
  22.         Bundle bundle1 = getArguments();  
  23.         textView.setText(bundle1.getString("id"));  
  24.         return view;  
  25.     }  
  26.   
  27. }  

MyFragment2:


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.fragmentdemo5_commute;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.TextView;  
  9.   
  10. public class MyFragment2 extends Fragment {  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.     }  
  15.   
  16.     @Override  
  17.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  18.             Bundle savedInstanceState) {  
  19.         View view = inflater.inflate(R.layout.f2, null);  
  20.           
  21.         TextView textView = (TextView) view.findViewById(R.id.textView);  
  22.         Bundle bundle2 = getArguments();  
  23.         textView.setText(bundle2.getString("id"));  
  24.         return view;  
  25.     }  
  26.   
  27. }  
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值