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 ...
随机推荐
- PhantomJS 远程做调试
做爬虫的工程师,一定会用到phantomjs,这是一个在linux上用的无界面的浏览器 在终端用phantomjs来爬取数据,或者是做测试,怎么去能看到执行到哪一步了,去实时的观测. 其实chrome ...
- 牛客练习赛3 B - 贝伦卡斯泰露
链接:https://www.nowcoder.net/acm/contest/13/B来源:牛客网 题目描述 贝伦卡斯泰露,某种程度上也可以称为古手梨花,能够创造几率近乎 为0的奇迹,通过无限轮回成 ...
- Java注解Annotation(一)
Java注解Annotation(一)——简介 这一章首先简单介绍一下注解,下一章会给出一个注解应用的DEMO. 1. 元注解 元注解的作用是负责注解其他的注解. JDK1.5中,定义了4个标准的me ...
- CentOS系统php5.6安装ImageMagick处理webp格式图片
1.先安装webp yum install libwebp 2.编译安装ImageMagick 之前有过yum安装的先卸载 yum remove ImageMagick 我使用的是老版本ImageMa ...
- 快速排序之C++实现
快速排序之C++实现 一趟快速排序的算法是: 1)设置两个变量i.j,排序开始的时候:i=0,j=N-1: 2)以第一个数组元素作为关键数据,赋值给key,即key=A[0]: 3)从j开始向前搜索, ...
- 命令神器:lsof 常用
lsof -i 显示所有网络连接lsof -i 6 获取IPv6信息lsof -itcp 显示tcp连接lsof -i:80 显示指定端口信息lsof -i@172.12.5.6 显示指定ip连接ls ...
- Java中的Runnable、Callable、Future、FutureTask的区别
本文转载自:http://blog.csdn.net/bboyfeiyu/article/details/24851847 Runnable 其中Runnable应该是我们最熟悉的接口,它只有一个ru ...
- elasticsearch实例讲解增删改查
1.首先弄明白四个概念 elasticsearch 关系型数据库 index 数据库 type 表 document 行 field 字段 如果刚一开始理解比较困难,那你就在心中默念100遍,10遍也 ...
- Notepad++源代码阅读——窗口封装与继承
引言 近期在看Notepad++的源代码,学习学习Win32 原生API的开发技巧. 本文以Notepad++ 1.0版本的源代码为例讲解如何封装windows窗口,实现面向对象开发,如何通过窗口的继 ...
- Git_Bug分支
软件开发中,bug就像家常便饭一样.有了bug就需要修复,在Git中,由于分支是如此的强大,所以,每个bug都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支删除. 当你接到一个修复一 ...