目录

Activity与Fragment通信,Fragment之间互相通信

Activity与Fragment通信,Fragment之间互相通信

效果显现:

./1.gif

实现步骤:

1.创建MainActivity,FragmentA,FragmnetB在activity_main.xml中静态加载FragmentA,FragmnetB 2.在MainActivity中获取FragmentA对象,再用对象,调用FragmentA中的方法,实现Activity与Fragment通信之间的通信 3.在FragmentA中获取MainActivity的对象,在通过MainActivity的对象获取FragmnetB对象,最后调用FragmnetB中方法,实现Fragment之间互相通信。

代码实现:

1.创建FragmentA,FragmnetB和相应的布局,这里我就不多解释了:
1.a_fragment.xml:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d41313"
    android:orientation="vertical">

    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:background="#000"
    android:textColor="#fff"
    android:text=" this is a fragent"
    android:gravity="center"/>

    <TextView
        android:id="@+id/a_frag_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="show text"
        android:gravity="center"/>

    <EditText
        android:id="@+id/a_frag_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/a_frag_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="send to FragmentB"/>

</LinearLayout>
2.FragmentA:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class FragmentA extends Fragment {

    private TextView showTv;
    private EditText toBEt;
    private Button toBBtn;

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

        View view = inflater.inflate(R.layout.a_fragment, container, false);
        bindID(view);
        return view;

    }

    private void bindID(View view) {

        showTv = view.findViewById(R.id.a_frag_tv);
        toBEt = view.findViewById(R.id.a_frag_et);
        toBBtn = view.findViewById(R.id.a_frag_btn);

    }


}
3.b_fragment.xml:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#3dd526"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="this is b fragment"
        android:textColor="#fff"
        android:background="#000"
        android:textSize="25sp"/>

    <TextView
        android:id="@+id/b_frag_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="show text"
        android:textSize="25sp"/>

</LinearLayout>
4.FragmentB:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class FragmentB extends Fragment {

    private TextView showTv;

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

        View view = inflater.inflate(R.layout.b_fragment,container,false);
        showTv = view.findViewById(R.id.b_frag_tv);
        return view;
    }
}
2.在activity_main.xml中加载之前的两个碎片:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.communicationfragment.MainActivity">

   <LinearLayout
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:layout_weight="1">

       <TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:textSize="25sp"
           android:background="#000"
           android:textColor="#fff"
           android:text=" this is Activity"
           android:gravity="center"/>

       <EditText
           android:id="@+id/main_to_a_et"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" />

       <Button
           android:id="@+id/main_to_a_btn"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:textAllCaps="false"
           android:text="send to FragmentA"/>

   </LinearLayout>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <fragment
            android:id="@+id/fragment_a"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.example.communicationfragment.FragmentA"/>
    </FrameLayout>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <fragment
            android:id="@+id/fragment_b"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.example.communicationfragment.FragmentB"/>
    </FrameLayout>

</LinearLayout>

3.好了,做了这么多的准备工作,现在可以实现功能了,首先实现Activity与Fragment通信,修改FragmentA中的代码,添加一个方法:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class FragmentA extends Fragment {

  ...........

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

    ...............
    private void bindID(View view) {

      ............

    }
    //创建公共地自定义方法,以便被调用
    public void reFresh(String content) {
        //通过传过来的参数,改变控件值
        showTv.setText(content);

    }
}

4.修改MainActivity中的代码,就可以实现Activity与Fragmnet之间的通讯了:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class MainActivity extends AppCompatActivity {

    ..........


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindID();
        //对发送按钮进行监听
        toABtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入内容
                String content = toAEt.getText().toString();
                //通过getSupportFragmentManager().findFragmentById()获取FragmentA对象
                FragmentA fragmentA = (FragmentA) getSupportFragmentManager().findFragmentById(R.id.fragment_a);
                //调用reFresh()方法
                fragmentA.reFresh(content);

            }
        });

    }

    private void bindID() {
        ..........
    }

}

5.下面我们来实现Fragment之间互相通信,在FragmentB中条件语句:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class FragmentB extends Fragment {

    private TextView showTv;

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

    public void reFresh(String content){
        showTv.setText(content);
    }
}

6.最后在FragmentA中添加语句:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class FragmentA extends Fragment {

    private TextView showTv;
    private EditText toBEt;
    private Button toBBtn;

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

     ..............
        toBBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = toBEt.getText().toString();
                //获取当前activity的对象
                MainActivity mainActivity = (MainActivity) getActivity();
                //通过activity的对象调用getSupportFragmentManager().findFragmentById()方法获取fragmentB对象
                FragmentB fragmentB = (FragmentB) mainActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_b);
                fragmentB.reFresh(content);

            }
        });
        return view;

    }

    private void bindID(View view) {

      ..........

    }

    public void reFresh(String content) {

       .........

    }
}