一、fragment的静态使用

  1.使用步骤:  

     a.继承fragment   重写onCreatevVew()的回调方法

   b.设置Fragment的布局

   c.在Activity中声明Fragment  使用方式与控件相同

  2.代码展示:

 a.继承fragment   重写onCreatevVew()的回调方法
public class TileFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.tilefragment, null);
}
}
b.设置Fragment的布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"> //忘记写会有控件覆盖,会报错的 <ImageView
android:id="@+id/iv_bck"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@mipmap/ic_launcher" /> <TextView
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="我是标题"
/>
</LinearLayout> c.在Activity中声明Fragment  使用方式与控件相同
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" //这个忘写会报错误,很难找出来的
tools:context=".MainActivity"> <fragment
android:name="com.example.zhang.myapplication.TileFragment" ----------->引用标签来静态加载fragment的数据
android:layout_width="match_parent"
android:layout_height="50dp"></fragment>
</LinearLayout> 二、fragment的动态使用
  a.创建Fragment的管理器对象
  b.获取Fragment的事物对象并开启
  c.调用事物的动态方法:动态的添加、动态的删除、动态的替换
  d.提交事物
  
  在xml文件中的代码,在布局文件中写好布局并设置ID,通过FragmentTransation.add去动态添加
  <LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/fragment_tile"
/>
  <LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frag_content"
/>
  //获得FragmentManager的对象
  FragmentManager manager = getSupportFragmentManager();
  //获取Fragment的事物对象并开始
  FragmentTransaction transaction = manager.beginTransaction();
  //动态添加fragment (add remove replace)
  transaction.add(R.id.fragment_tile, new TileFragment());
  transaction.add(R.id.frag_content, new ContentFragment());
  //提交获得的事物
  transaction.commit(); 三、Fragment的生命周期
  1.第一次启动app的时候
    onAttach:activity与fragment的产生关联时候进行回调
    onCreateView : 用户第一次绘制界面
    onActivityCreate : fragment与activity创建成功进行回调

  2.切换其他fragment的时候

  3.返回第一次启动的fragment的时候

  4.按home键的时候

  5.home键之后再重新返回当前界面

  6.按back键的时候

  7.fragment和activity的生命周期

     

   


  

  

Android-------------fragment的学习的更多相关文章

  1. android fragment 博客 学习记录

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

  2. Android Fragment应用实战

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

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

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

  4. 【转】基于Android Fragment功能的例子

    原文网址:http://blog.csdn.net/eyu8874521/article/details/8252216 通过最近空闲时候对Fragment的学习,尝试着写了一个小Demo,将在开发的 ...

  5. 基于Android Fragment功能的样例

    通过近期空暇时候对Fragment的学习,尝试着写了一个小Demo,将在开发的时候能经常使用到的Fragment知识放在一起,写过了这个Demo对Android Fragment的了解更加深入了,以后 ...

  6. Android编程权威指南笔记3:Android Fragment讲解与Android Studio中的依赖关系,如何添加依赖关系

    Android Fragment 当我在学习时,了解了Fragment词汇 Fragment是一种控制器对象,我就把所了解的简单说一下.activity可以派fragment完成一些任务,就是管理用户 ...

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

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

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

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

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

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

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

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

随机推荐

  1. dede 复制文章,远程图片无法本地化

    解决方法: 1.找到dede的后台目录,在后台目录下的inc下找到inc_archives_functions.php 2.搜索GetCurContent函数,找到如下这段代码: preg_match ...

  2. JFinal ORM和Hibernate简要对比

    1.JFinal采用ActiveRecord实现数据库操作支持,较Hibernate开发效率提升六到十倍. 2.JFinal ActiveRecord较Hibernate学习成本低,一小时内能上手开发 ...

  3. 利用委托机制处理.NET中的异常

    WinForm代码 private void button1_Click(object sender, EventArgs e) { try { Convert.ToInt32("abcd& ...

  4. PM2使用及介绍

    pm2 是一个带有负载均衡功能的Node应用的进程管理器.当你要把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着,0秒的重载, PM2是完美的.它非常适合IaaS结构,但不要把它用于 ...

  5. .zip/.rar打包与解压

    Linux下如何解压.zip和.rar文件,对于Window下的常见压缩文件.zip和.rar,Linux也有相应的方法来解压它们: 1)对于zip linux下提供了zip和unzip程序,zip是 ...

  6. apicloud代码压缩和全局加密

    首先说代码压缩,因为没什么用,就先说它了.代码压缩后,apicloud里面的css和js文件里面的空格呀回车呀都去掉了,就是文件小了,所有代码显示为一行了.这些代码的变量没有重命名,我们知道jquer ...

  7. 解决Android启动显示空白界面的问题

    Android程序启动时,第一个看的界面并不是我们的指定的第一个Activity界面,而是显示了一个空白的界面,带标题栏的,但是界面什么内容都没有,这个界面只显示不到1秒左右的时间就会切换到我们的第一 ...

  8. 直压到亚马逊AWS平台,阿里云OSS平台或者腾讯云COS平台

    GTX Compressor (直压上云技术预览版) Powered by GTXLab of Genetalks. 技术预览版本下载地址: https://github.com/Genetalks/ ...

  9. 20155333 2016-2017-2 《Java程序设计》第八周学习总结

    20155333 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 认识NIO NIO(New IO)-from JDK1.4 Channel: 衔接数据节点( ...

  10. Navicat for oracle cannot load OCI DLL

    Navicat for oracle 提示 cannot load OCI DLL87,126,193 instant client package is required for basic and ...