Fragment经常在我们的开发中见到,但是自我感觉对Fragment的理解还是处于初级的阶段,接下来我将用几篇文章尽量深的解析Fragment

让我们开始吧!!!


Fragment的生命周期

Fragment拥有自己的生命周期和接收、处理用户的事件,生命周期我们就用官网的一张图来展示一下

    

这张图对我来说还是比较好记的,他把周期分成基本的三大块吧

  1. Create阶段
  2. 和Activity声明周期相似的阶段
  3. Destroyed阶段

怎么样相对来说这样还是不错的吧~~~

Fragment的静态使用

 <RelativeLayout 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" > <fragment
android:id="@+id/id_fragment_title"
android:name="com.fragments.TitleFragment"
android:layout_width="fill_parent"
android:layout_height="45dp" /> <fragment
android:layout_below="@id/id_fragment_title"
android:id="@+id/id_fragment_content"
android:name="com.fragments.ContentFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /> </RelativeLayout>

主要是标黄的部分,这里在xml中静态的写入Fragment的路径,这样就能够引入到布局中,顺便看一下布局中还能直接写fragment标签,以前也知道,但是长时间不用都忘记了,这次正好复习一下,但是这种方式在开发中使用的机率还是比较小的,下面的方式才是我们主要是用的方式。

Fragment的动态使用

如何动态的添加、更新、以及删除Fragment

 <RelativeLayout 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" > <FrameLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/id_ly_bottombar"
android:layout_below="@id/id_fragment_title" /> </RelativeLayout>

布局中主要使用的帧布局FreamLayout   这是用来承载Fragment的布局

Activity的主要代码

 FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
mFragment = new ContentFragment();
transaction.replace(R.id.id_content, mFragment);
transaction.commit();

代码中的FragmentManager提供了对Fragment的动态管理,fm.beginTransaction()相当于数据库中的事物的解析

Android Fragment 初步解析的更多相关文章

  1. Android Fragment完全解析

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

  2. Android Fragment完全解析,关于碎片你所需知道的一切

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

  3. Android Fragment 完全解析

    参考文章:http://blog.csdn.net/guolin_blog/article/details/8881711 http://blog.csdn.net/guolin_blog/artic ...

  4. Android Fragment完全解析,关于碎片你所需知道的一切 (转)。

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

  5. (转) Android Fragment完全解析,关于碎片你所需知道的一切

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

  6. Android Fragment 深度解析

    1.Fragment的产生与介绍 Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套app,然后拷贝一份,修改布局以适应 ...

  7. Android Fragment应用实战

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

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

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

  9. Android Fragment碎片

    什么是碎片? 碎片(Fragment)是一种可以嵌入在活动当中的UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常广泛.可以把Fragment当成Activity一个界面的一 ...

随机推荐

  1. javaee IO流打印一行的方式

    package Dayin; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.F ...

  2. mysql与oracle 表字段定义比较

    链接: https://blog.csdn.net/yzsind/article/details/7948226

  3. 【转载】intellij idea如何将web项目打成war包

    1.点击[File]->[Project Structure]菜单(或使用Shift+Ctrl+Alt+S快捷键),打开[Project Structure]窗口.如下图: 2.在[Projec ...

  4. vue-cli index.js dev 配置中 assetsPublicPath 的值不能填 "./" 的问题

    问题 使用nginx又代理了一层 在浏览器中 / 代表域名的根目录,./代表当前路径 线上发布的时候一般都会使用nginx反向代理,所以使用./是最靠谱的,但是vue-cli dev 中的 asset ...

  5. Ubuntu的shell执行过程

    登录shell(login shell)会执行.bash_profile,.bash_profile中会执行.profile,.profile中会执行.bashrc 非登录shell(non-logi ...

  6. Google HTML/CSS Style Guide

    转自: http://google.github.io/styleguide/htmlcssguide.xml Google HTML/CSS Style Guide Revision 2.23 Ea ...

  7. svn版本库更新后自动同步到www

    注意:www目录一定要用SVN服务器 checkout出Repositories的代码 步骤: (1)新建www根目录 mkdir -p /data/www/lehuo (2)在www根目录下检出(c ...

  8. python-写入csv 文件

    项目要做一个导出客户信息的功能,需要写入csv: 注意文件写入的方式  例如   write open(‘w’) 从头开始写,之前写的会被替换  write open(‘a’) 则代表追加,文件指针放 ...

  9. Codeforces 851B/C

    B. Arpa and an exam about geometry 传送门:http://codeforces.com/contest/851/problem/B 本题是一个平面几何问题. 平面上有 ...

  10. Codeforces 805A/B/C

    A. Fake NP 传送门:http://codeforces.com/contest/805/problem/A 本题是一个数学问题. 给定两个正整数l,r(l≤r),对于区间[l..r]上的任一 ...