Fragment学习之Activity传递数据到Fragment

来源:互联网 发布:中程导弹知乎 编辑:程序博客网 时间:2024/05/14 01:07

上一篇文章我们理解怎样使用接口回调的方式将数据从Fragment传递到Activity中,这里我们将探讨如何将数据从Activity传到Fragment中。

思路:把数据先保存到Bundle中,然后在调用setArguments()方法进行传递。


MainActivity.java代码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class MainActivity extends FragmentActivity {  
  2.   
  3.     private FragmentManager manager;  
  4.     private FragmentTransaction transaction;  
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.   
  11.         /* 获取manager */  
  12.         manager = this.getSupportFragmentManager();  
  13.         /* 创建事物 */  
  14.         transaction = manager.beginTransaction();  
  15.   
  16.         /* 创建LeftFragment*/  
  17.         LeftFragment leftFragment = new LeftFragment();  
  18.         /*创建一个Bundle用来存储数据,传递到Fragment中*/  
  19.         Bundle bundle = new Bundle();  
  20.         /*往bundle中添加数据*/  
  21.         bundle.putString("name""廖泽民");  
  22.         /*把数据设置到Fragment中*/  
  23.         leftFragment.setArguments(bundle);  
  24.         /* 把Fragment添加到对应的位置 */  
  25.         transaction.add(R.id.left, leftFragment, "left");  
  26.         /* 提交事物 */  
  27.         transaction.commit();  
  28.   
  29.   
  30.     }  
  31.   
  32. }  

LeftFragment.java代码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class LeftFragment extends Fragment {  
  2.   
  3.   
  4.     public LeftFragment() {  
  5.         // TODO Auto-generated constructor stub  
  6.     }  
  7.   
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         // TODO Auto-generated method stub  
  11.         super.onCreate(savedInstanceState);  
  12.     }  
  13.       
  14.     @Override  
  15.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  16.   
  17.         /*动态加载布局*/  
  18.         View view = inflater.inflate(R.layout.left, null);  
  19.         /*从动态布局中获取控件*/  
  20.         TextView txtView = (TextView) view.findViewById(R.id.txt);  
  21.         /*通过getArgments()方法获取从Activity传过来的值*/  
  22.         Bundle bundle = this.getArguments();  
  23.         /*为TextView设置值*/  
  24.         txtView.setText(bundle.getString("name"));  
  25.           
  26.         return view;  
  27.     }  
  28.       
  29.     @Override  
  30.     public void onPause() {  
  31.         // TODO Auto-generated method stub  
  32.         super.onPause();  
  33.     }  
  34.       
  35. }  

activity_main.xml:

[html] view plaincopy在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.     <LinearLayout  
  9.         android:id="@+id/left"  
  10.         android:layout_width="224dp"  
  11.         android:layout_height="match_parent"  
  12.         android:background="#CCCCCC"  
  13.         android:orientation="vertical" >  
  14.     </LinearLayout>  
  15.   
  16.     <LinearLayout  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="match_parent"  
  19.         android:orientation="vertical" >  
  20.   
  21.     </LinearLayout>  
  22.   
  23. </LinearLayout>  

left.xml:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.   
  8.     <TextView   
  9.         android:id="@+id/txt"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent"/>  
  12. </LinearLayout>  

demo结果展示:

0 0
原创粉丝点击