MainActivity:


  1. public class MainActivity extends Activity {  
  2.     private FragmentManager manager;  
  3.     private FragmentTransaction transaction;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.   
  10.         manager = getFragmentManager();  
  11.         transaction = manager.beginTransaction();  
  12.   
  13.         MyFragment1 fragment1 = new MyFragment1();  
  14.         Bundle bundle1 = new Bundle();  
  15.         bundle1.putString("id""Activity发送给MyFragment1的数据");  
  16.         fragment1.setArguments(bundle1);  
  17.         transaction.replace(R.id.left, fragment1, "left");  
  18.   
  19.         MyFragment2 fragment2 = new MyFragment2();  
  20.         Bundle bundle2 = new Bundle();  
  21.         bundle2.putString("id""Activity发送给MyFragment2的数据");  
  22.         fragment2.setArguments(bundle2);  
  23.         transaction.replace(R.id.right, fragment2, "right");  
  24.   
  25.         transaction.commit();  
  26.     }  
  27.   



MyFragment1:

  1. public class MyFragment1 extends Fragment {  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.     }  
  6.   
  7.     @Override  
  8.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  9.             Bundle savedInstanceState) {  
  10.         View view = inflater.inflate(R.layout.f1, null);  
  11.           
  12.         TextView textView = (TextView) view.findViewById(R.id.textView);  
  13.         Bundle bundle1 = getArguments();  
  14.         textView.setText(bundle1.getString("id"));  
  15.         return view;  
  16.     }  
  17.   
  18. }  

MyFragment2

  1. public class MyFragment2 extends Fragment {  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.     }  
  6.   
  7.     @Override  
  8.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  9.             Bundle savedInstanceState) {  
  10.         View view = inflater.inflate(R.layout.f2, null);  
  11.           
  12.         TextView textView = (TextView) view.findViewById(R.id.textView);  
  13.         Bundle bundle2 = getArguments();  
  14.         textView.setText(bundle2.getString("id"));  
  15.         return view;  
  16.     }  
  17.   
  18. }  




更多相关文章

  1. 一句话锁定MySQL数据占用元凶
  2. Android(安卓)自动向手机添加联系人(测试数据用)
  3. Android开发之环形进度条(安卓默认…
  4. android 原生GET和POST提交数据
  5. android sensors HAL范例分析
  6. Android学习笔记(21)————利用JDBC连接服务器数据库
  7. Mediacodec
  8. SharedPreferences保存少量的数据
  9. android项目中单实例数据库类

随机推荐

  1. mysql滑动聚合/年初至今聚合原理与用法实
  2. mysql累积聚合原理与用法实例分析
  3. mysql连续聚合原理与用法实例分析
  4. 解决Win10系统安装MySQL8.0遇到的问题
  5. mysql滑动订单问题原理与解决方法实例分
  6. MySQL流程函数常见用法实例分析
  7. mysql存储过程之case语句用法实例详解
  8. mysql存储过程之游标(DECLARE)原理与用法详
  9. mysql存储过程之if语句用法实例详解
  10. MySQL多表连接的入门实例教程