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. 《Android源码设计模式》--原型模式

    No1: 原型模式使用场景: 1)类初始化需要消耗非常多的资源,这个资源包括数据.硬件资源等,通过原型复制避免这些消耗 2)通过new产生一个对象需要非常繁琐的数据准备货访问权限,这是可以使用原型模式 ...

  2. 百道CTF刷题记录(一)

    简介 最近在刷CTF题,主攻Web,兼职Misc Shiyanbar 0x01 简单的登陆题 简单概括: 考点: %00截断正则 CBC字节翻转攻击 难度: 难 WP:https://blog.csd ...

  3. Kotlin in Action 笔记

    Kotlin 参考 官网 reference kotlin实战 Try Kotlin Kotlin China Github 简介 Kotlin是一门把Java平台作为目标的新的编程语言.它简洁.安全 ...

  4. 怎么将maven项目打包成war包

    问题:我在eclipse上能运行,然后我想将这个maven项目打包成war然后放到另外一台机子上(其实是手动放在tomcat的webapp目录中),提供外部访问.现在问题是,一直maven项目打包一直 ...

  5. 网络数据修改工具netsed

    网络数据修改工具netsed   通过修改网络数据,可以绕过软件和防火墙的限制,达到特定的目的.Kali Linux提供一个简易数据修改工具netsed.该工具支持对TCP和UDP的数据进行修改.渗透 ...

  6. python使用web.py开发httpserver,解决post请求跨域问题

    使用web.py做http server开发时,遇到postman能够正常请求到数据,但是浏览器无法请求到数据,查原因之后发现是跨域请求的问题. 跨域请求,就是在浏览器窗口中,和某个服务端通过某个 “ ...

  7. BZOJ 4289: PA2012 Tax 差分建图 最短路

    https://www.lydsy.com/JudgeOnline/problem.php?id=4289 https://www.cnblogs.com/clrs97/p/5046933.html  ...

  8. [CC-XYHUMOQ]A humongous Query

    [CC-XYHUMOQ]A humongous Query 题目大意: 有一个长度为\(n(n\le32)\)的以\(1\)开头,\(0\)结尾的\(01\)序列\(S\).令\(f(S)\)表示序列 ...

  9. Codeforces Round #102 (Div. 1) A. Help Farmer 暴力分解

    A. Help Farmer 题目连接: http://www.codeforces.com/contest/142/problem/A Description Once upon a time in ...

  10. vijos 1659 河蟹王国 线段树区间加、区间查询最大值

    河蟹王国 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 https://vijos.org/p/1659 Description 河蟹王国有一位河蟹国王,他 ...