Fragment详解
2016-09-14 本文已影响45人
suxiliu
xml加载Fragment
1.新建两个Fragment继承Fragment
注意原生的包android.support.v4.app.Fragment;不能用,要改为android.app.Fragement;


2.在主布局文件中加载两个Fragment
id和name作为Fragment的主要标示

动态加载Fragment
只需要4个步骤。在Activtiy的onCreate()方法中
1.获取getFragmentManager()
2.开启事物beginTransaction()
3.使用Fragment的方法add()、remove()、replace()方法
4.提交事务commit();

Fragment之间的通信
案例:
1.第一个Fragment的TextView加id,在第二个Fragment获取这个TextView的内容
2.第二个Fragment,加一个Button,单击这个按钮可以获取第一个Fragment的TextView内容
3.在第二个Fragment的 java代码中,重写onActivityCreate()方法,在这个方法里面编写代码

Fragment生命周期

