Android中Fragment的简单介绍
Android是在Android 3.0 (API level 11)引入了Fragment的,中文翻译是片段或者成为碎片(个人理解),可以把Fragment当成Activity中的模块,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity运行的时候可以加载或者移除Fragment模块。
其中有个经典图,大家就字面上理解下就行:

可以把Fragment设计成可以在多个Activity中复用的模块,为了在Android上创建动态的、多窗口的用户交互体验,你需要将UI组件和Activity操作封装成模块进行使用,在activity中你可以对这些模块进行切入切出操作。可以使用Fragment来创建这些模块,如果一个fragment定义了自己的布局,那么在activity中它可以与其他的fragments生成不同的组合,从而为不同的屏幕尺寸生成不同的布局(一个小的屏幕一次只放一个fragment,大的屏幕则可以两个或以上的fragment),比如说下图:

Fragment的两种显示方式
首先,新建一个布局文件fragmentcontent.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_container"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:src="@drawable/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </FrameLayout>
这时候与对应的是建立一个显示布局文件的ContentFragment类:
public class ContentFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragmentcontent, container, false);
}
}
inflate()方法的三个参数:
第一个是resource ID,指明了当前的Fragment对应的资源文件;
第二个参数是父容器控件;
第三个布尔值参数表明是否连接该布局和其父容器控件,在这里的情况设置为false,因为系统已经插入了这个布局到父控件,设置为true将会产生多余的一个View Group。
如果需要在Mainactivity中显示的话:
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragmenttest.ContentFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
activity_main.xml中的代码为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.fragmenttest.MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中共十八大四中全会召开" /> <fragment
android:id="@+id/fragment1"
android:name="com.example.fragmenttest.ContentFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="依法治国,以人为本" /> <LinearLayout
android:id="@+id/parentTest"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
显示的结果为:

这个时候可以在Mainactivity中加一段代码:
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
ContentFragment fragment = new ContentFragment();
fragmentTransaction.add(R.id.parentTest, fragment);
fragmentTransaction.commit();
结果如下:

FragmentManager操作Fragment
基本操作的就是添加,替换,删除(如果是定义在xml文件中的是不可以删除的)
自定义的添加:
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
ContentFragment fragment = new ContentFragment();
fragmentTransaction.add(R.id.parentTest, fragment);
fragmentTransaction.commit();
替换:
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
TitleFragment fragment = new TitleFragment();
fragmentTransaction.replace(R.id.fragment1, fragment);
fragmentTransaction.commit();
如果仔细看一下,上面应该还是可以看出来基本上操作就是获取Manager,然后就行添加,删除操作,关于内部Fragments之间的交互还在研究中,希望有机会可以补发~
Android中Fragment的简单介绍的更多相关文章
- Android中Fragment和ViewPager那点事儿(仿微信APP)
在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...
- Android开发——Fragment的简单使用总结
前言: 之前搞项目的时候,就使用了这个Fragment,中间遇到了许多坑,把坑都解决了,现在写一篇较为简单的Fragment使用总结 Fragment的简单介绍: 简单来说,Fragment其实可以理 ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- Android 中Fragment使用
Android 中Fragment使用 public class MainActivity extends Activity { public static String[] array = { &q ...
- [原创]Android中LocationManager的简单使用,获取当前位置
Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置:注册/注销来自某个 LocationProvider的周期性的位置更新:以及注册/注销接近 ...
- Android中Fragment的两种创建方式
fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...
- Android中ProgressDialog的简单示例
网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下: 建立android工程等工作都略去,Google一下就可以了. 下面来介绍主 ...
- Android中View和ViewGroup介绍
1. 概念Android中的View与我们以前理解的“视图”不同.在Android中,View比视图具有更广的含义,它包含了用户交互和显示,更像Windows操作系统中的window. ViewGro ...
- Android中fragment之间和Activity的传值、切换
功能介绍:通过一个activity下方的三个按钮,分别是发送消息(sendButton).聊天记录(chatButton).常用语(commonButton).当单击按钮是,来切换上方的fragmen ...
随机推荐
- 【知了堂学习笔记】java 编写几种常见排序算法2
排序的分类: 1.直接选择排序 它的基本思想是:第一次从R[0]~R[n-1]中选取最小值,与R[0]交换,第二次从R[1]~R[n-1]中选取最小值,与R[1]交换,....,第i次从R[i-1]~ ...
- 【原创】MySQL CPU %sys高的案例分析(一)
[现象] 最近关注MySQL CPU告警的问题时,发现有一种场景,有一些服务器最近都较频繁的出现CPU告警,其中的现象是 SYS CPU占比较高. 下面的截图来源于“MySQL CPU报警”采集的文件 ...
- Javap -c 字节码解析
栈和局部变量操作 将常量压入栈的指令 aconst_null 将null对象引用压入栈 iconst_m1 将int类型常量-1压入栈 icon ...
- 八皇后II
用一个数组state记录已经选择的每一行皇后所在的位置,DFS count = 0 N = 8 state = [0]*N def dfs(row): global count for col in ...
- 支撑大规模公有云的Kubernetes改进与优化 (2)
接下来我们按照kubernetes创建容器的详细过程,以及可能存在的问题. 一.API Server的认证,鉴权,Quota 当客户需要创建一个pod的时候,需要先请求API Server. Kube ...
- 使用Python发送HTML格式的邮件(收到的邮件有发送方才是正解)
发送html格式的和普通文本格式差不多,只是MIMEText(content,"html","utf-8"))与MIMEText(content,"p ...
- 解决mongo 端口占用问题
在打开mongod之后如果不用了就按ctrl + c ,就不会出现以下的问题了 执行mongod报错 mongod 2016-08-03T14:31:15.691+0800 I CONTROL [in ...
- 【搜索+DP】codevs1066-引水入城
[题目大意] 一个N行M列的矩形,如上图所示,其中每个格子都代表一座城 市,每座城市都有一个海拔高度.现在要在某些城市建造水利设施.水利设施有两种,分别为蓄水厂和输水站.蓄水厂的功能是利用水泵将湖泊中 ...
- bzoj 3611
和BZOJ消耗站一样,先将那个询问的简图构建出来,然后就是简单的树形DP. (倍增数组开小了,然后就狂WA,自己生成的极限数据深度又没有那么高,链又奇迹般正确) #include <cstdio ...
- python开发_tkinter_单选按钮
这篇blog主要是描述python中tkinter的单选按钮操作 下面是我做的demo 运行效果: ====================================== 代码部分: ===== ...