Fragment与Fragment之间的数据通信

转载 2015年07月10日 00:05:48

Fragment与Fragment之间的数据通(FragmentA中向FragmentB发送数据)利用接口回调的方法

在FragmentA中的步骤

1.在FragmentA中定义一个内部接口

publicinterface OnPassValueSelectedListener{

                   public void putData(Stringvalue);

         }

2.在Fragment中定义接口对象

privateOnPassValueSelectedListener mListener;

3.在onAttach方法中将activity强制转换成接口对象

mListener=(OnPassValueSelectedListener) activity;

4.将数据设置到接口方法的参数中

mListener.putData(data);

在Activity中操作步骤:

1.实现FragmentA中的接口,并实现该接口中的方法

2.在该方法中封装数据并通过Bundle将数据发送FragmentB中

Bundlebundle=new Bundle();

                  bundle.putString("detail",value);

                  FragmentBfragmentB=new FragmentB();

                   fragmentB.setArguments(bundle);

                   FragmentManagermanager=getFragmentManager();

                   FragmentTransactionfragmentTransaction=manager.beginTransaction();

                   fragmentTransaction.replace(R.id.ll,fragmentB);

                   fragmentTransaction.addToBackStack(null);

                   fragmentTransaction.commit();


在FragmentB中的操作

1.接收数据

Bundlebundle =getArguments();

                   data=bundle.getString("detail");

=================================================================

代码详情:

public classFragmentA extends Fragment {

         private static final StringTAG="FragmentA";

         privateOnPassValueSelectedListener mListener;

         private ListView lv;

         private int width=0;

        

         //定义一个接口,并定义一个回调方法

         public interfaceOnPassValueSelectedListener{

                   public voidputData(String value);

         }

                  

         @Override

         public void onAttach(Activity activity){

                   // TODO Auto-generated methodstub

                   super.onAttach(activity);

                   Log.i(TAG,"-------onAttach-------->>");

                   try {//多态

                            mListener =(OnPassValueSelectedListener) activity;

                   } catch (Exception e) {

                            throw newClassCastException(activity.toString() + " must implementOnPassValueSelectedListener");

                   }

         }

        

         @SuppressLint("NewApi")

         @Override

         public void onCreate(BundlesavedInstanceState) {

                   // TODO Auto-generated methodstub

          //获得屏幕宽高

                   Pointpoint=new Point();

                   getActivity().getWindowManager().getDefaultDisplay().getSize(point);

                   width=point.x;

                   int height=point.y;

                   getActivity().setTitle(width+ ":" + height);

                   super.onCreate(savedInstanceState);

                   Log.i(TAG,"-------onCreate-------->>");

         }

        

         @Override

         public View onCreateView(LayoutInflaterinflater, ViewGroup container,

                            BundlesavedInstanceState) {

                   Log.i(TAG,"-------onCreateView-------->>");

                   View view=inflater.inflate(R.layout.fragment_a, container, false);

                   lv=(ListView)view.findViewById(R.id.listView1);

                   String[]strings=getResources().getStringArray(R.array.move);

                   ArrayAdapter<String>adapter=newArrayAdapter<String>(getActivity(),android.R.layout.simple_expandable_list_item_1,strings);

                   lv.setAdapter(adapter);

                   lv.setOnItemClickListener(newOnItemClickListener() {

 

                            @Override

                            public voidonItemClick(AdapterView<?> parent, View view,

                                               intposition, long id) {

                                     // TODOAuto-generated method stub

                                     InputStreamis;

                                     ByteArrayOutputStreambos=new ByteArrayOutputStream();

                                     Stringdata="";

                                     try {

                                               is=getActivity().getResources().getAssets().open("movie"+position+".txt");

                                               byte[]buffer=newbyte[1024];

                                               intlen=-1;

                                               while((len=is.read(buffer))!=-1) {

                                                        bos.write(buffer,0, len);

                                                        bos.flush();

                                               }

                                               data=newString(bos.toByteArray(), "utf-8");

                                     } catch(IOException e) {

                                               //TODO Auto-generated catch block

                                               e.printStackTrace();

                                     }

                                     if(width>=200) {

                                               mListener.putData(data);

                                     }else {

                                               Intentintent=new Intent(getActivity(), ContentActivity.class);

                                               Bundlebundle=new Bundle();

                                               intent.putExtra("content",data);

                                               startActivity(intent);

                                              

                                     }

                                              

                                    

                                    

                            }

                   });

                   return view;

         }       

        

        

}

1.       在mainActivity中实现在FragmentA中定义的接口,并且封装数据:

packagecom.jason.android12_betweenfragment;

 

importcom.jason.android12_betweenfragment.FragmentA.OnPassValueSelectedListener;

 

importandroid.os.Bundle;

importandroid.app.Activity;

importandroid.app.FragmentManager;

importandroid.app.FragmentTransaction;

importandroid.view.Menu;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

 

public classMainActivity extends Activity implements OnPassValueSelectedListener{

         @Override

         protected void onCreate(BundlesavedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity_main);

                  

 

         }

        

         @Override

        

         public void putData(String value) {

          //封装数据

                   Bundle bundle=newBundle();

                   bundle.putString("detail",value);

                  

                  FragmentBfragmentB=new FragmentB();

          //传递数据

                   fragmentB.setArguments(bundle);

                   FragmentManagermanager=getFragmentManager();

                   FragmentTransactionfragmentTransaction=manager.beginTransaction();

                   fragmentTransaction.replace(R.id.ll,fragmentB);

                   fragmentTransaction.addToBackStack(null);

                   fragmentTransaction.commit();

                  

                  

                  

         }

}

 

2.       在FragmentB中接受数据:

packagecom.jason.android12_betweenfragment;

 

importandroid.app.Activity;

importandroid.app.Fragment;

importandroid.os.Bundle;

import android.util.Log;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.view.ViewGroup;

importandroid.widget.Button;

importandroid.widget.TextView;

 

public classFragmentB extends Fragment {

         private static final StringTAG="FragmentB";

         private String data="";

         public FragmentB() {

                   // TODO Auto-generatedconstructor stub

         }

                  

         @Override

         public void onAttach(Activity activity){

                   // TODO Auto-generated methodstub

                   super.onAttach(activity);

                   Log.i(TAG,"-------onAttach-------->>");

         }

        

         @Override

         public void onCreate(BundlesavedInstanceState) {

                   // TODO Auto-generated methodstub

                   super.onCreate(savedInstanceState);

                   Log.i(TAG,"-------onCreate-------->>");

                   Bundlebundle =getArguments();

                   data=bundle.getString("detail");

         }

}


Fragment与Fragment之间的数据通信

Fragment与Fragment之间的数据通(FragmentA中向FragmentB发送数据)利用接口回调的方法 在FragmentA中的步骤 1.在FragmentA中定义一个内部接口 p...

Android :(使用fragment碎片)——让你的应用更灵活(二)数据通信

Fragment的三种通信fragment之间的通信包含了fragment之间的通信,Activity——>frament的通信以及fragment—>Activity的数据通信。这三种通信中第三个比...

Fragment之间通过Activity通信

  • 2014年09月17日 12:56
  • 2.39MB
  • 下载

android 客户端与服务器端之间采用JSON进行数据通信之一

Android 客户端与服务器端之间采用JSON进行数据通信                                     之 客户端获取服务器端数据 最近由于公司项目需要,需要将之前手...

Fragment与Activity之间通信实例

  • 2014年08月06日 13:04
  • 1.6MB
  • 下载

fragment和activity之间通信

  • 2015年12月03日 11:02
  • 2.99MB
  • 下载

在Android系统使用socket在Java层和native之间数据通信

http://blog.sina.com.cn/s/blog_82f2fc280101395m.html   在Android系统中,java代码和本地代码(c/c++)直接的通信一般是使用jni...

Fragment之间通信实例

  • 2016年06月10日 12:08
  • 22.85MB
  • 下载

Android两个Fragment之间通信Demo

  • 2016年04月09日 10:34
  • 1.91MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Fragment与Fragment之间的数据通信
举报原因:
原因补充:

(最多只允许输入30个字)