一.Fragment概要:

于Fragment经,布局更好地适应各种尺寸的android打电话,加方便的实现不同页面的切换,就不像曾经activity的跳转那样的麻烦了。能够在activity中嵌套不同的Fragment。每一个Fragment能够用不同的布局,能够动态的进行加入、替换,就像以下的图片一样:

 

二. Fragment的生命周期:

每一个Fragment都有自己生命周期。可是与activity的生命周期不全同样,Fragment额外的加入了5个生命周期回调方法。先看一下图:

这五个方法:

1.onAttach(Activity);  //当Activity与Fragment发生关联时调用。

2.onCreateView(LayoutInflater,ViewGroup,Bundle);  //创建该Fragment的视图

3.onActivityCreate(bundle);  //当Activity的onCreate()。方法返回时调用

4.onDestoryView();  //与onCreateView相相应,当改Fragment被移除时调用

5.onDetach();  //与onAttach()相相应,当Fragment与Activity的关联被取消时调用

注意:除了onCreateView。其它的全部方法假设你重写了,必须调用父类对于该方法的实现。
继承Fragment必须重写这种方法:
(2).onCreateView():
fragment第一次绘制它的用户界面的时候, 系统会调用此方法. 为了绘制fragment的UI,此方法必须返回一个View, 这个view就是你在fragment中实现的布局。不提供则返回null。

生命周期:

(1)第一次启动Fragment:

onAttach

onCreate

onCreateView

onActivityCreated

onStart

onResume

(2)切换到其它Fragment:

onPause

onStop

onDestroyView

(3)切换回来:

onCreateView

onActivityCreated

onStart

onResume

(4)返回手机桌面:

onPause

onStop

回到应用

onStart

onResume

(5)退出应用

onPause

onStop

onDestroyView

onDestroy

onDetach

三.动态的使用Fragment:

Fragment常常作为activity的界面的一部分,既然是一部分那么Fragment肯定会给activity用一个layout,也能够说是一个view。那么就要通过onCreateView返回activity一个layout,那么怎么返回呢?就要重写这种方法:

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return super.onCreateView(inflater, container, savedInstanceState);}

这里的三个參数第一个inflater不用说是用来获得布局文件的,第二个參数container就是将要插入的父ViewGroup,第三个提供恢复Fragment提供数据用的。

重写就要这样实现:

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_1, container, false);}

这样就能够把Fragment布局返回了。

四.实现開始的效果图:

主界面:

public class MainActivity extends Activity implements OnClickListener {RelativeLayout r1;RelativeLayout r2;RelativeLayout r3;RelativeLayout view = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.bottom_layout);r1 = (RelativeLayout) findViewById(R.id.layout1);r2 = (RelativeLayout) findViewById(R.id.layout2);r3 = (RelativeLayout) findViewById(R.id.layout3);r1.setOnClickListener(this);r2.setOnClickListener(this);r3.setOnClickListener(this);setDefaultFragment();}private void setDefaultFragment() {FragmentManager fm = getFragmentManager();FragmentTransaction transaction = fm.beginTransaction();MyFragment my = new MyFragment();transaction.replace(R.id.frame_layout1, my);transaction.commit();}@Overridepublic void onClick(View arg0) {FragmentManager fm = getFragmentManager();// 开启Fragment事务FragmentTransaction transaction = fm.beginTransaction();switch (arg0.getId()) {case R.id.layout1:if (view != null) {view.setBackgroundResource(R.color.back_bg);}view = r1;r1.setBackgroundResource(R.color.black_purple);MyFragment my = new MyFragment();transaction.replace(R.id.frame_layout1, my);transaction.commit();break;case R.id.layout2:if (view != null) {view.setBackgroundResource(R.color.back_bg);}view = r2;r2.setBackgroundResource(R.color.black_purple);MyFragment2 my2 = new MyFragment2();transaction.replace(R.id.frame_layout1, my2);transaction.commit();break;case R.id.layout3:if (view != null) {view.setBackgroundResource(R.color.back_bg);}view = r3;r3.setBackgroundResource(R.color.black_purple);MyFragment3 my3 = new MyFragment3();transaction.replace(R.id.frame_layout1, my3);transaction.commit();break;}}
}

主界面布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><LinearLayoutandroid:id="@+id/linear_layout"android:layout_width="fill_parent"android:layout_height="50dp"android:layout_alignParentBottom="true"android:background="#696969"android:orientation="horizontal" ><RelativeLayoutandroid:id="@+id/layout1"android:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="@drawable/tab_bg" ><ImageViewandroid:id="@+id/image1"android:layout_width="28dp"android:layout_height="28dp"android:layout_centerHorizontal="true"android:layout_marginTop="5dp"android:background="@drawable/ic_launcher" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/image1"android:layout_centerHorizontal="true"android:text="音乐"android:textSize="13sp" ></TextView></RelativeLayout><RelativeLayoutandroid:id="@+id/layout2"android:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="@drawable/tab_bg" ><ImageViewandroid:id="@+id/image2"android:layout_width="28dp"android:layout_height="28dp"android:layout_centerHorizontal="true"android:layout_marginTop="5dp"android:background="@drawable/ic_launcher" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/image2"android:layout_centerHorizontal="true"android:text="电影"android:textSize="13sp" ></TextView></RelativeLayout><RelativeLayoutandroid:id="@+id/layout3"android:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="@drawable/tab_bg" ><ImageViewandroid:id="@+id/image3"android:layout_width="28dp"android:layout_height="28dp"android:layout_centerHorizontal="true"android:layout_marginTop="5dp"android:background="@drawable/ic_launcher" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/image3"android:layout_centerHorizontal="true"android:text="影视"android:textSize="13sp" ></TextView></RelativeLayout></LinearLayout><FrameLayoutandroid:id="@+id/frame_layout1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_above="@id/linear_layout" ></FrameLayout></RelativeLayout>

Fragment实现:

public class MyFragment extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_1, container, false);}}

Fragment布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="第一个页面" /></RelativeLayout>

另外两个Fragment和这个同样。

版权声明:本文博主原创文章,博客,未经同意不得转载。

转载于:https://www.cnblogs.com/gcczhongduan/p/4886196.html

android (12) Fragment使用相关推荐

  1. Android之 Fragment

    什么是Fragment: Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局, ...

  2. Android 12 快速适配要点

    Android 12 需要更新适配点并不多,本篇主要介绍最常见的两个需要适配的点:android:exported 和 SplashScreen . 一.android:exported 它主要是设置 ...

  3. PermissionX 1.6发布,支持Android 12,可能是今年最大的版本升级

    本文同步发表于我的微信公众号,扫一扫文章底部的二维码或在微信搜索 郭霖 即可关注,每个工作日都有文章更新. 各位小伙伴们大家早上好. 没错,PermissionX又升级了,并且这次版本变化非常大,很有 ...

  4. Android(12)浅析 偏好设置 Preference(一)

    Android(12)浅析 偏好设置 Preference(一) ### 官方基本用法:https://developer.android.google.cn/guide/topics/ui/sett ...

  5. android viewpager 嵌套fragment,Android ViewPager+Fragment多层嵌套(使用问题处理)

    之前写了Android ViewPager+Fragment(使用问题处理),封装了一个BaseFragment,对于简单使用ViewPager+Fragment而言,是没有问题的. 不过,ViewP ...

  6. android 新浪财经截屏分享,[图]Android 12截图曝光:启用全新UI 增强隐私保护功能...

    原标题:[图]Android 12截图曝光:启用全新UI 增强隐私保护功能 来源:cnBeta.COM Android 12 有望在今年秋季上线,相关的开发者预览版和 Beta 版本有望在本月晚些时候 ...

  7. Android 在Fragment中执行onActivityResult不被调用的简单解决方法

    Android 在Fragment中执行onActivityResult不被调用的简单解决方法 参考文章: (1)Android 在Fragment中执行onActivityResult不被调用的简单 ...

  8. 原生Android12,Android 12原生设计曝光 网友看了惊呼神似iOS

    新酷产品第一时间免费试玩,还有众多优质达人分享独到生活经验,快来新浪众测,体验各领域最前沿.最有趣.最好玩的产品吧~!下载客户端还能获得专享福利哦! 网友都说新浪众测不讲武德,数码新品说发就发,新奇好 ...

  9. android碎片按钮,Android 碎片(Fragment)

    Android 碎片(Fragment) 碎片是活动的一部分,使得活动更加的模块化设计.我们可以认为碎片是一种子活动. 下面是关于碎片的重要知识点 - 碎片拥有自己的布局,自己的行为及自己的生命周期回 ...

  10. Android 进阶 Fragment 介绍和使用 (一)

    Fragment概述 Fragment是activity的界面中的一部分或一种行为.你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一 ...

最新文章

  1. tensorrt动态输入分辨率尺寸
  2. Scala模式匹配细节说明
  3. Oracle 原理: 过程和函数
  4. 5训练需要更改参数吗_糖尿病病人需要多喝水吗?多喝水的5大好处,了解一下...
  5. JNative用法注意事项
  6. Linux常用指令自己备用
  7. excl中将某列数据合到单个单元格
  8. web前端工程师学习路线指南,完整Web前端学习路线图
  9. python 输出文字_Python中输出ASCII大文字、艺术字、字符字小技巧
  10. ASP.NET生成验证码
  11. 一个学习PCI Express的 不错去处
  12. 综合能源管理服务认证是什么 , 综合能源服务认证有什么要求?
  13. 软件概要设计如何写(文档恐惧症的程序猿必读)
  14. Magento二次开发哪家好呢?
  15. 根据sitemap一键推送给百度收录的python小脚本
  16. python程序设计基础刘艳网课_智慧职教APPPython程序设计基础(九江职业技术学院)题目答案...
  17. 我的世界服务器组件管理器,我的世界ESS插件 Essentials | 我的世界 | MC世界侠
  18. 汇编 windows 10 通过DOSBox使用Debug程序
  19. ucenter php版本,PHP优秀系统UCenter的MVC架构
  20. 【Shell】判断文件是否包含给定字符串

热门文章

  1. jQuery form表单的serialize()参数和其他参数 如何一起传给后端
  2. django进阶03静态文件和模板
  3. CTA策略07_MultiTimeframeStrategy
  4. 从中台、数仓与元数据不为人知的3个角度,看数据管理的生与死
  5. 案例学习BlazeDS+Spring之五InSync03强类型
  6. 编译自定义的主题theme
  7. 显示所有大写字母python_python 输出所有大小写字母的方法
  8. 二叉树前序遍历python输出_[宜配屋]听图阁 - Python实现输入二叉树的先序和中序遍历,再输出后序遍历操作示例...
  9. Win7matlab7.0安装教程,Win7下MATLAB7.0安装教程
  10. 饭卡 01背包 DP