android Fragment与Activity交互,互相发数据(附图详解)

来源:互联网 发布:巨鹿之战 知乎 编辑:程序博客网 时间:2024/05/14 15:34

笔者近期看官方training,发现了很多有用又好玩的知识。其中,fragment与Activity通信就是一个。

fragment与Activity通信主要是两点:

1、fragment传递信息给Activity

此点是通过在fragment中定义接口与Activity共享数据。

2、Activity传递信息给fragment

此点主要是通过fragment的getArgument()和setArgument()两个函数传递bundle来传递。


效果:(最后附上源码)



主要流程:

1、在MainActivity中设置一个fragment容器,在MainActivity初始化的时候替换成OneFragment。


2、OneFragment中由于定义了接口,在MainActivity中实现了此接口,当点击button的时候会实现此接口中的函数,并且和Activity共享此数据。(也就是EditText中的内容)


3、MainActivity中获取到数据后,创建一个新的TwoFragment,并且使用Fragment的setArgument()方法把获取到的数据传递到TwoFragment,TwoFragment使用getArgument()接收。


代码:



MainActivity:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.example.trainingfragment;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Bundle;  
  7.   
  8. public class MainActivity extends Activity  
  9.         implements OneFragment.OnOneFragmentClickListener{  
  10.   
  11.     private FragmentManager fm;  
  12.     private FragmentTransaction ft;  
  13.   
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         fm=getFragmentManager();  
  20.         ft=fm.beginTransaction();  
  21.         ft.replace(R.id.fragment_container,new OneFragment());  
  22.         //使用FragmentTransaction一定不要忘记提交  
  23.         ft.commit();  
  24.     }  
  25.   
  26.     @Override  
  27.     public void onArticleClick(String text) {  
  28.         //此处定义一个新的TwoFragment  
  29.         //并且把在OneFragment获取到的text传入TwoFragment  
  30.         TwoFragment newFragment = new TwoFragment();  
  31.         Bundle args=new Bundle();  
  32.         args.putString("text", text);  
  33.         newFragment.setArguments(args);  
  34.   
  35.         //由于之前的一个FragmentTransaction已经提交过,所以此处需要重新定义  
  36.         ft=fm.beginTransaction();  
  37.         ft.replace(R.id.fragment_container, newFragment);  
  38.         //使用FragmentTransaction一定不要忘记提交  
  39.         ft.commit();  
  40.   
  41.     }  
  42. }  

OneFragment:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.example.trainingfragment;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Fragment;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11.   
  12. /** 
  13.  * 项目名称:TrainingFragment 
  14.  * 类描述: 
  15.  * 创建人:佳佳 
  16.  * 创建时间:2016/3/25 17:57 
  17.  * 修改人:佳佳 
  18.  * 修改时间:2016/3/25 17:57 
  19.  * 修改备注: 
  20.  */  
  21. public class OneFragment extends Fragment {  
  22.   
  23.     OnOneFragmentClickListener mCallback;  
  24.     private EditText et;  
  25.     private Button btn;  
  26.   
  27.     //此处定义接口  
  28.     public interface OnOneFragmentClickListener {  
  29.         //在接口中定义函数  
  30.         void onArticleClick(String text);  
  31.     }  
  32.   
  33.     @Override  
  34.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  35.         View view=inflater.inflate(R.layout.fragment_one,container,false);  
  36.         et=(EditText)view.findViewById(R.id.et_one);  
  37.         btn=(Button)view.findViewById(R.id.btn_one);  
  38.         btn.setOnClickListener(new View.OnClickListener() {  
  39.             @Override  
  40.             public void onClick(View v) {  
  41.                 mCallback.onArticleClick(et.getText().toString());  
  42.             }  
  43.         });  
  44.   
  45.         return view;  
  46.     }  
  47.   
  48.     @Override  
  49.     public void onAttach(Activity activity) {  
  50.         super.onAttach(activity);  
  51.         try {  
  52.             mCallback = (OnOneFragmentClickListener) activity;  
  53.         } catch (ClassCastException e) {  
  54.             throw new ClassCastException(activity.toString()  
  55.                     + " must implement OnHeadlineSelectedListener");  
  56.         }  
  57.     }  
  58. }  

TwoFragment:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.example.trainingfragment;  
  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. /** 
  11.  * 项目名称:TrainingFragment 
  12.  * 类描述: 
  13.  * 创建人:佳佳 
  14.  * 创建时间:2016/3/25 17:57 
  15.  * 修改人:佳佳 
  16.  * 修改时间:2016/3/25 17:57 
  17.  * 修改备注: 
  18.  */  
  19. public class TwoFragment extends Fragment {  
  20.   
  21.   
  22.     @Override  
  23.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  24.   
  25.         return inflater.inflate(R.layout.fragment_two, container, false);  
  26.     }  
  27.   
  28.     @Override  
  29.     public void onStart() {  
  30.         super.onStart();  
  31.         //获取Activity传递过来的数据并且显示到  
  32.         Bundle args=getArguments();  
  33.         setMyText(args.getString("text"));  
  34.     }  
  35.   
  36.     public void setMyText(String text) {  
  37.         TextView tv=(TextView)getActivity().findViewById(R.id.tv_two);  
  38.         tv.setText(text);  
  39.     }  
  40. }  

activity_main:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/fragment_container"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" />  

fragment_one:

[html] view plain copy
 在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.     android:padding="10dp">  
  7.   
  8.     <EditText  
  9.         android:id="@+id/et_one"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:hint="请输入要传递的内容" />  
  13.   
  14.     <Button  
  15.         android:id="@+id/btn_one"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="跳转并传递"  
  19.         android:textSize="20sp" />  
  20. </LinearLayout>  

fragment_two:

[html] view plain copy
 在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.     android:padding="10dp">  
  7.   
  8.     <EditText  
  9.         android:id="@+id/et_one"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:hint="请输入要传递的内容" />  
  13.   
  14.     <Button  
  15.         android:id="@+id/btn_one"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="跳转并传递"  
  19.         android:textSize="20sp" />  
  20. </LinearLayout>  


源码地址:http://download.csdn.net/detail/double2hao/9472978

0 0
原创粉丝点击