android——fragment详解
在android开发过程中,如果使用到了导航栏。那么不可避免的就需要使用fragment来处理界面。闲着没事,就详解一下Framgent的使用方法吧。
难得写一次。本人
shoneworn
shonewron
shoneworn
重要事情说三遍。
1.Fragment 的生命周期

场景演示 : 切换到该Fragment
11-29 14:26:35.095: D/AppListFragment(7649): onAttach
11-29 14:26:35.095: D/AppListFragment(7649): onCreate
11-29 14:26:35.095: D/AppListFragment(7649): onCreateView
11-29 14:26:35.100: D/AppListFragment(7649): onActivityCreated
11-29 14:26:35.120: D/AppListFragment(7649): onStart
11-29 14:26:35.120: D/AppListFragment(7649): onResume
屏幕灭掉:
11-29 14:27:35.185: D/AppListFragment(7649): onPause
11-29 14:27:35.205: D/AppListFragment(7649): onSaveInstanceState
11-29 14:27:35.205: D/AppListFragment(7649): onStop
屏幕解锁
11-29 14:33:13.240: D/AppListFragment(7649): onStart
11-29 14:33:13.275: D/AppListFragment(7649): onResume
切换到其他Fragment:
11-29 14:33:33.655: D/AppListFragment(7649): onPause
11-29 14:33:33.655: D/AppListFragment(7649): onStop
11-29 14:33:33.660: D/AppListFragment(7649): onDestroyView
切换回本身的Fragment:
11-29 14:33:55.820: D/AppListFragment(7649): onCreateView
11-29 14:33:55.825: D/AppListFragment(7649): onActivityCreated
11-29 14:33:55.825: D/AppListFragment(7649): onStart
11-29 14:33:55.825: D/AppListFragment(7649): onResume
回到桌面
11-29 14:34:26.590: D/AppListFragment(7649): onPause
11-29 14:34:26.880: D/AppListFragment(7649): onSaveInstanceState
11-29 14:34:26.880: D/AppListFragment(7649): onStop
回到应用
11-29 14:36:51.940: D/AppListFragment(7649): onStart
11-29 14:36:51.940: D/AppListFragment(7649): onResume
退出应用
11-29 14:37:03.020: D/AppListFragment(7649): onPause
11-29 14:37:03.155: D/AppListFragment(7649): onStop
11-29 14:37:03.155: D/AppListFragment(7649): onDestroyView
11-29 14:37:03.165: D/AppListFragment(7649): onDestroy
11-29 14:37:03.165: D/AppListFragment(7649): onDetach
可以看出,和activity还是有差别的。
2.fragment的使用
fragment使用的时候,合理的选择容器是很重要的。如果容器选择不对。那么就可能达不到自己想要的效果。
由于fragment在viewgroup中是作为一个view来显示的。也就是只能作为一部分。所以,如果fragment不能覆盖整个屏幕,就会出现原来的view和新的view一起显示的景象。下面是用我线性布局来做为容器来放fragment。先看看出现的什么效果吧
点击textview1 后创建新的fragment2 。每次点击后,fragment2没有去沾满屏幕。而是不停的向下依次排列。在点击返回键后,又一次的回退,将fragment2 pop出栈。

布局文件:
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#32dfda"
android:gravity="center"
android:text="TextView1"
android:textSize="18sp" /> <TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#36df0a"
android:gravity="center"
android:text="TextView1"
android:textSize="18sp" /> <TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#82dfda"
android:gravity="center"
android:text="TextView1"
android:textSize="18sp" /> </LinearLayout>
fragment1.java 代码:
package com.ailin.accm.activity; import com.ailin.accm.R; import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView; public class Fragment1 extends Fragment {
private Context context; public Fragment1(Context context) { this.context = context;
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, null);
initView(view);
return view;
} private void initView(View view) {
TextView tv1 = (TextView) view.findViewById(R.id.textView1);
tv1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { Fragment2 fg2 = new Fragment2(context);
FragmentTransaction fragmentTrans = getFragmentManager().beginTransaction();
fragmentTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTrans.add(R.id.container, fg2, "fg2");
fragmentTrans.addToBackStack(null);
fragmentTrans.commit();
}
}); } }
从上面可以看出,在线性布局container中,fragment2作为一个view,被依container依次给addview进去了。那么,如果container换成相对布局呢?又会是什么样子?

可以看出,这个时候,在点击textview1的话,fragment2,就会覆盖fragment1.
PS:补充说明一下,container在添加fragment2 作为view的时候,会进行一次重绘。所以,在设置fragment2.xml的时候,最好使用相对布局RelativeLayout .否则,你的fragment在tab2中能正常显示,但是,拿到fragment1中作为一个view添加上去了,是按照绝对大小来添加的。给大家看看效果吧。
fragment1 使用相对布局作为容器。 fragment2.xml 中使用线性布局。效果看下图。

但是如果fragment2.xml也换成了相对布局,效果就是第二幅图那样了。
鸣谢:http://blog.csdn.net/forever_crying/article/details/8238863/
借用了一下对方的图。文章也写的很好。
android——fragment详解的更多相关文章
- Android Fragment详解
一.什么是Fragment Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕要比手机的大得多,有更多的 ...
- Android Fragment 详解(一)
Android从3.0开始引入fragment,主要是为了支持更动态更灵活的界面设计,比如在平板上的应用.平板机上拥有比手机更大的屏幕空间来组合和交互界面组件们.Fragment使你在做那样的设计时, ...
- Android Fragment详解(二):Fragment创建及其生命周期
Fragments的生命周期 每一个fragments 都有自己的一套生命周期回调方法和处理自己的用户输入事件. 对应生命周期可参考下图: 创建片元(Creating a Fragment) To c ...
- Android Fragment详解(一):概述
Fragment是activity的界面中的一部分或一种行为.你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一个Fragment.你 ...
- Android Fragment详解(六):Fragement示例
把条目添加到动作栏 你的fragment们可以向activity的菜单(按Manu键时出现的东西)添加项,同时也可向动作栏(界面中顶部的那个区域)添加条目,这都需通过实现方法onCreateOptio ...
- Android Fragment详解(三): 实现Fragment的界面
为fragment添加用户界面: Fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中. 一个 要为fragment提供layout ...
- Android Fragment 详解(未完...)
版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Fragment 文中如有纰漏,欢迎大家留言指出. 之前写过一篇关于 Fragment 生命周期的文章 ...
- Android Fragment详解(五):Fragment与Activity通讯
与activity通讯 尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例. Fragment可 ...
- Android Fragment详解(四):管理Fragment
要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager(). 你可以用FragmentManager来做以上事情: ...
随机推荐
- Audio笔记之MediaPlayerService:setDataSource
//下面是一个典型的播放序列: MediaPlayer player=new MediaPlayer() player->setDataSource(url,header); player-&g ...
- NuGet学习笔记(3)——搭建属于自己的NuGet服务器(转)
在上一篇NuGet学习笔记(2) 使用图形化界面打包自己的类库 中讲解了如何打包自己的类库,接下来进行最重要的一步,从零开始搭建属于自己的NuGet服务器,诚然园子里及其它很多地方已经有完全写好的Nu ...
- ASP.NET母版与内容页相对路径的问题
1. 图片问题 非常好解决 <img runat="server" src="~/images/ad468x60.gif" alt="" ...
- animation与transition
animation 动画,无法直接决定开始时间 demo1 @-webkit-keyframes demo-animation1{ 0% { -webkit-transform:translate3d ...
- xml入门简介--两天学会xml
前言 在很久以前,笔者曾见到过1000+页的xml书,里面还有n多的概念,XSL,Xquery,让人头痛.无奈最近需要用到,所以在w3c恶补了一下.以下大致整理了一下相关概念,但是对XSL等派生语言没 ...
- iPad学做菜
项目描述:家常菜.川菜 .鲁菜.东北菜.甜品等各大菜系应有尽有,详细的制作步骤,再也不用为自己不会做饭而烦恼. 主要技术:主界面采用UISplitViewController的结构设计:自定义各大菜系 ...
- K - Ignatius and the Princess IV
Description "OK, you are not too bad, em... But you can never pass the next test." ...
- JavaScript match 和 exec 备忘笔记
这是一道广为引用的面试题:var someText="web2.0 .net2.0"; var pattern=/(\w+)(\d)\.(\d)/g; var outCome_ex ...
- li浮动引起ul高度坍陷的解决方法
我们都知道float在CSS中的作用是使元素脱离正常的文档流并使其移动到其父元素的“最左边”或“最右边”.元素浮动之后,它脱离当前正常的文档流,所以无法撑开其父元素,造成父元素的高度塌陷.如下的代码就 ...
- POJ3026 最小生成树
问题: POJ3026 分析: 采用BFS算出两两之间的距离,再用PRIM算法计算最小生成树. AC代码: //Memory: 220K Time: 32MS #include <iostrea ...