一 、Fragment的理解

Fragment 与activity 相似,但比activity 多出几个方法 ,Fragment的生命周期小于activity

  一个Activity 中可以包含多个Fragment   ,fragment 就像一个个积木,可以组合成一个大的玩具Activity。

activity     oncreate  onstart  onResume  onPause  onstop  ondestroy   onRestart

1. onAttach() 当Fragment和Activity建立关联的时候调用。

2. onCreateView() 为Fragment创建视图(加载布局)时调用。

3. onActivityCreated() 确保与Fragment相关联的Activity一定已经创建完毕的时候调用。

4. onDestroyView() 当与Fragment关联的视图被移除的时候调用。

5. onDetach() 当Fragment和Activity解除关联的时候调用。

二、 Fragment 实际使用例子

  1、 准备 一个xml 文件

首先,创建一个工程,叫做FragmentTest,然后创建一个left_fragment.xml文件,代码如下:
  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="点我"
/>
</LinearLayout>

  

  2、创建一个LeftFragment 类 继承于 Fragment 类 ,包选择 android.app.Fragment 。 V4 下面的Fragment是为了兼容低版本的安卓系统。

    重写  oncreateView() 方法,将 left_fragment.xml 文件添加到布局中

 public class LeftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container, false);
return view;
}
}

  同理,在准备right_fragment

  3、 在Activity 中加入fragment 。同样,首先建立  activity_main.xml 文件

 ?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"
android:orientation="horizontal"> <fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
></fragment> <FrameLayout
android:id="@+id/right_framelayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<fragment
android:id="@+id/right_fragment"
android:name="com.example.fragmenttest.RightFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
></fragment>
</FrameLayout> </LinearLayout>

  4、运行程序 ,主页面加载完 activity_main.xml  会显示如下界面。

  

三、动态加载替换 fragment

上面的例子,我们是在activity_main.xml文件中写死了fragment

  1、还是准备好xml文件 other_fragment.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#CFEDDB"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="我是另一个的fragment"
/>
</LinearLayout>

  2、Othe_Fragment 类

1 public class LeftFragment extends Fragment {
2 @Nullable
3 @Override
4 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
5 View view = inflater.inflate(R.layout.other_fragment, container, false);
6 return view;
7 }
8 }

3、 MainActivity中给按钮设置点击事件,动态添加Fragment的代码逻辑都放在了点击事件中。

  private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//总共分5步
//创建fragment对象
OtherFragment otherFragment = new OtherFragment();
//获取FragmentManager管理器
FragmentManager fragmentManager = getFragmentManager();
//开启事务
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//替换碎片文件
fragmentTransaction.replace(R.id.right_framelayout,otherFragment);
//提交事务
fragmentTransaction.commit();
}
});
}

运行如下

android Fragment 使用的更多相关文章

  1. 【Android自学日记】【转】Android Fragment 真正的完全解析(下)

    上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和各种API,如果你还不了解,请看:Android Fragment 真正的完全解析(上). 本篇将介绍上篇博客提到的:如何管理Frag ...

  2. Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理

    Toolbar作为ActionBar使用介绍 本文介绍了在Android中将Toolbar作为ActionBar使用的方法. 并且介绍了在Fragment和嵌套Fragment中使用Toolbar作为 ...

  3. Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复

    Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...

  4. Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误

    嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...

  5. Android Fragment使用(一) 基础篇 温故知新

    Fragment使用的基本知识点总结, 包括Fragment的添加, 参数传递和通信, 生命周期和各种操作. Fragment使用基础 Fragment添加 方法一: 布局里的标签 标识符: tag, ...

  6. Android Fragment应用实战

    现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...

  7. Android Fragment 真正的完全解析(下)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和 ...

  8. Android Fragment

    1.Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期. 2.Fragment 生命周期: 首页 最新文章 在线课程 业界 开发 ...

  9. Android Fragment应用实战,使用碎片向ActivityGroup说再见

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/13171191 现在Fragment的应用真的是越来越广泛了,之前Android在3 ...

  10. Android Fragment完全解析

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...

随机推荐

  1. php程序员绝不能违背的安全铁则

    作为PHP程序员,特别是新手,对于互联网的险恶总是知道的太少,对于外部的入侵有很多时候是素手无策的,他们根本不知道黑客是如何入侵的.提交入侵.上传漏洞.sql 注入.跨脚本攻击等等.作为最基本的防范你 ...

  2. git用.gitignore忽略指定文件

    .gitignore 配置文件用于配置不需要加入版本管理的文件,配置好该文件可以为我们的版本管理带来很大的便利,以下是个人对于配置 .gitignore 的一些心得. 1.配置语法: 以斜杠“/”开头 ...

  3. Hessian怎样实现远程调用

    1.Spring中除了提供HTTP调用器方式的远程调用,还对第三方的远程调用实现提供了支持,其中提供了对Hessian的支持. Hessian是由Caocho公司发布的一个轻量级的二进制协议远程调用实 ...

  4. 在线免费生成 <IDEA>全系列 注册码

    body { background: #fff; color: #333; font-family: Consolas, sans-serif; margin: 2em auto; width: 70 ...

  5. C# ref、out、params与值类型参数修饰符

    1.值类型: static void Main(string[] args) { ; ; NumVal(a, b); Console.WriteLine("a={0},b={1}" ...

  6. windows API 创建临时文件

    创建完临时文件后,即可用C\C++标准函数写入.读取,也可以用API.MFC方法来操作. TCHAR szPathName[MAX_PATH]; TCHAR szFileName[MAX_PATH]; ...

  7. 基于ssh框架的在线考试系统开发的质量属性

    我做的系统是基于ssh框架的在线考试系统.在线考试系统有以下几点特性:(1)系统响应时间需要非常快,可以迅速的出题,答题.(2)系统的负载量也需要非常大,可以支持多人在线考试(3)还有系统的安全性也需 ...

  8. 基于eBox旋转编码器

    在电子产品设计中,经常会用到旋转编码开关,比如数码电位器等,它的英文名翻译过来就是Rotary Encoder Switch.常见的有5pin和3pin产品.5pin实在左右旋转的基础上增加了向下按得 ...

  9. SQL语句 还原未知逻辑名称数据库

    1. 查看 SQL Server 2000 中 Northwind 数据库文件的逻辑文件名(logical file name)和物理文件路径(operation system file name): ...

  10. >Python下使用subprocess中文乱码的解决方案

    # -*- coding: CP936 -*- import subprocess cmd="cmd.exe" begin=101 end=110 while begin<e ...