一.Fragment概要:

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

 

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDcwODY2Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

二. Fragment的生命周期:

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

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDcwODY2Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

这五个方法:

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,那么怎么返回呢?就要重写这种方法:

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

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

重写就要这样实现:

@Override
public 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; @Override
protected 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();
} @Override
public 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" > <LinearLayout
android:id="@+id/linear_layout"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#696969"
android:orientation="horizontal" > <RelativeLayout
android:id="@+id/layout1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/tab_bg" > <ImageView
android:id="@+id/image1"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="@drawable/ic_launcher" /> <TextView
android: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> <RelativeLayout
android:id="@+id/layout2"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/tab_bg" > <ImageView
android:id="@+id/image2"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="@drawable/ic_launcher" /> <TextView
android: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> <RelativeLayout
android:id="@+id/layout3"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/tab_bg" > <ImageView
android:id="@+id/image3"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="@drawable/ic_launcher" /> <TextView
android: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> <FrameLayout
android: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{

	@Override
public 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" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="第一个页面" /> </RelativeLayout>

另外两个Fragment和这个同样。

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

android (12) Fragment使用的更多相关文章

  1. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  2. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  3. Android中Fragment的两种创建方式

    fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...

  4. Android之Fragment学习笔记①

    Android Fragment完全解析,关于碎片你所需知道的一切 一. 什么是FragmentFragment(碎片)就是小型的Activity,它是在Android3.0时出现的.Fragment ...

  5. Android之 Fragment

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

  6. [转]Android:Activity+Fragment及它们之间的数据交换(一)

    2014-05-18         来源:Android:Activity+Fragment及它们之间的数据交换(一)   简介: 为什么要用Fragment?使用Fragment可以在一个Acti ...

  7. Android:Activity+Fragment及它们之间的数据交换.

    Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...

  8. android之Fragment基础详解(一)

      一.Fragment的设计哲学 Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕比手机的大得多,有 ...

  9. Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...

  10. android之fragment的使用

    android中的fragment与html中的div很类似,下图中通过左边的按键可以控制右边的显示内容.右边的内容就是一个fragment,通过点击按键来控制fragment的实现. 工程目录 需要 ...

随机推荐

  1. WinXP局域网共享设置

    关闭局域网共享 1.不允许SAM帐户和共享的匿名枚举(系统默认是允许的). 组策略-计算机配置-Windows 设置-安全设置-本地安全策略-安全选项-网络访问:不允许SAM帐户和共享的匿名枚举. 设 ...

  2. SpringMVC整合Shiro,Shiro是一个强大易用的Java安全框架,提供了认证、授权、加密和会话管理等功能

    SpringMVC整合Shiro,Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能. 第一步:配置web.xml <!-- 配置Shiro过滤器,先让Shiro ...

  3. 30分钟学会如何使用Shiro(转)

    本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分 ...

  4. HDU 1556 Color the ball【算法的优化】

    /* 解题思路:每次仅仅求解一開始的第一个数字,让第一个数字加一,最后的一个数字的后面一个数减一.我们能够想想,最后加的时候,就是加上前面一个数出现的次数和自己本身出现的次数. 解题人:lingnic ...

  5. 介绍linux设备驱动编程

    目前,Linux软件工程师大致可分为两个层次: (1)Linux应用软件工程师(Application Software Engineer):       主要利用C库函数和Linux API进行应用 ...

  6. JVM学习:方法重载的优先级

    重载:方法名一致,参数长度或者类型不一致. 先放总结,下面为例子 参数具有继承.实现关系,优先考虑子类: 在不考虑对基本类型自动装拆箱(auto-boxing,auto-unboxing),以及可变长 ...

  7. php课程 8-29 gd库能够画哪些东西

    php课程 8-29 gd库能够画哪些东西 一.总结 一句话总结:文字,点,线,圆,弧线,矩形,各种形状都是可以的,和html5中的canva能画的东西很像,使用也很像,参数怎么记呢,参数完全不用记, ...

  8. js进阶 12-6 监听鼠标滚动事件和窗口改变事件怎么写

    js进阶 12-6 监听鼠标滚动事件和窗口改变事件怎么写 一.总结 一句话总结:滚动事件scroll(),浏览器窗口调整监听resize(),思考好监听对象. 1.滚动事件scroll()的监听对象是 ...

  9. 自己写的关于生产者与消费者模式,还有定时任务的demo

    为了加深对生产者消费者模式的理解,特意写了这个demo,里面还包含了一个自己写的定时任务.代码下载地址:http://download.csdn.net/detail/li_yan_fei/98115 ...

  10. embed-it_Integrator memory compile工具使用之二

    embed-it_Integrator memory compile工具使用之二 主要内容 使用ish接口自动加载memory的cfg文件运行生成memory 脚本内容 打开Integrate &am ...