Fragment是Android honeycomb 3.0新增的概念,Fragment名为碎片不过却和Activity十分相似,下面介绍下Android Fragment的作用和用法。Fragment用来描述一些行为或一部分用户界面在一个Activity中,你可以合并多个fragment在一个单独的activity中建立多个UI面板,同时重用fragment在多个activity中.你可以认为fragment作为一个activity中的一节模块 ,fragment有自己的生命周期,接收自己的输入事件,你可以添加或移除从运行中的activity.

一个fragment必须总是嵌入在一个activity中,同时fragment的生命周期受activity而影响,举个例子吧,当activity 暂停,那么所有在这个activity的fragments将被destroy释放。然而当一个activity在运行比如resume时,你可以单独的操控每个fragment,比如添加或删除。

Fragment作为Android 3.0的新特性,有些功能还是比较强大的,比如 合并两个Activity,如图

如上图所示,用Activity A 表示文章标题列表,ActivityB表示文章具体内容。我们可以看到两个Activity通过两个Fragment合并到一个Activity的布局方式,对于平板等大屏幕设备来说有着不错的展示面板。不过因为Fragment和Activity的生命周期都比较复杂,下图表示的fragments的生命周期:

Activity、Fragment分别对比下:

两个的生命周期很类似,也息息相关。

创建一个fragment你必须创建一个Fragment的子类或存在的子类,比如类似下面的代码

public static class AndroidFragmentextends Fragment {
    @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
              return inflater.inflate(R.layout.android_fragment, container,false);
    }
}

onCreate()
当fragment创建时被调用,你应该初始化一些实用的组件,比如在fragment暂停或停止时需要恢复的

onCreateView()
当系统调用fragment在首次绘制用户界面时,如果画一个UI在你的fragment你必须返回一个View当然了你可以返回null代表这个fragment没有UI.

那么如何添加一个Fragment到Activity中呢? Activity的布局可以这样写

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment 
android:name="com.android.cwj.ArticleListFragment"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    <fragment 
android:name="com.android.cwj.ArticleReaderFragment"
            android:id="@+id/viewer"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</LinearLayout>

最后提醒大家Fragment存在于Activity的ViewGroup中,按照继承关系大家就可以了解他的结构。

参考:

docs/guide/topics/fundamentals/fragments.html

http://dasheng.iteye.com/blog/986809

http://zwkufo.blog.163.com/blog/static/258825120112207254076/

/**
* @author 张兴业
* 邮箱:xy-zhang@163.com
* qq:363302850
*/

转载于:https://blog.51cto.com/xyzlmn/817257

Android——Fragment介绍相关推荐

  1. Android Fragment 基本介绍

    Android Fragment 基本介绍 Android Fragment 基本介绍 Fragment Android是在Android 3.0 (API level 11)开始引入Fragment ...

  2. 【转】 Android Fragment 真正的完全解析(下)

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

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

    本篇将介绍上篇博客提到的:如何管理Fragment回退栈,Fragment如何与Activity交互,Fragment与Activity交互的最佳实践,没有视图的Fragment的用处,使用Fragm ...

  4. Android Fragment嵌套导致的bug

    原文链接 Android 多个Fragment嵌套导致的三大BUG Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误 http ...

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

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

  6. Android Fragment 简单实例

    Android上的界面展示都是通过Activity实现的.Activity实在是太经常使用了.我相信大家都已经很熟悉了,这里就不再赘述. 可是Activity也有它的局限性,相同的界面在手机上显示可能 ...

  7. Android Fragment 真正彻底的解决(下一个)

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因.以及一些主要的使用方 ...

  8. Android Fragment手柄后退按钮按下[重复]

    本文翻译自:Android Fragment handle back button press [duplicate] This question already has answers here : ...

  9. Android Fragment 真正的完全解析

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 Hongyang 目录视图 摘要视图 订阅 有奖征资源,博文分享有 ...

最新文章

  1. Codeforces1063D Candies for Children 【分类讨论】【暴力】
  2. docker 1.8.2 源代码编译
  3. Intellij导入子项目时,maven列表子项目灰色不可用---解决方法
  4. php mysql 测试页_mysql+php分页类(已测)
  5. [渝粤教育] 中国地质大学 信息资源管理 复习题 (2)
  6. 新手入门概览(一):机器学习
  7. Orchard Core 使用工作流处理页面提交
  8. 使用MonkeyTest对Android客户端进行压力测试 自动化代码
  9. 软件工程 2016.7.3 日报
  10. linux wifi中继,树莓派无线中继
  11. Python爬虫-模拟登入-selenium模块
  12. 1062lcd在dxp哪个库_Protel DXP 2004印刷电路板设计方法
  13. Linux微信安装网页版
  14. 由“外”向“内”,安踏破解运动科技“卡脖子”难题
  15. ARM——开发工具—编译器
  16. aps生产排程解决家具用品业的难题
  17. postman双击打不开的解决方案
  18. 科大讯飞胡郁:我所领导的超脑项目,代表了AI的未来
  19. elementUI tab 切换 table表头消失
  20. 程序员:妹妹高考650多,她想选择互联网专业,我该怎么劝?

热门文章

  1. 【一】Drupal 入门之新建主题
  2. CTF加解密/编码常用在线网址
  3. CG-CTF-Web-AAencode
  4. 回流Reflow和重绘Repaint
  5. Linux上搭建verdaccio私服
  6. 小程序 -- [sitemap 索引情况提示] 根据 sitemap 的规则[0],当前页面 [pages/index/index] 将被索引
  7. 判断扫码是支付宝还是微信
  8. 中的stop_谈谈stop容器
  9. Enhanced LSTM for Natural Language Inference-学习笔记
  10. 多用继承 android,Android温习之路之继承