安卓第十天笔记-fragment

Fragment(片段)

一.Fragment简介

*Fragment是3.0引入的API,主要为了解决平板,大屏幕手机显示问题

*Fragment代表了Activity的子模块,因此可以把fragment理解成Activity的片段

*Fragment必须被嵌入Activity中使用

二.创建Fragment的步骤

  1. 创建一个类继承Fragment类或者其子类
  2. 重写Fragment中的一些onxxx方法
  3. 一般有三个方法要重写

    .onCreate()系统创建Fragment的对象后回调该方法,初始化操作 .onCreateView()当fragment绘制界面组件时会回调该方法 .onPause()当用户离开该Fragment时将回调这个方法

三使用fragment使用Activity

*创建一个布局用来填充fragment

<?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"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="声音大小"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="速度"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3D"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="左声道"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="右声道"/>
</LinearLayout>

*创建一个fragement public class SoundFragment extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.sound_item,null) ; return view;
}

*主界面布局

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layoutwidth="matchparent" android:layoutheight="matchparent" android:orientation="vertical">

<!--容器-->
<FrameLayout
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"> </FrameLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <Button
android:onClick="sound"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="声音"/>
</LinearLayout> </LinearLayout>

*Activity

      /**
* 点击声音按键显示fragment
* @param v
*/
public void sound(View v){
//声明Fragment
SoundFragment soundFragment = new SoundFragment();
//获取Fragment管理器
FragmentManager manager = getFragmentManager();
//开启事务
FragmentTransaction transaction = manager.beginTransaction();
//替换掉当前布局中的帧布局容器
transaction.replace(R.id.fl_container,soundFragment);
//提交事务
transaction.commit();
}

四生命周期

  • onAttach---用于与Acivity建立连接与断开,当该Fragment被添加到Activity时被回调,只会被调用一次
  • onCreate---创建Fragment时被调用
  • onCreateView--每次创建Fragment时都会被调用
  • onActivityCreated:当Fragment所在的Activity被启动完成后调用
  • onStart:启动Fragment时调用
  • onResume:恢复Fragment时被回调,在onStart方法后一定会被调用
  • onPause:暂停Fragment时被回调
  • onStop:停止Fragment时被回调
  • onDestroyView:建立与销毁视图
  • onDesctory:用于初始化与销毁与Activity中的一样
  • onDetach:将这个Fragment从Activity中删除,替换完成时调用这个方法,在onDestroy方法后一定回调用这个方法

五 Activity与Fragment生命周期对比

Activity--------------------Fragment
onAttach()
onCreate()
onCreateView()
onCreate() onActivityCreated onStart() onStart() onResume() onResume() onPause() onPause() onStop() onStop() onDestoryView()
onDestory()
onDestory() onDetach()

安卓第十天笔记-fragment的更多相关文章

  1. 安卓第十四天笔记-内容提供者(ContentProvider)

    安卓第十四天笔记-内容提供者(ContentProvider) ContentProvider--内容提供者 1.ContentProvider简介 ContentProvider是不同应用程序之间进 ...

  2. 安卓开发笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)

    记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果,但作为学习,还是需要来了解下这个新引入类FragmentTabHost 之前2篇文章的链接: 安 ...

  3. 安卓第十三天笔记-服务(Service)

    安卓第十三天笔记-服务(Service) Servcie服务 1.服务概念 服务 windows 服务没有界面,一直运行在后台, 运行在独立的一个进程里面 android 服务没有界面,一直运行在后台 ...

  4. 安卓第十一天笔记-Intent与inter-filter配置

    安卓第十一天笔记-Intent与inter-filter配置 Intent与inter-filter配置 1.Intent对象简述 Android应用中有包含三种重要组件:Activity,Servi ...

  5. 安卓第四天笔记-Sqlite

    安卓第四天笔记-Sqlite 1.数据库的创建运行与更新 1.1.创建一个类继承SqliteOpenHelper 1.2.创建构造方法 /** * 数据库创建类 * @author 刘楠 * * 20 ...

  6. Android UI开发第二十八篇——Fragment中使用左右滑动菜单

    Fragment实现了Android UI的分片管理,尤其在平板开发中,好处多多.这一篇将借助Android UI开发第二十六篇——Fragment间的通信. Android UI开发第二十七篇——实 ...

  7. 安卓开发笔记——Fragment+ViewPager组件(高仿微信界面)

    什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开发复习笔记——ViewPager组件(仿微信引导界面)>,不清楚的朋友可以看看,这里就不再 ...

  8. Android基础笔记(十八)- Fragment

    博客的感悟终点-開始 什么是Fragment 加入fragment到Activity的两种方式 Fragment的生命周期 Fragment的向下兼容 Fragment之间的通信 博客的感悟,终点-開 ...

  9. 【转】 Pro Android学习笔记(四十):Fragment(5):适应不同屏幕或排版

    目录(?)[-] 设置横排和竖排的不同排版风格 改写代码 对于fragment,经常涉及不同屏幕尺寸和不同的排版风格.我们在基础小例子上做一下改动,在横排的时候,仍是现实左右两个fragment,在竖 ...

随机推荐

  1. 【原创】14. MYSQL++之SSQLS(原理解析)

    从之前所介绍的SSQLS的介绍中我们可以感受到,SSQLS的精髓应该在sql_create_#这个宏,他所创建出来的这个结构体将会是突破的关键,所以我将会从以下顺序入手. 1. sql_create_ ...

  2. C++的ORM工具比较

        用过Java的都知道SSH框架,特别对于数据库开发,Java领域有无数的ORM框架,供数据持久层调用,如Hibernate,iBatis(现在改名叫MyBatis),TopLink,JDO,J ...

  3. [Linux] zip 与 unzip 命令

    zip zip 命令基本格式为: zip options archive inpath inpath ... archive 是 .zip 文件(新的或已经存在的). inpath 是需要打包的目录或 ...

  4. 【原创】C#搭建足球赛事资料库与预测平台(5) 赔率数据表设计1

            本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html 开源C#彩票数据资料库系列文章总目录:http://www.cn ...

  5. 如何编写Angular指令

    [20140917]Angular:如何编写一个指令 *:first-child { margin-top: 0 !important; } body>*:last-child { margin ...

  6. wamp2.5虚拟站点建立

    1. D:\wamp\bin\apache\Apache2.2.21\conf 目录下打开httpd.conf修改以下几个地方.(软件安装在D盘) (1)178行DocumentRoot " ...

  7. Angular系列----AngularJS入门教程01:AngularJS模板 (转载)

    是时候给这些网页来点动态特性了——用AngularJS!我们这里为后面要加入的控制器添加了一个测试. 一个应用的代码架构有很多种.对于AngularJS应用,我们鼓励使用模型-视图-控制器(MVC)模 ...

  8. CheckListBox的实现方式分析

    实际项目中常常要实现有CheckBox列表框.但是WPF没有自带这样的一个控件,下面就用Style来实现这样的功能.而对于CheckBox列表框,又常常会有一个Select All的CheckBox来 ...

  9. STL or Force --- CSU 1553: Good subsequence

    Good subsequence Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1553 Mean: 给你一个长 ...

  10. 开发(ASP.NET程序)把写代码写至最有面向对象味道

    前几天,搬房子时又拿起<重构----改善既有代码的设计>这本书来随便翻来看下,重构Refactoring在开发时,是时常也经常会使用得到. 她确实教我们怎样把写程序写简洁,清楚 好明白,好 ...