1. 自定义两个fragment的布局和java类。
  2. 在mainactivity中引用布局文件
  3. 在其中的一个fragment中的控件上添加监听,获取到另一个fragment中控件的内容,展示出来完成fragment之间的通信。

     

    上代码:

<?xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
>

<
TextView
android:id="@+id/tv_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是fragment1的数据"
android:textColor="#000000"
android:textSize="25sp"
/>

</
LinearLayout>

 

package com.cm.communicationbetweenfragments;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* Created by Administrator on 2016/1/2.
*/
public class Fragment1 extends Fragment {


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


return inflater.inflate(R.layout.fragment1,container,false);
}
}

 

 

<?xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#FFFE00"
>

<
TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是fragment2的数据"
android:textSize="25sp"
android:textColor="#000000"
/>

<
Button
android:id="@+id/btn_fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击我可以获取fragment1的数据哦"
android:textColor="#000000"
android:textSize="20sp"

/>

</
LinearLayout>

 

 

package com.cm.communicationbetweenfragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

/**
* Created by Administrator on 2016/1/2.
*/
public class Fragment2 extends Fragment1 {

Button
btn;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment2,container,false);
}

@Override
public void onStart() {
super.onStart();
btn=(Button)getActivity().findViewById(R.id.btn_fragment2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv_fragment1=(TextView)getActivity().findViewById(R.id.
tv_fragment1);
btn.setText(tv_fragment1.getText());
//Toast.makeText(getActivity(),"获取fragment1的数据",Toast.LENGTH_SHORT).show();
}
});
}
}

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity"
>

<
fragment
android:name="com.cm.communicationbetweenfragments.Fragment1"
android:id="@+id/fragment1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
<
fragment
android:name="com.cm.communicationbetweenfragments.Fragment2"
android:id="@+id/fragment2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
</
LinearLayout>

 

点击之前:

 

点击之后:

 

 

 

从而实现了两个fragment之间的通信。