您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码:  验证码,看不清楚?请点击刷新验证码 必填



  求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Modeler   Code  
会员   
 
   
 
 
     
   
 订阅
  捐助
安卓第十天笔记-fragment
 
作者:森林森 来自于:博客园 发布于 2016-3-25
  316  次浏览      15
 

一.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()

   
316 次浏览       15
 
相关文章

手机软件测试用例设计实践
手机客户端UI测试分析
iPhone消息推送机制实现与探讨
Android手机开发(一)
 
相关文档

Android_UI官方设计教程
手机开发平台介绍
android拍照及上传功能
Android讲义智能手机开发
相关课程

Android高级移动应用程序
Android系统开发
Android应用开发
手机软件测试
最新课程计划
信息架构建模(基于UML+EA)3-21[北京]
软件架构设计师 3-21[北京]
图数据库与知识图谱 3-25[北京]
业务架构设计 4-11[北京]
SysML和EA系统设计与建模 4-22[北京]
DoDAF规范、模型与实例 5-23[北京]

android人机界面指南
Android手机开发(一)
Android手机开发(二)
Android手机开发(三)
Android手机开发(四)
iPhone消息推送机制实现探讨
手机软件测试用例设计实践
手机客户端UI测试分析
手机软件自动化测试研究报告
更多...   

Android高级移动应用程序
Android应用开发
Android系统开发
手机软件测试
嵌入式软件测试
Android软、硬、云整合

领先IT公司 android开发平台最佳实践
北京 Android开发技术进阶
某新能源领域企业 Android开发技术
某航天公司 Android、IOS应用软件开发
阿尔卡特 Linux内核驱动
艾默生 嵌入式软件架构设计
西门子 嵌入式架构设计
更多...