Fragment之间进行通信

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_42046338/article/details/81092374

activity_main.xml作为主Activity的布局文件,在里面加入两个Fragment的引用,使用android:name前缀来引用具体的Fragment:

<?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"
    tools:context=".MainActivity">

    <fragment

        android:id="@+id/fragment1"

        android:name="com.example.myapplication5.BlankFragment"

        android:layout_width="0dip"

        android:layout_height="match_parent"

        android:layout_weight="1" />



    <fragment

        android:id="@+id/fragment2"

        android:name="com.example.myapplication5.BlankFragment2"

        android:layout_width="0dip"

        android:layout_height="match_parent"

        android:layout_weight="1" />
</LinearLayout>

 最后打开或新建MainActivity作为程序的主Activity,里面的代码非常简单,都是自动生成的:

public class MainActivity extends AppCompatActivity {

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

接着把上边XML中的Fragment创建一下  并在里面写一个Textview可以让我们进行访问

Fragment1 代码

<?xml version="1.0" encoding="utf-8"?>
<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=".BlankFragment">


    <TextView

        android:id="@+id/fragment1_text"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="This is fragment 1"

        android:textColor="#000000"

        android:textSize="25sp" />

</LinearLayout>

 JAVA 中代码 可以看到并没有变动

public class BlankFragment extends Fragment {


    public BlankFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

}

接下来我们去写Fragment2的代码  还是一样 敲了一个个Button进行演示

 

 

<?xml version="1.0" encoding="utf-8"?>
<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=".BlankFragment2">



    <Button

        android:id="@+id/button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Get fragment1 text"

        />

</LinearLayout>

接着打开Fragment2.java,添加onActivityCreated方法,并处理按钮的点击事件:

public class BlankFragment2 extends Fragment {


    private View inflate;

    public BlankFragment2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflate = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Button button = (Button) getActivity().findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                TextView textView = (TextView) getActivity().findViewById(R.id.fragment1_text);

                Toast.makeText(getActivity(), textView.getText(), Toast.LENGTH_LONG).show();

            }

        });

    }
    }

在Fragment2中找到了Fragement1中的组件ID 从而进行Toast并且也可以进行修改 

fragment中onCreateView与onActivityCreated的区别      

 


 

转载请说明出处:https://blog.csdn.net/qq_42046338

不懂onActivityCreated可以查看下上边的链接

展开阅读全文

没有更多推荐了,返回首页