Fragment和Fragment之间的数据传输

来源:互联网 发布:徐州专业淘宝摄影 编辑:程序博客网 时间:2024/05/16 00:29

前面我已经实现了Activity之间以及基于Fragment的Activity之间的数据传递,现在需要实现的是由同一Activity托管的两个Fragment,及Fragment one 与 Fragment two之间的数据传输。

如图所示:


上图表达了Fragment之间发送和接收数据用到的具体方法.。

先在Activity中获取Fragment One 和 Fragment Two的实例,然后将Fragment One 的 TargetFragment 设置为 Fragment Two,在Fragment One中调用 getTargetFragment.onActivityResult就可以将数据传递给Fragment Two.  然后Fragment Two在onActivityResult方法中解析数据即可。

然后同样将Fragment Two 的 TargetFragment 设置为 Fragment One,在Fragment Two中调用 getTargetFragment.onActivityResult就可以将数据传递给Fragment Two.  然后Fragment Two在onActivityResult方法中解析数据即可。


用一个Activity 绑定两个Fragment 一个在上 一个在下

实现的效果如下:

图一 为未发送消息时刚刚进去的界面.

图二 为Fragment_one 发送了一个 “哈哈,这是one发的消息” 。 在Fragment Two中显示出来了。

图三 为Fragment_two 发送了一个“收到了,我是,two”。 在Fragment one中显示出来了。



代码如下:


Activity的布局代码如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <FrameLayout        android:id="@+id/fragment_container_one"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        />    <FrameLayout        android:id="@+id/fragment_container_two"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        /></LinearLayout>

Activity的Java代码:

public class MainActivity extends FragmentActivity {public Fragment children_fragment_one;public Fragment children_fragment_two;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_layout);        init();            }private void init() {// TODO Auto-generated method stubFragmentManager fm = getSupportFragmentManager();children_fragment_one = new Children_Fragment_one();children_fragment_two =  new Children_Fragment_two();//将Fragment two设置成Fragment one的目标 Fragment 然后Fragment two就可以接收Fragment one发送的消息了children_fragment_one.setTargetFragment(children_fragment_two, Children_Fragment_one.requestCode);//将Fragment one设置成Fragment two的目标 Fragment 然后Fragment one就可以接收Fragment two发送的消息了children_fragment_two.setTargetFragment(children_fragment_one, Children_Fragment_two.requestCode);//这样Fragment one和 Fragment two就能够实现互相通信了        fm.beginTransaction().add(R.id.fragment_container_one, children_fragment_one).commit();        fm.beginTransaction().add(R.id.fragment_container_two,children_fragment_two).commit();}}


定义了一个存储数据的类:

public class MyData implements Serializable{public String content;public String getContent() {return content;}public void setContent(String content) {this.content = content;}}


Fragment_one的布局代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:background="#CDCD00"     >        <TextView        android:id="@+id/tv_show_content"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:textColor="#008B00"        android:textSize="20sp"        android:gravity="center_horizontal"        android:text="显示Fragment_two发来的信息"        />             <EditText        android:id="@+id/edit_send_content"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:textSize="20sp"        android:textColor="#000000"        android:background="#FFFFFF"        android:hint="请输入要发送的内容"        android:layout_margin="20dp"        />    <Button        android:id="@+id/bt_send_message"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#008B00"        android:textSize="20sp"        android:textColor="#CDCD00"        android:text="发送消息给Fragment_two"        android:layout_margin="20dp"         />   </LinearLayout>

Fragment_one的Java代码:

public class Children_Fragment_one extends Fragment{/* * 用于实现Fragment通信的例子 * 定义了一个TextView用来显示Fragment two发过来的信息 * 定义了一个EditText用力啊输入发送给Fragment two的内容 * 定义了一个Button用来触发发送消息的事件 * 定义了一个MyData 序列化对象用来存储数据 * */public TextView tv_show_content;public EditText edit_send_content;public Button bt_send_message;public static int requestCode = 0;public static String key = "Children_Fragment_one.key";@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {// TODO Auto-generated method stubView view = inflater.inflate(R.layout.fragment_layout_one, container,false);tv_show_content = (TextView) view.findViewById(R.id.tv_show_content);edit_send_content = (EditText) view.findViewById(R.id.edit_send_content);bt_send_message = (Button) view.findViewById(R.id.bt_send_message);bt_send_message.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMyData data = new MyData();data.setContent(edit_send_content.getText().toString());Intent intent = new Intent();intent.putExtra(key, data);//用于发送消息给Fragment twogetTargetFragment().onActivityResult(requestCode, Activity.RESULT_OK, intent);edit_send_content.setText("");}});return view;}//该方法用来接收 Fragment two发送过来的消息@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubif( resultCode != Activity.RESULT_OK)return ;if( requestCode == Children_Fragment_two.requestCode){MyData mydata = (MyData) data.getSerializableExtra(Children_Fragment_two.key);tv_show_content.setText(mydata.getContent());}}}


Fragment_two的布局代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:background="#008B00"     >        <TextView        android:id="@+id/tv_show_content"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:textColor="#CDCD00"        android:textSize="20sp"        android:gravity="center_horizontal"        android:text="显示Fragment_one发来的信息"        />             <EditText        android:id="@+id/edit_send_content"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:textSize="20sp"        android:textColor="#000000"        android:background="#FFFFFF"        android:hint="请输入要发送的内容"        android:layout_margin="20dp"        />    <Button        android:id="@+id/bt_send_message"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#CDCD00"        android:textSize="20sp"        android:textColor="#008B00"        android:text="发送消息给Fragment_one"        android:layout_margin="20dp"         />   </LinearLayout>


Fragment_two的java代码:


public class Children_Fragment_two extends Fragment{/* * 用于实现Fragment通信的例子 * 定义了一个TextView用来显示Fragment one发过来的信息 * 定义了一个EditText用力啊输入发送给Fragment one的内容 * 定义了一个Button用来触发发送消息的事件 * 定义了一个MyData 序列化对象用来存储数据 * */public TextView tv_show_content;public EditText edit_send_content;public Button bt_send_message;public static int requestCode = 1;public static String key = "Children_Fragment_two.key";@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {// TODO Auto-generated method stubView view = inflater.inflate(R.layout.fragment_layout_two, container,false);tv_show_content = (TextView) view.findViewById(R.id.tv_show_content);edit_send_content = (EditText) view.findViewById(R.id.edit_send_content);bt_send_message = (Button) view.findViewById(R.id.bt_send_message);bt_send_message.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMyData data = new MyData();data.setContent(edit_send_content.getText().toString());Intent intent = new Intent();intent.putExtra(key, data);//用于发送消息给Fragment onegetTargetFragment().onActivityResult(requestCode, Activity.RESULT_OK, intent);edit_send_content.setText("");}});return view;}//该方法用来接收 Fragment one发送过来的消息@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubif( resultCode != Activity.RESULT_OK)return ;if( requestCode == Children_Fragment_one.requestCode){MyData mydata = (MyData) data.getSerializableExtra(Children_Fragment_one.key);tv_show_content.setText(mydata.getContent());}}}


需要源代码的朋友请联系QQ:657415669


0 0
原创粉丝点击