目录
1.fragment是什么
2.编写一个Fragment
3.怎么使用
4.事务API
5.数据传递
6.状态保存和恢复
7.Fragment与Activity交互
8.生命周期
1.fragment是什么
视图组合 业务处理单元
2.编写一个Fragment
- res/layout 目录下新建一个布局文件 如:fragment_layout.xml
- 新建一个MyFragment类 继承Fragment类 实现onCreateView
- 在onCreateView中加载写好的布局文件 创建一个布局mLayout
public class MyFragment extends Fragment {
private View mLayout;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mLayout = inflater.inflate(R.layout.fragment_layout, container, false);
return mLayout;
}
}
3.怎么使用
使用Fragment标签在布局中使用,在具体节点下放置fragment标签
重要属性:
- id :fragment标签或者父容器,必须设置一个id,不然会报错
- tag:给fragment设置一个标签可以在FragmentManager通过tag查找是否存在该实例
- name:编写的Fragment地址
<FrameLayout
android:id="@+id/layoutBanner"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<fragment
android:id="@+id/fragment_banner"
android:name="com.zplh.hei.ui.banner.BannerFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="BannerFragment" />
</FrameLayout>
代码中创建实例 new Fragment() 通过事物添加进布局
Fragment fragment = new MyFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 添加进R.id.container布局中
fragmentTransaction.add(R.id.container, fragment);
fragmentTransaction.commit();
4.事务API
//获得Fragment管理器
FragmentManager fragmentManager = getFragmentManager();
//获得Fragment事务
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//添加一个Fragment
fragmentTransaction.add(int containerViewId, Fragment fragment);
//替换已添加到容器中的现有片段
fragmentTransaction.replace(int containerViewId, Fragment fragment);
//删除一个现有的片段。如果它被添加到一个容器中,它的视图也从该容器中删除
fragmentTransaction.remove(Fragment fragment);
//提交事务(提交才会生效)
fragmentTransaction.commit();
//添加进事务栈 这时候点击back键 会回滚事务
fragmentTransaction.addToBackStack("tag");
//显示一个Fragment
fragmentTransaction.show(Fragment fragment);
//隐藏一个现有的Fragment
fragmentTransaction.hide(Fragment fragment);
......
更多API 查看源码 android.app.FragmentTransaction
5.数据传递
- 传递参数
//通过Bundle来给Fragment传递参数
Bundle args = new Bundle();
//设置参数
args.putString("name", "要传递的字符串");
//实例Fragment
Fragment fragment = new StoreFragmentWithArg();
fragment.setArguments(args);
- .获得参数
//获取参数
Bundle args = getArguments();
if (null != args) {
String name = args.getString("name");
}
6.状态保存和恢复
- 保存
private String name;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("name", name);
}
- 恢复
private String name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
name = savedInstanceState.getString("name");
}
}
7.Fragment与Activity交互
private IComm comm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Activity activity = getActivity();
//onCreate,确定是否支持接口
if (activity instanceof IComm) {
comm = (IComm) activity;
}
}
//定义接口 Activity实现接口
public interface IComm {
void notifyViewCount(String tag, int viewCount);
}
8.生命周期
- 打印生命周期的方法
public class MyFragment extends Fragment {
@Override
public void onAttach(Context context) {
super.onAttach(context);
log("onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
log("onCreate");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
log("onCreateView:创建视图");
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
log("onViewCreated:在创建视图");
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
log("onViewStateRestored:在视图状态恢复");
}
@Override
public void onStart() {
super.onStart();
log("onStart");
}
@Override
public void onResume() {
super.onResume();
log("onResume");
}
@Override
public void onPause() {
super.onPause();
log("onPause");
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
log("onSaveInstanceState:做保存状态");
}
@Override
public void onStop() {
super.onStop();
log("onStop");
}
@Override
public void onDestroyView() {
log("onDestroyView:销毁视图");
super.onDestroyView();
}
@Override
public void onDestroy() {
log("onDestroy");
super.onDestroy();
}
private void log(String tag) {
Log.d("Fragment生命周期", tag);
}
}
依次执行以下操作控制台的日志输出
首次进入
Fragment生命周期: onAttach
Fragment生命周期: onCreate
Fragment生命周期: onCreateView:创建视图
Fragment生命周期: onViewCreated:在创建视图
Fragment生命周期: onViewStateRestored:在视图状态恢复
Fragment生命周期: onStart
Fragment生命周期: onResume关闭屏幕
Fragment生命周期: onPause
Fragment生命周期: onSaveInstanceState:做保存状态
Fragment生命周期: onStop唤醒屏幕
Fragment生命周期: onStart
Fragment生命周期: onResume退出
Fragment生命周期: onPause
Fragment生命周期: onDestroyView:销毁视图
Fragment生命周期: onDestroy
9.探讨
FragmentManager.findFragmentById findFragmentByTag
Fragment推荐使用于需要复用的组件 和需要嵌套组件的需求
Fragment嵌套Fragment