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的简单介绍的更多相关文章

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

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

  2. Android开发——Fragment的简单使用总结

    前言: 之前搞项目的时候,就使用了这个Fragment,中间遇到了许多坑,把坑都解决了,现在写一篇较为简单的Fragment使用总结 Fragment的简单介绍: 简单来说,Fragment其实可以理 ...

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

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

  4. Android 中Fragment使用

    Android 中Fragment使用 public class MainActivity extends Activity { public static String[] array = { &q ...

  5. [原创]Android中LocationManager的简单使用,获取当前位置

    Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置:注册/注销来自某个 LocationProvider的周期性的位置更新:以及注册/注销接近 ...

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

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

  7. Android中ProgressDialog的简单示例

    网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下: 建立android工程等工作都略去,Google一下就可以了. 下面来介绍主 ...

  8. Android中View和ViewGroup介绍

    1. 概念Android中的View与我们以前理解的“视图”不同.在Android中,View比视图具有更广的含义,它包含了用户交互和显示,更像Windows操作系统中的window. ViewGro ...

  9. Android中fragment之间和Activity的传值、切换

    功能介绍:通过一个activity下方的三个按钮,分别是发送消息(sendButton).聊天记录(chatButton).常用语(commonButton).当单击按钮是,来切换上方的fragmen ...

随机推荐

  1. [js]DOM 篇

    DOM 是 JavaScript 操作网页的接口,全称为“文档对象模型”(Document Object Model).它的作用是将网页转为一个 JavaScript 对象,从而可以用脚本进行各种操作 ...

  2. Linux-看完这篇Linux基本的操作就会了(转)

    前言 只有光头才能变强 这个学期开了Linux的课程了,授课的老师也是比较负责任的一位.总的来说也算是比较系统地学习了一下Linux了~~~ 本文章主要是总结Linux的基础操作以及一些简单的概念~如 ...

  3. JQuery的源码阅读

    探索原理,animation实现,一个对象可以同时绑定多个事件,这是如何实现的? (function(window, undefined) { function jQuery(selector){ r ...

  4. Ubntu 14.04 下 开源骨架跟踪-skeltrack

    Skeltrack是个不错的开源骨架跟踪软件.跟踪起来还相对的稳定速度还不错.能满足基本的体感功能.下面来介绍下怎么安装.  1.运行环境配置 #need clutter 1.8 or greater ...

  5. .net mvc控制器传递方法到视图

    很多人都是在视图里面定义方法,然后再使用.我个人也是这么干的.但是为了验证是否可以将方法从控制器传递到视图,所以做了个测试.结果真的可以.原理是利用了委托(delegate),因为委托本身就是一种类型 ...

  6. 装了wamp之后,80端口被占用解决办法

    1.如果装了IIS,那么把IIS停掉. 2.如果装了sqlserver,那么在cmd里面执行命令:services.msc,进入服务里面,把SQL Server Reporting Services ...

  7. 【原】使用Spring自带的JdbcTemplate。

    使用Spring自带的JdbcTemplate,可以简化对数据库的操作,用起来十分方便.通过一下几个步骤的配置,即可以使用JdbcTemplate. (1)配置好Spring的数据源,加入mysql驱 ...

  8. 解决MySQL建立连接问题,快速回收复用TCP的TIME_WAIT

    最近同事遇到一个问题,使用python开发的工具在执行的时候无法和MySQL建立连接,其最直接的现象就是满篇的TIME_WAIT,最后通过调整tcp_timestamps参数问题得以解决,再次记录一下 ...

  9. 由最小生成树(MST)到并查集(UF)

    背景 最小生成树(Minimum Spanning Tree)的算法中,克鲁斯卡尔算法(Kruskal's algorithm)是一种常用算法. 在克鲁斯卡尔算法中的一个关键问题是如何判断图中的两个点 ...

  10. LINUX block I/O --systemtap

    http://hushi55.github.io/2015/10/16/Block-Input-Output/ http://myaut.github.io/dtrace-stap-book/kern ...